import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import getUIConfiguration from '../uiConfig';

require('it-each')({ testPerIteration: true });

const resources = require('./resources/logbookPager.resource');

/* code executed before each test */
beforeEach(() => {
  Enzyme.configure({ adapter: new Adapter() });
  jest.resetModules();
});

describe('LogbookPagerUnitTests', () => {
  describe('rendering', () => {
    it.each(resources.rendering, '%s', ['description'], (element, next) => {
      if (element.expected === null) {
        //mock the configuration file. Since mockGetUIConfiguration is out of scope of jest.mock(), it must start with 'mock'.
        const mockGetUIConfiguration = getUIConfiguration({
          EVENTS_PER_PAGE: element.EVENTS_PER_PAGE,
        });
        jest.mock('../../config/ui/config', () => mockGetUIConfiguration);
        expect(getWrapper(element.eventCount).get(0)).toBeNull();
      }
      next();
    });
  });

  describe('getTotalPageNumber', () => {
    it.each(
      resources.getTotalPageNumber,
      '%s',
      ['description'],
      (element, next) => {
        //mock the configuration file. Since mockGetUIConfiguration is out of scope of jest.mock(), it must start with 'mock'.
        const mockGetUIConfiguration = getUIConfiguration({
          EVENTS_PER_PAGE: element.EVENTS_PER_PAGE,
        });
        jest.mock('../../config/ui/config', () => mockGetUIConfiguration);
        expect(
          getWrapper(element.eventCount).instance().getTotalPageNumber()
        ).toBe(element.expected);
        next();
      }
    );
  });
});

function getWrapper(eventCount) {
  const LogbookPager = require('../../components/Logbook/LogbookPager').default;
  return Enzyme.shallow(
    <LogbookPager
      eventCount={eventCount}
      isFloatingRight
      onPageClicked={() => null}
    />
  );
}
