"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.Edit__styles__titleEditWrapper {\n  position: relative;\n  width: 100%;\n  height: 5rem;\n\n  border-bottom: solid;\n  border-width: ".concat(theme.titleEditWrapperBorderWidth || 'inherit', ";\n  border-color: ").concat(theme.titleEditWrapperBorderColor || 'inherit', ";\n  box-sizing: border-box;\n\n  padding-left: ").concat(theme.titleEditWrapperLeftPadding || 'inherit', ";\n\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n}\n\n.Edit__styles__titleEditInput {\n  font-size: ").concat(theme.titleEditInputFontSize || 'inherit', ";\n  width: 80%;\n}\n\n.Edit__styles__inputWrapper,\n.Edit__styles__errorsWrapper {\n  width: 100%;\n  flex: 1 1 auto;\n\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n}\n\n.Edit__styles__errorBorder {\n  margin: ").concat(theme.errorBorderMargin || 'inherit', ";\n  padding: ").concat(theme.errorBorderPadding || 'inherit', ";\n  border: solid 1px ").concat(theme.errorBorderColor || 'inherit', ";\n  border-radius: ").concat(theme.errorBorderBorderRadius || 'inherit', ";\n}\n");
  },
  'titleEditWrapper': 'Edit__styles__titleEditWrapper',
  'titleEditInput': 'Edit__styles__titleEditInput',
  'inputWrapper': 'Edit__styles__inputWrapper',
  'errorsWrapper': 'Edit__styles__errorsWrapper',
  'errorBorder': 'Edit__styles__errorBorder'
};
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();
  });
});