import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import NewOrEditEventPanel from '../../components/Logbook/NewOrEditEventPanel';

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

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

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

describe('NewOrEditEventPanelUnitTests', () => {
  describe('rendering', () => {
    it.each(
      resources.renderingEventHeader,
      '%s',
      ['description'],
      (element, next) => {
        expect(
          getShallowedWrapper({ event: element.event })
            .find('PanelHeading')
            .find('b')
            .text()
        ).toEqual(element.expected);
        next();
      }
    );

    it.each(
      resources.renderingEditorWrapper,
      '%s',
      ['description'],
      (element, next) => {
        expect(
          getShallowedWrapper({
            event: element.event,
            investigationId: element.investigationId,
            user: element.user,
          })
            .find('EditorWrapper')
            .prop('event')
        ).toEqual(element.event);
        expect(
          getShallowedWrapper({
            event: element.event,
            investigationId: element.investigationId,
            user: element.user,
          })
            .find('EditorWrapper')
            .prop('investigationId')
        ).toEqual(element.investigationId);
        expect(
          getShallowedWrapper({
            event: element.event,
            investigationId: element.investigationId,
            user: element.user,
          })
            .find('EditorWrapper')
            .prop('user')
        ).toEqual(element.user);
        next();
      }
    );

    it.each(
      resources.renderingAuthorAndTime,
      '%s',
      ['description'],
      (element, next) => {
        expect(
          getShallowedWrapper({ event: element.event })
            .find('AuthorAndTime')
            .prop('event')
        ).toEqual(element.event);
        next();
      }
    );

    it.each(
      resources.renderingTagContainer,
      '%s',
      ['description'],
      (element, next) => {
        if (element.event) {
          expect(
            getShallowedWrapper({
              event: element.event,
              investigationId: element.investigationId,
            })
              .find('Connect(TagsSelectorContainer)')
              .prop('tags')
          ).toEqual(element.event.tag);
        } else {
          expect(
            getShallowedWrapper({
              event: element.event,
              investigationId: element.investigationId,
            })
              .find('Connect(TagsSelectorContainer)')
              .prop('tags')
          ).toEqual([]);
        }
        expect(
          getShallowedWrapper({
            event: element.event,
            investigationId: element.investigationId,
          })
            .find('Connect(TagsSelectorContainer)')
            .prop('investigationId')
        ).toEqual(element.investigationId);
        next();
      }
    );

    it.each(
      resources.renderingEventFooter,
      '%s',
      ['description'],
      (element, next) => {
        expect(
          getShallowedWrapper({
            context: element.context,
            event: element.event,
          }).find('EventFooter')
        ).toBeDefined();
        next();
      }
    );
  });

  describe('testFunctionalProps', () => {
    // The following feature has been desactivated for the time being
    // test("onSaveButtonClicked() is called when the user clicks on 'save' button during edition", () => {
    //   let resource = resources.testFunctionalProps;
    //   let mockedOnSaveButtonClicked = jest.fn();
    //   let wrapper = getShallowedWrapper({ event: resource.event, investigationId: resource.investigationId, onSaveButtonClicked: mockedOnSaveButtonClicked, user: resource.user });
    //   expect(mockedOnSaveButtonClicked).toHaveBeenCalledTimes(0);
    //   wrapper.find("EventFooter").dive().find({ children: "Save" }).simulate("click");
    //   expect(mockedOnSaveButtonClicked).toHaveBeenCalledTimes(1);
    //   expect(mockedOnSaveButtonClicked).toHaveBeenCalledWith({
    //     _id: resource.event._id,
    //     category: resource.event.category,
    //     content: expect.anything(),
    //     creationDate: expect.anything(),
    //     type: resource.event.type,
    //     tag: resource.event.tag,
    //     title: resource.event.title,
    //     username: resource.user.username,
    //     previousVersionEvent: resource.event._id,
    //   });
    // });
  });
});

/**
 * Get a shallowed wrapper
 */
function getShallowedWrapper(propsObject) {
  return Enzyme.shallow(
    <NewOrEditEventPanel
      event={propsObject.event}
      investigationId={
        propsObject.investigationId
          ? propsObject.investigationId
          : 'investigationIdForTest'
      }
      onCancelButtonClicked={
        propsObject.onCancelButtonClicked
          ? propsObject.onCancelButtonClicked
          : () => null
      }
      onSaveButtonClicked={
        propsObject.onSaveButtonClicked
          ? propsObject.onSaveButtonClicked
          : () => null
      }
      user={
        propsObject.user
          ? propsObject.user
          : { sessionId: 'sessionIdForTest', username: 'usernameForTest' }
      }
    />
  );
}
