"use strict";

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

var _immutable = require("immutable");

var _InteractionType = _interopRequireDefault(require("quiz-interactions/lib/records/InteractionType"));

var _Edit = _interopRequireDefault(require("quiz-interactions/lib/components/multiple_choice/Edit"));

var _QuestionContainer = _interopRequireDefault(require("quiz-interactions/lib/components/common/edit/components/QuestionContainer"));

var _OutcomeCount = _interopRequireDefault(require("outcomes-react/lib/containers/OutcomeCount"));

var _Alert = _interopRequireDefault(require("@instructure/ui-alerts/lib/components/Alert"));

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

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

describe('Item Edit', function () {
  var ItemWithContext = withContext(_presenter.default);
  var type = new _InteractionType.default({
    name: 'Multiple Choice',
    slug: 'choice'
  });
  var item = new _Item.default({
    id: '666',
    title: 'Tough Color Question',
    itemBody: 'Which color is happiest?',
    scoringData: {
      value: 'id'
    },
    calculatorType: 'basic'
  });
  sinon.stub(item, 'getInteractionType').returns(type);
  var setShouldClone = sinon.stub();
  var changeItemStateStub = sinon.stub();
  var shouldClone = false;
  var testbed = createTestbed(ItemWithContext, {
    entry: item,
    workingItem: item,
    setShouldClone: setShouldClone,
    shouldClone: shouldClone,
    changeItemState: changeItemStateStub
  });
  afterEach(function () {
    changeItemStateStub.resetHistory();
    setShouldClone.resetHistory();
  });
  describe('#render', function () {
    it('renders the item body', function () {
      testbed.render();
      var textInput = testbed.findChildrenByType(_QuestionContainer.default)[0];
      expect(textInput.props.itemBody).eq('Which color is happiest?');
    });
    it('renders the versioning alert when immutable', function () {
      var immutableItem = new _Item.default({
        id: '666',
        title: 'Tough Color Question',
        itemBody: 'Which color is happiest?',
        scoringData: {
          value: 'id'
        },
        status: 'immutable'
      });
      var shallowTestbed = testbed.renderShallowly({
        item: immutableItem
      });
      var alert = shallowTestbed.find(_Alert.default);
      expect(alert).to.not.be.undefined();
    });
    it('renders an error when there is a size error', function () {
      sinon.stub(item, 'getErrors').returns((0, _immutable.fromJS)({
        onSelf: ['some error']
      }));
      testbed.render({
        errorsAreShowing: true
      });
      expect(testbed.getText()).to.include('some error');
    });
    it('passes the correct hidden options value when calculator per question is enabled', function () {
      testbed.render({
        showCalculator: true
      });
      var component = testbed.findChildByType(_Edit.default);
      expect(component.props.hiddenOptions).to.eql({
        calculator: false
      });
    });
    it('passes the correct hidden options value when calculator per question is not enabled', function () {
      testbed.render({
        showCalculator: false
      });
      var component = testbed.findChildByType(_Edit.default);
      expect(component.props.hiddenOptions).to.eql({
        calculator: true
      });
    });
    it('passes the correct oneQuestionAtATime value to the interaction', function () {
      testbed.render({
        oneQuestionAtATime: true
      });
      var component = testbed.findChildByType(_Edit.default);
      expect(component.props.oneQuestionAtATime).to.be.true();
    });
  });
  describe('includes the correct additional options', function () {
    it('does not include additional options by default', function () {
      testbed.render();
      var component = testbed.findChildByType(_Edit.default);
      expect(component.props.additionalOptions).to.have.length(0);
    });
    describe('with outcomes enabled', function () {
      it('does not include OutcomeAlignments when not configured', function () {
        testbed.render({
          isEditing: true
        });
        var component = testbed.findChildByType(_Edit.default);
        expect(component.props.additionalOptions).to.have.length(0);
      });
      describe('with outcomes configured', function () {
        beforeEach(function () {
          testbed.render({
            contextUuid: 'alphabeta',
            outcomesEndpoint: 'http://foo',
            outcomesToken: 'tokentoken'
          });
        });
        it('includes OutcomeAlignments', function () {
          var component = testbed.findChildByType(_Edit.default);
          expect(component.props.additionalOptions).to.have.length(1);
          expect(component.props.additionalOptions.map(function (opt) {
            return opt.key;
          })).to.include('outcomes');
        });
        it('should include an outcome count object', function () {
          expect(testbed.findChildrenByType(_OutcomeCount.default)).not.to.have.length(0);
        });
        it('passes outcomes endpoint and token props', function () {
          var count = testbed.findChildrenByType(_OutcomeCount.default)[0];
          expect(count.props.host).to.equal('http://foo');
          expect(count.props.jwt).to.equal('tokentoken');
          expect(count.props.contextUuid).to.equal('alphabeta');
          var component = testbed.findChildByType(_Edit.default);
          var alignments = component.props.additionalOptions.map(function (opt) {
            return opt.component;
          })[0];
          expect(alignments.props.host).to.equal('http://foo');
          expect(alignments.props.jwt).to.equal('tokentoken');
          expect(alignments.props.contextUuid).to.equal('alphabeta');
        });
      });
    });
  });
  describe('outcome modal handlers', function () {
    var modalHandlerStub = sinon.stub();
    var changeOutcomeAlignmentSetGuid = sinon.stub();
    beforeEach(function () {
      testbed.render({
        isEditing: true,
        occludeQuizEntries: modalHandlerStub,
        outcomesEndpoint: 'http://foo',
        changeOutcomeAlignmentSetGuid: changeOutcomeAlignmentSetGuid
      });
    });
    it('calls onOutcomeModalOpen', function () {
      var component = testbed.findChildByType(_Edit.default);
      var alignments = component.props.additionalOptions[0].component;
      alignments.props.onModalOpen();
      expect(modalHandlerStub.called).to.be.true();
    });
    it('calls onOutcomeModalClose', function () {
      var component = testbed.findChildByType(_Edit.default);
      var alignments = component.props.additionalOptions[0].component;
      alignments.props.onModalClose();
      expect(modalHandlerStub.called).to.be.true();
    });
    it('calls onOutcomeAlignmentSetChange', function () {
      var component = testbed.findChildByType(_Edit.default);
      var alignments = component.props.additionalOptions[0].component;
      var outcomeAlignmentSet = {
        guid: 'abc'
      };
      alignments.props.onUpdate(outcomeAlignmentSet);
      expect(changeOutcomeAlignmentSetGuid.called).to.be.true();
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done, {
        ignores: ['aria-required-children', 'label', 'label-title-only']
      });
    });
  });
});