"use strict";

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

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

var _immutable = require("immutable");

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

var _Item = _interopRequireDefault(require("../../../../../../../common/records/Item"));

var _QuizEntry = _interopRequireDefault(require("../../../../../../../common/records/QuizEntry"));

var _constants = require("@instructure/quiz-common/lib/constants");

var styles = {
  componentId: 'fFUmw',
  template: function template(theme) {
    return "\n\n.Footer__styles__footer {\n  background-color: ".concat(theme.footerBackgroundColor || 'inherit', ";\n  color: ").concat(theme.footerColor || 'inherit', ";\n\n  padding: ").concat(theme.footerPadding || 'inherit', ";\n\n  display: flex;\n  justify-content: space-between;\n  vertical-align: middle;\n  align-items: center;\n}\n\n.Footer__styles__leftFooter {\n  flex: 8 0 0;\n  display: inline-flex;\n  flex-wrap: nowrap;\n  align-items: center;\n}\n\n.Footer__styles__rightFooter {\n  flex: 0 0 auto;\n  display: inline-flex;\n  text-align: right;\n}\n\n[dir=\"ltr\"] .Footer__styles__rightFooter {\n  text-align: right;\n}\n\n[dir=\"rtl\"] .Footer__styles__rightFooter {\n  text-align: right;\n}\n\n.Footer__styles__pointsSection,\n.Footer__styles__feedbackSection {\n  display: flex;\n}\n\n.Footer__styles__feedbackSection {\n  margin-left: ").concat(theme.feedbackSectionLeftMargin || 'inherit', ";\n}\n\n.Footer__styles__pointsInput {\n  width: 6rem;\n  font-size: ").concat(theme.pointsInputFontSize || 'inherit', ";\n  display: inline-flex;\n}\n\n.Footer__styles__pointsText {\n  display: inline-flex;\n  font-size: ").concat(theme.pointsTextFontSize || 'inherit', ";\n  align-self: center;\n  vertical-align: middle;\n  margin-left: ").concat(theme.pointsTextLeftMargin || 'inherit', ";\n}\n");
  },
  'footer': 'Footer__styles__footer',
  'leftFooter': 'Footer__styles__leftFooter',
  'rightFooter': 'Footer__styles__rightFooter',
  'pointsSection': 'Footer__styles__pointsSection',
  'feedbackSection': 'Footer__styles__feedbackSection',
  'pointsInput': 'Footer__styles__pointsInput',
  'pointsText': 'Footer__styles__pointsText'
};
describe('QuizEntryEditFooter', function () {
  var sandbox = sinon.createSandbox();
  var alternativeRightFooterStub = sandbox.stub();
  var changeQuizEntryPointsStub = sandbox.stub();
  var clearChangesStub = sandbox.stub();
  var clearModificationsStub = sandbox.stub();
  var openModalStub = sandbox.stub();
  var setUiStub = sandbox.stub();
  var successfulSubmitStub = sandbox.stub();
  var workingQuizEntry = new _QuizEntry.default({
    id: '55',
    pointsPossible: 3999
  });

  workingQuizEntry.isValid = function () {
    return true;
  };

  var quizEntry = new _QuizEntry.default({
    id: '55',
    entryType: 'Item',
    entryId: 66,
    pointsPossible: 12,
    position: 3
  });
  var workingItem = new _Item.default({
    id: '1',
    properties: {
      varyPointsByAnswer: false
    }
  });

  workingItem.isValid = function () {
    return true;
  };

  workingItem.getErrors = function () {
    return (0, _immutable.fromJS)({});
  };

  workingItem.errorsFor = function () {
    return (0, _immutable.fromJS)({});
  };

  var item = new _Item.default({
    id: '66'
  });
  var defaultProps = {
    changeQuizEntryPoints: changeQuizEntryPointsStub,
    clearChanges: clearChangesStub,
    clearModifications: clearModificationsStub,
    displayPosition: 1,
    entry: item,
    guid: 'guid123',
    isValid: true,
    openModal: openModalStub,
    quizEntry: quizEntry,
    quizId: '99',
    setUi: setUiStub,
    successfulSubmit: successfulSubmitStub,
    workingEntry: workingItem,
    workingQuizEntry: workingQuizEntry
  };
  var testbed = createTestbed(withContext(_presenter.default), defaultProps);
  beforeEach(function () {
    sandbox.resetHistory();
  });
  shouldRenderComponent(testbed, {}, _presenter.default);
  describe('item feedback', function () {
    it('calls open item feedback modal', function () {
      var item2 = new _Item.default((0, _immutable.fromJS)({
        feedback: {
          neutral: 'neutral'
        }
      }));
      testbed.render({
        entry: item2
      });
      var feedbackButton = testbed.findChildrenByClass(styles.feedbackSection)[0].childNodes[0];

      _testUtils.default.Simulate.click(feedbackButton);

      expect(setUiStub.called).to.be.true();
      expect(openModalStub).to.be.calledOnce.calledWith(_constants.ITEM_FEEDBACK_MODAL, sinon.match.has('modalTrigger'));
    });
  });
  context('points section', function () {
    it('displays the points possible', function () {
      testbed.render();
      expect(testbed.rootNode.innerHTML.match(/3,999/)).to.exist();
    });
    it('is disabled when varyPointsByAnswer is true', function () {
      var workingItem2 = new _Item.default({
        id: '1',
        properties: {
          varyPointsByAnswer: true
        },
        scoringData: {
          values: [{
            value: 'someId',
            points: 3
          }, {
            value: 'otherId',
            points: 5
          }]
        }
      });
      testbed.render({
        workingEntry: workingItem2
      });
      var input = testbed.findChildByTagName('input');
      expect(input.disabled).to.be.true();
    });
    it('is not disabled when varyPointsByAnswer is false', function () {
      testbed.render();
      var input = testbed.findChildByTagName('input');
      expect(input.disabled).to.be.false();
    });
    it('renders regradeAdjustedPointsPossible when present', function () {
      var regradedQuizEntry = new _QuizEntry.default({
        id: '55',
        entryType: 'Item',
        entryId: 66,
        pointsPossible: 12,
        position: 3,
        regradeAdjustedPointsPossible: 15
      });
      testbed.render({
        workingQuizEntry: regradedQuizEntry
      });
      var input = testbed.findChildrenByTagName('input')[0];
      expect(input.value).to.equal('15');
    });
    it('calls changeQuizEntryPoints on change', function () {
      testbed.render();
      var input = testbed.findChildrenByTagName('input')[0];
      testbed.change(input, '456');
      expect(changeQuizEntryPointsStub).to.have.been.calledWith(quizEntry.id, 456);
    });
    it('resets to zero on blur with invalid input', function () {
      testbed.render();
      var input = testbed.findChildrenByTagName('input')[0];
      testbed.change(input, 'thisisnotanumber');

      _testUtils.default.Simulate.blur(input);

      expect(input.value).to.equal('0');
    });
    it('does not reset to zero on blur valid input', function () {
      testbed.render();
      var input = testbed.findChildrenByTagName('input')[0];

      _testUtils.default.Simulate.blur(input);

      expect(input.value).to.equal('3,999');
    });
  });
  describe('alternativeRightFooter', function () {
    it('does not render save/cancel buttons', function () {
      testbed.render({
        alternativeRightFooter: alternativeRightFooterStub
      });
      var buttonTexts = testbed.findChildrenByTagName('button').map(function (b) {
        return b.innerText;
      });
      expect(buttonTexts).to.not.include('Cancel');
      expect(buttonTexts).to.not.include('Done');
    });
    it('calls the alternativeRightFooter', function () {
      testbed.render({
        alternativeRightFooter: alternativeRightFooterStub
      });
      expect(alternativeRightFooterStub).to.have.been.calledOnce();
    });
    it('renders the content returned by alternativeRightFooter', function () {
      testbed.render({
        alternativeRightFooter: function alternativeRightFooter() {
          return 'alternativeRightFooterContent';
        }
      });
      expect(testbed.getText()).to.contain('alternativeRightFooterContent');
    });
  });
  context('cancelling', function () {
    it('clears modifications', function () {
      testbed.render();
      var buttons = testbed.findChildrenByTagName('button');
      var cancelButton = buttons.find(function (b) {
        return b.innerText === 'Cancel';
      });

      _testUtils.default.Simulate.click(cancelButton);

      expect(clearChangesStub).to.be.calledOnce();
    });
  });
  context('submitting', function () {
    it('updates existing quiz entry and item', function () {
      testbed.render();
      var buttons = testbed.findChildrenByTagName('button');
      var doneButton = buttons.find(function (b) {
        return b.innerText === 'Done';
      });

      _testUtils.default.Simulate.click(doneButton);

      expect(successfulSubmitStub).to.be.calledOnce();
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done, {
        ignores: ['label', 'label-title-only']
      });
    });
  });
});