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

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

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

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

describe('PublicEventPanelUnitTests', () => {
  describe('rendering', () => {
    it.each(
      resources.renderingParagraphWhenEventHasASingleVersion,
      '[renderingParagraphWhenEventHasASingleVersion: %s ]',
      ['description'],
      (element, next) => {
        expect(
          getShallowedWrapper({ event: element.event }).find('p').length
        ).toBe(element.expected.paragraphCount);
        if (element.expected.paragraphCount !== 0) {
          expect(
            getShallowedWrapper({ event: element.event })
              .find('p')
              .prop('children')
          ).toEqual(element.expected.paragraphText);
        }
        next();
      }
    );

    it.each(
      resources.renderingEventVersionPanel,
      '[renderingEventVersionPanel: %s ]',
      ['description'],
      (element, next) => {
        expect(
          getShallowedWrapper({ event: element.event }).find(
            'EventVersionPanel'
          ).length
        ).toBe(element.expected.eventVersionPanelCount);

        expect(
          getShallowedWrapper({ event: element.event })
            .find('EventVersionPanel')
            .at(0)
            .prop('availableTags')
        ).toEqual(element.expected.first.availableTags);
        expect(
          getShallowedWrapper({ event: element.event })
            .find('EventVersionPanel')
            .at(0)
            .prop('event')
        ).toEqual(element.expected.first.event);
        expect(
          getShallowedWrapper({ event: element.event })
            .find('EventVersionPanel')
            .at(0)
            .prop('version')
        ).toEqual(element.expected.first.version);
        if (element.expected.eventVersionPanelCount > 1) {
          expect(
            getShallowedWrapper({ event: element.event })
              .find('EventVersionPanel')
              .at(1)
              .prop('availableTags')
          ).toEqual(element.expected.second.availableTags);
          expect(
            getShallowedWrapper({ event: element.event })
              .find('EventVersionPanel')
              .at(1)
              .prop('event')
          ).toEqual(element.expected.second.event);
          expect(
            getShallowedWrapper({ event: element.event })
              .find('EventVersionPanel')
              .at(1)
              .prop('version')
          ).toEqual(element.expected.second.version);
        }
        next();
      }
    );

    it.each(
      resources.renderingEventFooter,
      '[renderingEventFooter: %s ]',
      ['description'],
      (element, next) => {
        const wrapper = getShallowedWrapper({ event: element.event });
        expect(wrapper.find('EventFooter').prop('isSaveButtonEnabled')).toBe(
          element.expected.isSaveButtonEnabled
        );
        expect(wrapper.find('EventFooter').prop('isSaveButtonVisible')).toBe(
          element.expected.isSaveButtonVisible
        );
        expect(wrapper.find('EventFooter').prop('cancelButtonLabel')).toEqual(
          element.expected.buttonLabel
        );
        next();
      }
    );
  });
});

function getShallowedWrapper(propsObject) {
  return Enzyme.shallow(
    <PublicEventPanel
      event={propsObject.event}
      onCloseButtonClicked={
        propsObject.onCloseButtonClicked
          ? propsObject.onCloseButtonClicked
          : () => null
      }
    />
  );
}
