import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import EventListMenu from '../../components/Logbook/Menu/EventListMenu';
import { LIST_VIEW } from '../../constants/eventTypes';

const resources = require('./resources/EventListMenu.resource.js');

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

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

describe('EventListMenu unit tests', () => {
  describe('renderingButtons', () => {
    it.each(
      resources.renderingButtons,
      '%s',
      ['description'],
      (element, next) => {
        const NewButtonWrapper = getShallowWrapper({
          isNewButtonEnabled: element.isNewButtonEnabled,
          logbookContext: element.logbookContext,
          eventCountBySelectionFilter: element.eventCountBySelectionFilter,
        }).findWhere((n) => n.prop('text') === element.text);
        expect(NewButtonWrapper.prop('isEnabled')).toEqual(
          element.expected.isEnabled
        );
        if (element.text === 'New ' || element.text === 'Take a photo') {
          expect(NewButtonWrapper.prop('isVisible')).toEqual(
            element.expected.isVisible
          );
        }
        next();
      }
    );
  });

  describe('renderAutoRefreshMenu', () => {
    it.each(
      resources.renderingAutoRefreshMenuItem,
      '%s',
      ['description'],
      (element, next) => {
        const menuItemWrapper = getShallowWrapper({
          autorefreshEventList: element.autorefreshEventList,
          logbookContext: element.logbookContext,
        }).findWhere((n) => n.prop('eventKey') === 3.5);

        if (element.logbookContext.isReleased) {
          expect(menuItemWrapper).not.toBeUndefined();
        } else {
          expect(menuItemWrapper.childAt(0).prop('children')).toEqual(
            element.expected.text
          );
        }
        next();
      }
    );
  });

  describe('calling prop function', () => {
    it.each(
      resources.callingPropFunction,
      '%s',
      ['description'],
      (element, next) => {
        const mocked = jest.fn();
        const wrapper = getShallowWrapper({
          isSortingLatestEventsFirst: element.isSortingLatestEventsFirst,
          onSortingButtonClicked: mocked,
        });

        expect(mocked).toHaveBeenCalledTimes(0);
        wrapper
          .find({ eventKey: element.eventKey })
          .dive()
          .find('SafeAnchor')
          .dive()
          .find('a')
          .simulate('click', { preventDefault: jest.fn() });
        expect(mocked).toHaveBeenCalledTimes(element.expected);
        next();
      }
    );
  });
});

function getShallowWrapper(params) {
  if (params) {
    params.activePage = params.activePage ? params.activePage : 1;
    params.autorefreshEventList = params.autorefreshEventList
      ? params.autorefreshEventList
      : false;
    params.availableTags = params.availableTags ? params.availableTags : [];
    params.eventCountBySelectionFilter = params.eventCountBySelectionFilter
      ? params.eventCountBySelectionFilter
      : 0;
    params.eventCountSinceLastRefresh = params.eventCountSinceLastRefresh
      ? params.eventCountSinceLastRefresh
      : 0;
    params.getEvents = params.getEvents ? params.getEvents : () => null;
    params.investigationId = params.investigationId
      ? params.investigationId
      : 'testInvestigationId';
    params.isNewButtonEnabled =
      params.isNewButtonEnabled === false ? false : true;
    params.isSortingLatestEventsFirst =
      params.isSortingLatestEventsFirst === false ? false : true;
    params.logbookContext = params.logbookContext
      ? params.logbookContext
      : { name: null, isReleased: null };
    params.onPageClicked = params.onPageClicked
      ? params.onPageClicked
      : () => null;
    params.onRefreshEventListMenuItemClicked = params.onRefreshEventListMenuItemClicked
      ? params.onRefreshEventListMenuItemClicked
      : () => null;
    params.onSortingButtonClicked = params.onSortingButtonClicked
      ? params.onSortingButtonClicked
      : () => null;
    params.searchEvents = params.searchEvents
      ? params.searchEvents
      : () => null;
    params.selectionFilter = params.selectionFilter
      ? params.selectionFilter
      : {};
    params.sessionId = params.sessionId
      ? params.sessionId
      : 'sessionIdForTests';
    params.setNewEventVisibility = params.setNewEventVisibility
      ? params.setNewEventVisibility
      : () => null;
    params.setView = params.setView ? params.setView : () => null;
    params.view = params.view ? params.view : LIST_VIEW;

    return Enzyme.shallow(
      <EventListMenu
        activePage={params.activePage}
        autorefreshEventList={params.autorefreshEventList}
        availableTags={params.availableTags}
        eventCountBySelectionFilter={params.eventCountBySelectionFilter}
        eventCountSinceLastRefresh={params.eventCountSinceLastRefresh}
        getEvents={params.getEvents}
        investigationId={params.investigationId}
        isNewButtonEnabled={params.isNewButtonEnabled}
        isSortingLatestEventsFirst={params.isSortingLatestEventsFirst}
        logbookContext={params.logbookContext}
        onPageClicked={params.onPageClicked}
        onRefreshEventListMenuItemClicked={
          params.onRefreshEventListMenuItemClicked
        }
        onSortingButtonClicked={params.onSortingButtonClicked}
        searchEvents={params.searchEvents}
        selectionFilter={params.selectionFilter}
        sessionId={params.sessionId}
        setNewEventVisibility={params.setNewEventVisibility}
        setView={params.setView}
        view={params.view}
      />
    );
  }
}
