import React from 'react';
import { configure, shallow } from 'enzyme';
import Adapter from '@cfaester/enzyme-adapter-react-18';
import Licit, { DataType } from './Licit';
import RichTextEditor from '../ui/RichTextEditor';
import { noop } from '@modusoperandi/licit-ui-commands';

/**
 * Configure Jest to use react / enzyme
 */
configure({
  adapter: new Adapter()
});
describe('<Licit />', function () {
  var wrapper;
  var licit;

  // provide an empty document just to shut up that warning
  var data = {
    type: 'doc',
    content: [{
      type: 'paragraph',
      content: [{
        type: 'text',
        text: ' '
      }]
    }]
  };

  // Mocking the functions used in _onReady
  var fakeEditorView = {
    focus: noop,
    dispatch: noop,
    state: {
      doc: {
        content: {
          size: 10
        },
        resolve: function resolve() {
          return {
            min: function min() {
              return 0;
            },
            max: function max() {
              return 10;
            },
            parent: {
              inlineContent: true
            }
          };
        },
        toJSON: function toJSON() {
          return data;
        }
      },
      tr: {
        setSelection: function setSelection() {
          return fakeEditorView.state.tr;
        },
        scrollIntoView: noop
      }
    }
  };
  beforeEach(function () {
    wrapper = shallow( /*#__PURE__*/React.createElement(Licit, {
      data: data
    }));
    licit = wrapper.instance();
  });
  it('should render a <RichTextEditor /> ', function () {
    expect(wrapper.find(RichTextEditor)).toBeTruthy();
  });
  describe('editorView (getter)', function () {
    it('should return the prosemirror view', function () {
      // Using shallow, the underlying RichTexEditor is never really created,
      // and Licit's _onReady method is not called.  Call it here with the fake
      // view created above
      licit._onReady(fakeEditorView);

      // Verify that getter now returns the underlying view.
      expect(licit.editorView).toBe(fakeEditorView);
    });
  });
});
describe('<Licit with HTML input/>', function () {
  var wrapper;
  var licit;
  var HELLO = 'Hello ';
  var WORLD = 'World';
  var data = '<p stylename="None">' + HELLO + '<strong overridden="false">' + WORLD + '</strong></p>';
  beforeEach(function () {
    wrapper = shallow( /*#__PURE__*/React.createElement(Licit, {
      data: data,
      dataType: DataType.HTML
    }));
    licit = wrapper.instance();
  });
  it('should render a <RichTextEditor /> ', function () {
    expect(wrapper.find(RichTextEditor)).toBeTruthy();
  });
  it('should match state text content with the passed in text ', function () {
    expect(licit.state.editorState.doc.textContent).toBe(HELLO + WORLD);
  });
});