"use strict";

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

var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));

var _immutable = require("immutable");

var _Errors = _interopRequireDefault(require("../../../../../../../common/components/shared/errors/Errors"));

var _index = _interopRequireDefault(require("../index"));

var _editableFieldTests = _interopRequireDefault(require("../../../__tests__/editableFieldTests"));

var styles = {
  componentId: 'fDDFj',
  template: function template(theme) {
    return "\n\n.fDDFj_eqoe{border-bottom:solid;border-color:".concat(theme.titleEditWrapperBorderColor || 'inherit', ";border-width:").concat(theme.titleEditWrapperBorderWidth || 'inherit', ";box-sizing:border-box;display:flex;flex-direction:column;height:5rem;justify-content:center;padding-left:").concat(theme.titleEditWrapperLeftPadding || 'inherit', ";position:relative;width:100%}\n\n.fDDFj_dwFS{font-size:").concat(theme.titleEditInputFontSize || 'inherit', ";width:80%}\n\n.fDDFj_dRxD,.fDDFj_dvSU{display:flex;flex:1 1 auto;flex-direction:column;justify-content:center;width:100%}\n\n.fDDFj_xXob{border:solid 1px ").concat(theme.errorBorderColor || 'inherit', ";border-radius:").concat(theme.errorBorderBorderRadius || 'inherit', ";margin:").concat(theme.errorBorderMargin || 'inherit', ";padding:").concat(theme.errorBorderPadding || 'inherit', "}");
  },
  'titleEditWrapper': 'fDDFj_eqoe',
  'titleEditInput': 'fDDFj_dwFS',
  'errorsWrapper': 'fDDFj_dRxD',
  'inputWrapper': 'fDDFj_dvSU',
  'errorBorder': 'fDDFj_xXob'
};
describe('Quiz Title Edit Presenter', function () {
  var styleClasses = {
    edit_input: styles.titleEditInput
  };
  var QuizTitleEditWithContext = withContext(_index.default); // shared tests with QuizInstructionsEdit

  (0, _editableFieldTests.default)(QuizTitleEditWithContext);
  var testbed = createTestbed(QuizTitleEditWithContext, {
    hideError: function hideError() {},
    isValid: true,
    saveAndPreview: function saveAndPreview() {},
    showError: function showError() {}
  });
  it('should show Errors if errorsAreShowing is true and chocies have errors', function () {
    testbed.render({
      errorsAreShowing: true,
      errors: (0, _immutable.List)('something is wrong')
    });
    var errorBorder = testbed.findChildrenByClass(styles.errorBorder)[0];
    expect(errorBorder).to.not.be.undefined();
    var errorList = testbed.findChildrenByType(_Errors.default)[0];
    expect(errorList).to.not.be.undefined();
  });
  it('should call saveAndPreview when blurred', function () {
    // make test synchronous
    var fakeDefer = function fakeDefer(fn) {
      document.activeElement.blur();
      fn();
    };

    var saveAndPreviewSpy = sinon.spy();
    testbed.render({
      deferMock: fakeDefer,
      saveAndPreview: saveAndPreviewSpy
    });
    document.activeElement.blur();
    expect(saveAndPreviewSpy).to.have.been.calledOnce();
  });
  it('should call showError on blur when invalid', function () {
    // make test synchronous
    var fakeDefer = function fakeDefer(fn) {
      document.activeElement.blur();
      fn();
    };

    var showErrorStub = sinon.stub();
    testbed.render({
      isValid: false,
      deferMock: fakeDefer,
      showError: showErrorStub
    });
    document.activeElement.blur();
    expect(showErrorStub).to.have.been.calledOnce();
  });
  it('editing the input calls the onInputChange prop', function () {
    var onChangeSpy = sinon.spy();
    testbed.render({
      onInputChange: onChangeSpy
    });
    var titleInput = testbed.findChildrenByClass(styleClasses.edit_input)[0];

    _testUtils.default.Simulate.change(titleInput);

    expect(onChangeSpy.called).to.be.true();
  });
  it('keying down on the input calls the onInputKeyDown prop', function () {
    var onKeyDownSpy = sinon.spy();
    testbed.render({
      onInputKeyDown: onKeyDownSpy
    });
    var titleInput = testbed.findChildrenByClass(styleClasses.edit_input)[0];

    _testUtils.default.Simulate.keyDown(titleInput);

    expect(onKeyDownSpy.called).to.be.true();
  });
});