import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import { LOGGED_IN } from '../../constants/actionTypes';
import { LOGBOOK_CONTAINER_CONTEXT } from '../../constants/eventTypes';
import getUIConfiguration from '../uiConfig';

require('it-each')({ testPerIteration: true });
const resources = require('./resources/LogbookContainer.resource.js');

beforeEach(() => {
  Enzyme.configure({ adapter: new Adapter() });
  jest.resetModules();
});

/* In these tests, redux store is not used. Tests involving the redux store should go in integration tests folder*/
describe.skip('LogbookContainerUnitTests', () => {
  describe('Rendering', () => {
    it.each(
      resources.renderingEventListOrEmptyLogbookMessage,
      '[renderingEventListOrEmptyLogbookMessage: %s ]',
      ['aboutThisTest'],
      (element, next) => {
        //mock the configuration file. Since mockGetUIConfiguration is out of scope of jest.mock(), it must start with 'mock'.
        const mockGetUIConfiguration = getUIConfiguration({
          AUTOREFRESH_EVENTLIST: false,
        });
        jest.mock('../../config/ui/config', () => mockGetUIConfiguration);

        // require logbook class after the configuration mocking
        const LogbookContainerClass = require('../../containers/Logbook/LogbookContainer')
          .LogbookContainer;
        LogbookContainerClass.prototype.getEventsBySelectionFilter = jest.fn(
          () => Promise.resolve(element.getEventsBySelectionFilterResponse1)
        );
        LogbookContainerClass.prototype.getEventCountBySelectionFilter = jest.fn(
          () => Promise.resolve(element.getEventCountBySelectionFilterResponse1)
        );

        const wrapper = getShallowWrapper({
          logbookContext: element.logbookContext,
        });

        setTimeout(() => {
          wrapper.update();
          expect(wrapper.exists('EventList')).toBe(
            element.expected.showsEventList
          );
          expect(wrapper.find('UserMessage').length).toBe(
            element.expected.userMessageCount
          );
          if (element.expected.userMessageCount === 1) {
            expect(wrapper.find('UserMessage').prop('message')).toEqual(
              element.expected.userMessageProps.message
            );
            expect(wrapper.find('UserMessage').prop('type')).toEqual(
              element.expected.userMessageProps.type
            );
          }
          next();
        }, 200);
      }
    );
  });
});

/**
 * Get the shallow wrapper of the logbook
 * @param {Object} props provided to the logbook
 */
function getShallowWrapper(props) {
  if (props) {
    props.availableTags = props.availableTags ? props.availableTags : [];
    props.clearAvailableTags = props.clearAvailableTags
      ? props.clearAvailableTags
      : () => null;
    props.logbookContext = props.logbookContext
      ? props.logbookContext
      : { name: LOGBOOK_CONTAINER_CONTEXT, isReleased: false };
    props.setLogbookContext = props.setLogbookContext
      ? props.setLogbookContext
      : () => null;
    props.user = props.user
      ? props.user
      : { type: LOGGED_IN, username: 'username', sessionId: 'sessionId' };

    const LogbookContainer = require('../../containers/Logbook/LogbookContainer')
      .LogbookContainer;
    return Enzyme.shallow(
      <LogbookContainer
        investigationId="testInvestigationId"
        /* redux props (redux not used here) */
        availableTags={props.availableTags}
        clearAvailableTags={props.clearAvailableTags}
        logbookContext={props.logbookContext}
        setLogbookContext={props.setLogbookContext}
        user={props.user}
      />
    );
  }
}
