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

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

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

describe('EditorWrapperUnitTests', () => {
  describe('rendering', () => {
    test('editor can upload images to the server. The test checks that images_upload_handler() is bound to an existing function ', () => {
      EditorWrapper.prototype.imagesUploadHandler = jest.fn();
      const wrapper = getShallowWrapper({});
      expect(EditorWrapper.prototype.imagesUploadHandler).toHaveBeenCalledTimes(
        0
      );
      wrapper.prop('init').images_upload_handler();
      expect(EditorWrapper.prototype.imagesUploadHandler).toHaveBeenCalledTimes(
        1
      );
    });
  });
});

function getShallowWrapper(params) {
  if (params) {
    params.event = params.event ? params.event : undefined;
    params.investigationId = params.investigationId
      ? params.investigationId
      : 'testInvestigationId';
    params.onContentChanged = params.onContentChanged
      ? params.onContentChanged
      : () => null;
    params.user = params.user ? params.user : {};

    return Enzyme.shallow(
      <EditorWrapper
        event={params.event}
        investigationId={params.investigationId}
        onContentChanged={params.onContentChanged}
        user={params.user}
      />
    );
  }
}
