import BookReader from '../../src/js/BookReader';

beforeAll(() => {
  global.alert = jest.fn();
})
afterEach(() => {
  jest.restoreAllMocks();
});

describe('BookReader.prototype.toggleFullscreen', ()  => {
  test('will always emit an event', () => {
    const br = new BookReader();
    br.mode = br.constMode1up;
    br.enterFullscreen = jest.fn();
    br.trigger = jest.fn();
    br.refs.$brContainer = {
      css: jest.fn(),
      animate: jest.fn()
    }

    br.toggleFullscreen();
    expect(br.enterFullscreen).toHaveBeenCalled();
  });

  test('will bind `_fullscreenCloseHandler` by default', () => {
    const br = new BookReader();
    br.mode = br.constMode1up;
    br.switchMode = jest.fn();
    br.updateBrClasses = jest.fn();
    br.refs.$brContainer = {
      css: jest.fn(),
      animate: jest.fn(),
    };
    expect(br._fullscreenCloseHandler).toBeUndefined();

    br.toggleFullscreen();
    expect(br._fullscreenCloseHandler).toBeDefined();
  });

  test('will start with opening fullscreen', () => {
    const br = new BookReader();
    br.mode = br.constMode1up;
    br.enterFullscreen = jest.fn();

    br.toggleFullscreen();
    expect(br.enterFullscreen).toHaveBeenCalled();
  });

  test('will close fullscreen if BookReader is in fullscreen', () => {
    const br = new BookReader();
    br.mode = br.constMode1up;
    br.exitFullScreen = jest.fn();
    br.isFullscreenActive = true;

    br.toggleFullscreen();
    expect(br.exitFullScreen).toHaveBeenCalled();
  });
});

describe('BookReader.prototype.trigger', () => {
  test('fires custom event', () => {
    const br = new BookReader();
    global.br = br;
    global.dispatchEvent = jest.fn();

    const props = {bar: 1};
    br.trigger('foo', props);
    expect(global.dispatchEvent.mock.calls.length).toBe(1);
  });
});

describe('`BookReader.prototype.prev`', () => {
  const br = new BookReader();
  global.br = br;
  br.trigger = jest.fn();
  br.flipBackToIndex = jest.fn();
  br.jumpToIndex = jest.fn();

  test('does not take action if user is on front page', () => {
    br.firstIndex = 0;
    br.prev();
    expect(br.trigger.mock.calls.length).toBe(0);
    expect(br.flipBackToIndex.mock.calls.length).toBe(0);
    expect(br.jumpToIndex.mock.calls.length).toBe(0);
  });

  describe('2up mode', () => {
    test('fires event and turns the page', () => {
      br.firstIndex = 10;
      br.mode = br.constMode2up;
      br.prev();
      expect(br.jumpToIndex.mock.calls.length).toBe(0); // <--  does not get called
      expect(br.trigger.mock.calls.length).toBe(1);
      expect(br.flipBackToIndex.mock.calls.length).toBe(1);
    });
  })

  describe('non 2up mode', () => {
    test('jumps to provided index', () => {
      br.firstIndex = 100;
      br.mode = br.constMode1up;
      br.prev();
      expect(br.jumpToIndex.mock.calls.length).toBe(1);  // <--  gets called
      expect(br.trigger.mock.calls.length).toBe(1); // <-- gets called by `jumpToIndex` internally
      expect(br.flipBackToIndex.mock.calls.length).toBe(1); // <-- gets called by `jumpToIndex` internally
    })
  });
});


@@ -197,24 +197,424 @@
 ', ()  =%3E %7B%0A
+  test('uses %60isFullscreen%60 to check fullscreen state', () =%3E %7B%0A    const isFSmock = jest.fn();%0A    isFSmock.mockReturnValueOnce(false);%0A    const br = new BookReader();%0A    br.mode = br.constMode1up;%0A    br.enterFullscreen = jest.fn();%0A    br.isFullscreen = isFSmock;%0A%0A    br.toggleFullscreen();%0A    expect(br.isFullscreen).toHaveBeenCalled();%0A    expect(br.isFullscreen).toHaveBeenCalled();%0A  %7D);%0A%0A
   test('will

add test for `isFullscreen` check