"use strict";

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

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: 'cCwUt',
  template: function template(theme) {
    return "\n\n.Show__styles__instructionsShowWrapper {\n  position: relative;\n  width: 100%;\n  min-height: 4rem;\n\n  box-sizing: border-box;\n\n  padding: ".concat(theme.instructionsShowWrapperPadding || 'inherit', ";\n\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n}\n\n.Show__styles__instructionsShowText {\n  white-space: normal;\n  overflow: hidden;\n  font-size: ").concat(theme.instructionsShowTextFontSize || 'inherit', ";\n}\n");
  },
  'instructionsShowWrapper': 'Show__styles__instructionsShowWrapper',
  'instructionsShowText': 'Show__styles__instructionsShowText'
};
describe('Building QuizInstructions', function () {
  var quiz = new _Quiz.default({
    id: '3'
  });
  var scrollKey = 'scrollKey';
  var testbed = createTestbed(withContext(_presenter.default), {
    clearModifications: function clearModifications() {},
    persistModification: function persistModification() {},
    saveQuizChange: function saveQuizChange() {},
    screenreaderNotification: function screenreaderNotification() {},
    changeQuizInstructions: function changeQuizInstructions() {},
    isEditin: false,
    quiz: quiz,
    workingQuiz: (0, _immutable.Map)(),
    switchOffEditing: function switchOffEditing() {},
    switchOnEditing: function switchOnEditing() {},
    scrollKey: scrollKey
  });
  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 instructions edit if isEditing is true', function () {
    testbed.render({
      isEditing: true
    });
    expect(testbed.findChildrenByType(_Edit.default)).to.not.be.empty();
  });
  it('should render quiz instructions preview if isEditing is false', function () {
    testbed.render({
      isEditing: false
    });
    expect(testbed.findChildrenByClass(showStyles.instructionsShowText)).to.not.be.empty();
  });
  it('should call switchOffEditing and clearModifications on save', function () {
    var clearModificationsSpy = sinon.spy();
    var switchOffEditingSpy = sinon.spy();
    testbed.render({
      clearModifications: clearModificationsSpy,
      switchOffEditing: switchOffEditingSpy,
      isEditing: true
    });
    var instructionsEdit = testbed.findChildByType(_Edit.default);
    instructionsEdit.props.saveAndPreview();
    expect(clearModificationsSpy).to.have.been.calledOnce();
    expect(switchOffEditingSpy).to.have.been.calledOnce();
  });
  it('should not pass null instructions into the rce', function () {
    testbed.render({
      isEditing: true,
      quiz: new _Quiz.default({
        instructions: null,
        id: '3'
      })
    });
    var instructions = testbed.findChildByType(_presenter.default);
    expect(instructions.workingInstructions()).to.be.equal('');
  });
  it('should dispatch a change instructions action on the right quiz', function () {
    var changeInstructionsStub = sinon.stub();
    testbed.render({
      isEditing: true,
      quiz: new _Quiz.default({
        instructions: 'the original quiz desc',
        id: '3'
      }),
      changeQuizInstructions: changeInstructionsStub,
      saveQuizChange: function saveQuizChange() {}
    });
    var instructionsEdit = testbed.findChildByType(_Edit.default);
    instructionsEdit.props.onInputChange({}, 'some value here?');
    var stubArgs = changeInstructionsStub.getCall(0).args[0];
    expect(stubArgs[0]).to.be.equal('3');
  });
  it('should be able to persist modifications to instructions ', function () {
    var saveQuizChangeStub = sinon.stub();
    var moddedQuiz = new _Quiz.default({
      id: '3',
      instructions: 'the original quiz desc'
    });
    sinon.stub(moddedQuiz, 'getWorkingInstance').returns(moddedQuiz.merge({
      instructions: 'the working instructions'
    }));
    testbed.render({
      isEditing: true,
      quiz: moddedQuiz,
      clearModifications: function clearModifications() {},
      saveQuizChange: saveQuizChangeStub,
      changeQuizInstructions: function changeQuizInstructions() {}
    }); // easiest way to get saveAndPreview

    var instructionsEdit = testbed.findChildByType(_Edit.default);
    instructionsEdit.props.saveAndPreview();
    var stubArgs = saveQuizChangeStub.getCall(0).args;
    expect(stubArgs[0].get('instructions')).to.be.equal('the working instructions');
  });
  it('passes scrollKey to EditButton', function () {
    testbed.render();
    var editButton = testbed.findChildByType(_EditButton.default);
    expect(editButton.props.scrollKey).to.equal(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: 'Instructions switched to edit mode. Navigate down to access text editor.',
    turnedOff: 'Instructions edit mode off.'
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});