"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _react = _interopRequireDefault(require("react"));

var _enzyme = require("enzyme");

var _ = _interopRequireDefault(require("."));

var _components = require("../../../../../components");

var _Item = _interopRequireDefault(require("../../../../../components/Menu/Item"));

var _2 = require("../..");

var getClientRectSpy = jest.fn(function () {
  return {
    width: 100
  };
});
Range.prototype.getBoundingClientRect = getClientRectSpy;
Range.prototype.getClientRects = getClientRectSpy;

window.HTMLElement.prototype.scroll = function () {};

describe('TutorialEditor', function () {
  var node;
  var props;
  beforeEach(function () {
    props = {
      key: "tutorial-editor-0",
      clearSnippetSelection: jest.fn(),
      endpoints: [{
        title: 'Api Title',
        categoryId: '1234567890',
        children: [{
          _id: '23435465768798',
          api: {
            method: 'GET'
          },
          children: [],
          parentDoc: '',
          slug: '',
          title: '',
          type: 'endpoint',
          path: ''
        }]
      }],
      getCode: jest.fn(),
      handleSnippetSelection: jest.fn(),
      lineNumbers: ['1-2'],
      referenceEnabled: true,
      selectedTab: 1,
      setSelectedTab: jest.fn(),
      snippet: {
        endpoint: {
          title: '',
          page: '23435465768798',
          slug: ''
        },
        codeOptions: [{
          name: 'Curl',
          code: '',
          language: 'curl',
          highlightedSyntax: 'powershell'
        }, {
          name: 'c',
          code: '',
          language: 'c',
          highlightedSyntax: 'c'
        }]
      },
      updateSnippet: jest.fn(),
      updateStep: jest.fn()
    };
    node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_2.PermissionContext.Provider, {
      value: false
    }, /*#__PURE__*/_react["default"].createElement(_["default"], props)));
  });
  it('should display tutorial response editor', function () {
    expect(node.find('.TutorialEditor-Nav')).toHaveLength(2);
  });
  describe('TutorialEditor event handling', function () {
    describe('Tab creation events', function () {
      var createTab;
      var langSelect;
      beforeEach(function () {
        createTab = node.find('.TutorialEditor-Nav').find('.TutorialEditor-Nav-Plus').find(_components.Box).find(_components.Menu).find(_Item["default"]).at(1);
        langSelect = node.find('.TutorialEditor-Nav').find('.TutorialEditor-Nav-Plus').find(_components.Box).find(_components.Menu).find(_Item["default"]).at(0).find(_components.Select).find('.Select').find('.Select-Input');
      });
      it('should called updateSnippet and updateStep when a tab is created', function () {
        createTab.simulate('click');
        expect(props.updateStep).toHaveBeenCalled();
        expect(props.updateSnippet).toHaveBeenCalled();
      });
      it('should NOT create a tab without a language selected', function () {
        var event = {
          target: {
            value: ''
          }
        };
        langSelect.simulate('change', event);
        createTab.simulate('click');
        expect(props.updateStep).toHaveBeenCalledTimes(0);
        expect(props.updateSnippet).toHaveBeenCalledTimes(0);
      });
      it('should call getCode on tab creation', function () {
        createTab.simulate('click');
        expect(props.getCode).toHaveBeenCalled();
      });
      it('should NOT calle getCode if there is an page mismatch', function () {
        props.snippet.endpoint.page = '';
        createTab.simulate('click');
        expect(props.getCode).toHaveBeenCalledTimes(0);
      });
      it('should set language when a new language is selected while creating a tab', function () {
        var value = JSON.stringify({
          language: 'javascript',
          highlightedSyntax: 'javascript'
        });
        var event = {
          target: {
            value: value
          }
        };
        langSelect.simulate('change', event);
        createTab.simulate('click');
        expect(props.snippet.codeOptions).toContainEqual(expect.objectContaining({
          language: 'javascript'
        }));
      });
    });
    describe('Tab name input events', function () {
      var nameInput;
      beforeEach(function () {
        nameInput = node.find('.TutorialEditor-Controls').find('.TutorialEditor-Controls-ControlWrapper').find('.TutorialEditor-Controls-ControlWrapper-Input').at(1);
      });
      it('should call updateSnippet when tab name input is changed', function () {
        var event = {
          target: {
            value: 'needstobetwentycharacterstohitanelse'
          }
        };
        nameInput.simulate('change', event);
        expect(props.updateSnippet).toHaveBeenCalled();
      });
    });
    describe('updateSnippet', function () {
      it('should call updateSnippet when code is changed in editor', function () {
        var cm = node.find('.CodeEditor').first();
        var cmProps = cm.props();
        cmProps.onChange(cm, {
          origin: '+input'
        }, 'console.log("Hello World")');
        expect(props.updateSnippet).toHaveBeenCalled();
      });
      it('should NOT call updateSnippet with invalid origin prop', function () {
        var cm = node.find('.CodeEditor').first();
        var cmProps = cm.props();
        cmProps.onChange(cm, {
          origin: 'invalid'
        }, 'console.log("Hello World")');
        expect(props.updateSnippet).toHaveBeenCalledTimes(0);
      });
      it('should called updateSnippet and updateStep when a tab is updated', function () {
        var select = node.find('.TutorialEditor-Controls').find('.TutorialEditor-Controls-ControlWrapper-Select');
        var value = JSON.stringify({
          language: 'javascript',
          highlightedSyntax: 'javascript'
        });
        var event = {
          target: {
            value: value
          }
        };
        select.simulate('change', event);
        expect(props.updateSnippet).toHaveBeenCalled();
      });
    });
    describe('updateStep', function () {
      it('should call updateStep when line numbers are changed', function () {
        var lineInput = node.find('.TutorialEditor-Controls').find('.TutorialEditor-Controls-ControlWrapper').find('.TutorialEditor-Controls-ControlWrapper-Input').first();
        lineInput.simulate('change');
        expect(props.updateStep).toHaveBeenCalled();
      });
      it('should NOT call updateStep with invalid line numbers input', function () {
        var lineInput = node.find('.TutorialEditor-Controls').find('.TutorialEditor-Controls-ControlWrapper').find('.TutorialEditor-Controls-ControlWrapper-Input').first();
        var event = {
          target: {
            value: 'ajsdnaksfn'
          }
        };
        lineInput.simulate('change', event);
        expect(props.updateStep).toHaveBeenCalledTimes(0);
      });
      it('should call updateStep when a tab is deleted', function () {
        var deleteButton = node.find('.TutorialEditor-Controls').find('.TutorialEditor-Controls-Delete');
        deleteButton.simulate('click');
        expect(props.updateStep).toHaveBeenCalled();
        expect(props.updateSnippet).toHaveBeenCalled();
      });
    });
    it('should call handleSnippetSelection when a snippet is selected', function () {
      var snipSelect = node.find('.TutorialEditor-Nav-Menu').find(_components.Menu).find('.Menu-Item_link').at(0);
      snipSelect.simulate('click');
      expect(props.handleSnippetSelection).toHaveBeenCalled();
    });
    it('should set the index to the selected tab', function () {
      var selectTab = node.find('.TutorialEditor-Nav-Tab').find(_components.Button).at(1);
      selectTab.simulate('click');
      expect(props.setSelectedTab).toHaveBeenCalledWith(1);
    });
    describe('codeEditor', function () {
      var node2;
      beforeEach(function () {
        props.snippet.codeOptions[1].code = 'hello world;';
        node2 = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_2.PermissionContext.Provider, {
          value: true
        }, /*#__PURE__*/_react["default"].createElement(_["default"], props)));
      });
      it('should apply readonly syntaxHighlighter text', function () {
        var cm = node2.find('.CodeEditor').first();
        var row = cm.find('div.cm-linerow');
        var span1 = row.find('span').at(1);
        var span2 = row.find('span').at(2);
        expect(row).toHaveLength(1);
        expect(span1.text()).toStrictEqual('hello');
        expect(span2.text()).toStrictEqual('world');
      });
    });
  });
});