"use strict";

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

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

var _immutable = require("immutable");

var _toggleEditingTests = _interopRequireDefault(require("../../../__tests__/toggleEditingTests"));

var _presenter = _interopRequireDefault(require("../presenter"));

var _Edit = _interopRequireDefault(require("../../Edit"));

var _EditButton = _interopRequireDefault(require("../../../editButton/EditButton"));

var _Quiz = _interopRequireDefault(require("../../../../../../../common/records/Quiz"));

var showStyles = {
  componentId: 'bFiTq',
  template: function template(theme) {
    return "\n\n.Show__styles__titleShowWrapper {\n  position: relative;\n  width: 100%;\n  height: 5rem;\n\n  border-bottom: ".concat(theme.titleShowWrapperBottomBorder || 'inherit', ";\n  box-sizing: border-box;\n\n  padding-left: ").concat(theme.titleShowWrapperLeftPadding || 'inherit', ";\n\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n}\n\n.Show__styles__titleShowText {\n  font-size: ").concat(theme.titleShowTextFontSize || 'inherit', ";\n  overflow: hidden;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n}\n");
  },
  'titleShowWrapper': 'Show__styles__titleShowWrapper',
  'titleShowText': 'Show__styles__titleShowText'
};
describe('QuizTitle', function () {
  var quiz = new _Quiz.default({
    id: '1',
    title: 'title'
  });
  var props = {
    changeQuizTitle: Function.prototype,
    quiz: quiz,
    workingQuiz: quiz,
    errorsShowing: (0, _immutable.Map)(),
    hideError: Function.prototype,
    isEditing: false,
    onQuizUpdated: Function.prototype,
    saveQuizChange: Function.prototype,
    screenreaderNotification: Function.prototype,
    scrollKey: 'scrollKey',
    showError: Function.prototype,
    switchOffEditing: Function.prototype,
    switchOnEditing: Function.prototype
  };
  var testbed = createTestbed(withContext(_presenter.default), props);
  it('renders a non-navigable tab index for scroll focus', function () {
    testbed.render();
    var subject = testbed.findChildrenByTagNameAndAttribute('div', 'tabindex', '-1')[0];
    expect(subject).to.be.ok();
  });
  it('should render quiz title edit if isEditing is true', function () {
    testbed.render({
      isEditing: true
    });
    expect(testbed.findChildrenByType(_Edit.default)).to.not.be.empty();
  });
  it('should render quiz title preview if isEditing is false', function () {
    testbed.render({
      isEditing: false
    });
    expect(testbed.findChildrenByClass(showStyles.titleShowText)).to.not.be.empty();
  });
  it('handleInput should switchOffEditing if enter was hit', function () {
    var switchOffEditingStub = sinon.stub();
    testbed.render({
      isEditing: true,
      switchOffEditing: switchOffEditingStub
    });
    var titleInput = testbed.findChildByType(_Edit.default);

    _testUtils.default.Simulate.keyDown(titleInput.input, {
      key: 'q',
      keyCode: 81,
      which: 81
    });

    expect(switchOffEditingStub.called).to.be.false();

    _testUtils.default.Simulate.keyDown(titleInput.input, {
      key: 'Enter',
      keyCode: 13,
      which: 13
    });

    expect(switchOffEditingStub.calledOnce).to.be.true();
  });
  it('should call switchOffEditing prop on save', function () {
    var switchOffEditingSpy = sinon.spy();
    testbed.render({
      switchOffEditing: switchOffEditingSpy,
      isEditing: true
    });
    var titleEdit = testbed.findChildByType(_Edit.default);
    titleEdit.props.saveAndPreview();
    expect(switchOffEditingSpy).to.have.been.calledOnce();
  });
  it('should call onQuizUpdated prop on save', function () {
    var onQuizUpdatedSpy = sinon.spy();
    testbed.render({
      onQuizUpdated: onQuizUpdatedSpy,
      isEditing: true
    });
    var titleEdit = testbed.findChildByType(_Edit.default);
    titleEdit.props.saveAndPreview();
    expect(onQuizUpdatedSpy).to.have.been.calledWith({
      id: '1',
      title: 'title'
    });
  });
  it('should be able to persist modifications to title', function () {
    var saveQuizChangeStub = sinon.stub();
    var workingQuiz = {
      title: 'the working title',
      errorsFor: Function.prototype,
      isValid: function isValid() {
        return true;
      }
    };
    var quiz = new _Quiz.default({
      id: '1'
    });
    testbed.render({
      quiz: quiz,
      workingQuiz: workingQuiz,
      isEditing: true,
      saveQuizChange: saveQuizChangeStub
    }); // easiest way to get cancel

    var titleEdit = testbed.findChildByType(_Edit.default);
    titleEdit.props.saveAndPreview();
    var stubArgs = saveQuizChangeStub.getCall(0).args;
    expect(stubArgs[0].title).to.be.equal('the working title');
  });
  it('passes scrollKey to EditButton', function () {
    testbed.render();
    var editButton = testbed.findChildByType(_EditButton.default);
    expect(editButton.props.scrollKey).to.equal(props.scrollKey);
  });
  it('does not include edit button when editing', function () {
    testbed.render({
      isEditing: true
    });
    var editButtons = testbed.findChildrenByType(_EditButton.default);
    expect(editButtons).to.have.length(0);
  });
  (0, _toggleEditingTests.default)(testbed, {
    turnedOn: 'Title switched to edit mode.',
    turnedOff: 'Title edit mode off.'
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});