"use strict";

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

var _immutable = require("immutable");

var _Show = _interopRequireDefault(require("quiz-interactions/lib/components/multiple_choice/Show"));

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

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

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

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

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

var _Bank = _interopRequireDefault(require("../../../../../../common/records/Bank"));

var _ItemEdit = _interopRequireDefault(require("../../../../../../common/components/resources/item/ItemEdit"));

var _ItemShow = _interopRequireDefault(require("../../../../../../common/components/resources/item/ItemShow"));

var _ItemOverride = _interopRequireDefault(require("../../../../../../common/components/resources/item/ItemOverride"));

var _Stimulus = _interopRequireDefault(require("../../../../../../common/records/Stimulus"));

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

var _index = _interopRequireDefault(require("../index"));

var _QuizEntryShow = _interopRequireDefault(require("../../QuizEntryShow"));

var _QuizEntryEdit = _interopRequireDefault(require("../../QuizEntryEdit"));

var _StimulusEdit = _interopRequireDefault(require("../../../../../../common/components/resources/stimulus/StimulusEdit"));

var _StimulusShow = _interopRequireDefault(require("../../../../../../common/components/resources/stimulus/StimulusShow"));

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

var _QuizEntry2 = require("../../../../../../../tests/factories/QuizEntry");

var _WarningWrapper = _interopRequireDefault(require("../../../../../../common/components/resources/WarningWrapper"));

var overlayStyles = {
  componentId: 'cqGrr',
  template: function template(theme) {
    return "\n\n.overlay__styles__root {\n  position: relative;\n  align-self: center;\n}\n\n.overlay__styles__overlay {\n  position: absolute;\n  top: 0;\n  left: 0;\n  cursor: pointer;\n  z-index: ".concat(theme.overlayZIndex || 'inherit', ";\n\n  height: 100%;\n  width: 100%;\n}\n\n.overlay__styles__overlay:hover {\n    box-sizing: border-box;\n    border-left: ").concat(theme.overlayBarSize || 'inherit', " solid ").concat(theme.overlayBarColor || 'inherit', ";\n  }\n");
  },
  'root': 'overlay__styles__root',
  'overlay': 'overlay__styles__overlay'
};
describe('QuizEntry Building', function () {
  var sandbox = sinon.createSandbox();
  var QuizEntryWithContext = withContext(wrapInDragAndDropTestContext(_presenter.default));
  var fakeItem = new _Item.default({
    title: 'Super Neat Item'
  });

  fakeItem.getInteractionType = function () {
    return new _InteractionType.default({
      slug: 'choice'
    });
  };

  var changeQuizEntryPointsStub = sandbox.stub();
  var openModalStub = sandbox.stub();
  var screenreaderNotificationStub = sandbox.stub();
  var showErrorStub = sandbox.stub();
  var itemQuizEntry = new _QuizEntry.default({
    id: 3
  });
  sinon.stub(itemQuizEntry, 'getEntry').returns(fakeItem);
  var propsForQuizEntryStub = sandbox.stub().returns({
    quizEntry: itemQuizEntry,
    quizEntries: (0, _immutable.List)([])
  });
  var props = {
    connectDropTarget: sinon.stub().returnsArg(0),
    changeItemState: function changeItemState() {},
    clearModifications: function clearModifications() {},
    changeQuizEntryPoints: changeQuizEntryPointsStub,
    editable: true,
    errorsAreShowing: false,
    hideError: function hideError() {},
    interactionType: new _InteractionType.default({
      name: 'foo'
    }),
    isEditing: false,
    isValid: true,
    entry: fakeItem,
    workingEntry: fakeItem,
    openModal: openModalStub,
    propsForQuizEntry: propsForQuizEntryStub,
    quizEntry: new _QuizEntry.default({
      entryType: 'Item',
      entry: fakeItem
    }),
    quizEntries: (0, _immutable.List)([]),
    removeItem: function removeItem() {},
    screenreaderNotification: screenreaderNotificationStub,
    scrollKey: 'scrollKey',
    setUi: function setUi() {},
    showError: showErrorStub,
    switchOnEditing: function switchOnEditing() {},
    switchOffEditing: function switchOffEditing() {},
    onEditQuizEntryToggle: function onEditQuizEntryToggle() {},
    editQuizEntryToggle: false
  };
  var testbed = createTestbed(QuizEntryWithContext, props);
  afterEach(function () {
    sandbox.resetHistory();
  });
  describe('is editing', function () {
    it('is does not show an overlay when mouse is over', function () {
      testbed.render({
        isEditing: true
      });
      var overlayElement = testbed.findChildrenByClass(overlayStyles.overlay)[0];
      expect(overlayElement).to.be.undefined();
    });
    it('passes the right props to the interaction component', function () {
      testbed.render({
        isEditing: true
      });
      var editElement = testbed.findChildByType(_Edit.default);
      expect(Object.keys(editElement.props)).to.include('itemBody');
      expect(Object.keys(editElement.props)).to.include('changeItemState');
    });
    it('renders a quizEntryEdit child', function () {
      testbed.render({
        isEditing: true
      });
      var editElement = testbed.findChildByType(_QuizEntryEdit.default);
      expect(editElement).to.not.be.undefined();
    });
    it('renders a quizEntryEdit child when item is temporary', function () {
      testbed.render({
        isEditing: false,
        entry: fakeItem.set('id', _constants.TEMPORARY_ITEM_ID)
      });
      var editElement = testbed.findChildByType(_QuizEntryEdit.default);
      expect(editElement).to.not.be.undefined();
    });
    it('calls points modifications on entry points change', function () {
      testbed.render({
        isEditing: true
      });
      var component = testbed.findChildrenByType(_ItemEdit.default)[0];
      component.props.handleQuizEntryPointsChange(1);
      expect(changeQuizEntryPointsStub.getCall(0).args[0]).to.eq('0');
      expect(changeQuizEntryPointsStub.getCall(0).args[1]).to.eq(1);
    });
    it('renders an immutability warning if immutable', function () {
      var item = new _Item.default({
        status: 'immutable'
      });
      var qe = new _QuizEntry.default({
        status: 'immutable',
        entryType: 'Item'
      });
      sinon.stub(qe, 'getEntry').returns(item);
      testbed.render({
        isEditing: true,
        quizEntry: qe,
        entry: item
      });
      var warning = testbed.findChildByType(_Alert.default);
      expect(warning.props.variant).to.equal('warning');
    });
    it('renders a deleted bankEntry warning if an archived bankEntry', function () {
      var _QuizEntryBankEntry = (0, _QuizEntry2.QuizEntryBankEntry)({
        bankEntryAttrs: {
          archived: true
        }
      }),
          entry = _QuizEntryBankEntry.entry,
          quizEntry = _QuizEntryBankEntry.quizEntry;

      testbed.render({
        isEditing: true,
        quizEntry: quizEntry,
        entry: entry
      });
      var warning = testbed.findChildByType(_Alert.default);
      expect(warning.props.variant).to.equal('info');
    });
    describe('stimuli', function () {
      it('renders a stimulus quizEntry without items', function () {
        var stimulus = new _Stimulus.default();
        testbed.render({
          quizEntry: new _QuizEntry.default({
            entryType: 'Stimulus',
            entry: stimulus
          })
        });
        var element = testbed.findChildByType(_StimulusShow.default);
        expect(element).to.not.be.undefined();
        var quizEntryElements = testbed.findChildrenByType(_presenter.default);
        expect(quizEntryElements.length).to.eq(1);
      });
      it('renders a stimulus quizEntry with items', function () {
        var stimulus = new _Stimulus.default();
        var childEntry = new _QuizEntry.default({
          id: 1,
          entryType: 'Item',
          entry: new _Item.default()
        });
        sinon.stub(childEntry, 'getEntry').returns(new _Item.default());
        var childEntries = (0, _immutable.List)([childEntry]);
        var quizEntry = new _QuizEntry.default({
          id: 2,
          entryType: 'Stimulus',
          entry: stimulus
        });
        sinon.stub(quizEntry, 'getNestedEntries').returns(childEntries);
        sinon.stub(quizEntry, 'getEntry').returns(stimulus);
        testbed.render({
          quizEntry: quizEntry,
          childEntries: childEntries,
          entry: stimulus
        });
        var element = testbed.findChildByType(_StimulusShow.default);
        expect(element).to.not.be.undefined();
        var quizEntryElements = testbed.findChildrenByType(_presenter.default);
        expect(quizEntryElements.length).to.eq(2); // Rendered by stimulus

        var connectedQuizEntryElements = testbed.findChildrenByType(_index.default);
        expect(connectedQuizEntryElements.length).to.eq(1);
      });
      it('renders an editing stimulus quizEntry', function () {
        var stimulus = new _Stimulus.default();
        testbed.render({
          isEditing: true,
          quizEntry: new _QuizEntry.default({
            entryType: 'Stimulus',
            entry: stimulus
          }),
          entry: stimulus
        });
        var element = testbed.findChildByType(_StimulusEdit.default);
        expect(element).to.not.be.undefined();
      });
      it('renders a deleted bankEntry warning if an archived bankEntry', function () {
        var _QuizEntryBankEntry2 = (0, _QuizEntry2.QuizEntryBankEntry)({
          bankEntryAttrs: {
            archived: true
          },
          entryType: 'Stimulus'
        }),
            entry = _QuizEntryBankEntry2.entry,
            quizEntry = _QuizEntryBankEntry2.quizEntry;

        testbed.render({
          isEditing: true,
          quizEntry: quizEntry,
          entry: entry
        });
        var warning = testbed.findChildByType(_Alert.default);
        expect(warning.props.variant).to.equal('info');
      });
    });
    describe('renders bank', function () {
      it('renders a quizEntryShow child', function () {
        var bank = new _Bank.default({
          title: 'Astronomy Questions'
        });
        testbed.render({
          entry: bank,
          quizEntry: new _QuizEntry.default({
            entryType: 'Bank',
            entry: bank
          })
        });
        var editElement = testbed.findChildByType(_QuizEntryShow.default);
        expect(editElement.props.name).to.eq('Astronomy Questions');
      });
    });
    describe('renders bank entry', function () {
      it('renders a quizEntryShow child', function () {
        var _QuizEntryBankEntry3 = (0, _QuizEntry2.QuizEntryBankEntry)(),
            entry = _QuizEntryBankEntry3.entry,
            quizEntry = _QuizEntryBankEntry3.quizEntry;

        testbed.render({
          quizEntry: quizEntry,
          entry: entry
        });
        var editElement = testbed.findChildByType(_QuizEntryShow.default);
        expect(editElement).to.not.be.undefined();
      });
      it('renders a QuizEntryEdit child', function () {
        var _QuizEntryBankEntry4 = (0, _QuizEntry2.QuizEntryBankEntry)(),
            entry = _QuizEntryBankEntry4.entry,
            quizEntry = _QuizEntryBankEntry4.quizEntry;

        testbed.render({
          quizEntry: quizEntry,
          entry: entry,
          isEditing: true
        });
        var editElement = testbed.findChildByType(_QuizEntryEdit.default);
        expect(editElement).to.not.be.undefined();
        var alert = testbed.findChildByType(_Alert.default);
        expect(alert).to.not.be.undefined();
        expect(alert.props.variant).to.equal('info');
      });
      it('renders an unauthorized QuizEntryEdit child', function () {
        var _QuizEntryBankEntry5 = (0, _QuizEntry2.QuizEntryBankEntry)({
          quizEntryAttrs: {
            entryEditable: false
          }
        }),
            entry = _QuizEntryBankEntry5.entry,
            quizEntry = _QuizEntryBankEntry5.quizEntry;

        testbed.render({
          quizEntry: quizEntry,
          entry: entry,
          isEditing: true
        });
        var editElement = testbed.findChildByType(_QuizEntryEdit.default);
        expect(editElement).to.not.be.undefined();
        var alert = testbed.findChildByType(_Alert.default);
        expect(alert).to.not.be.undefined();
        expect(alert.props.variant).to.equal('warning');
      });
      it('merges quiz entry overrides into item', function () {
        var _QuizEntryBankEntry6 = (0, _QuizEntry2.QuizEntryBankEntry)({
          itemAttrs: {
            properties: (0, _immutable.fromJS)({
              shuffleRules: {
                choices: {
                  shuffled: true,
                  toLock: [0, 3]
                }
              }
            })
          },
          quizEntryAttrs: {
            properties: (0, _immutable.fromJS)({
              itemOverrides: {
                properties: {
                  shuffleRules: {
                    choices: {
                      toLock: [3]
                    }
                  }
                }
              }
            })
          }
        }),
            entry = _QuizEntryBankEntry6.entry,
            quizEntry = _QuizEntryBankEntry6.quizEntry;

        testbed.render({
          quizEntry: quizEntry,
          entry: entry
        });
        var itemShow = testbed.findChildByType(_ItemShow.default);
        var shuffleRules = itemShow.props.entry.properties.getIn(['shuffleRules', 'choices']).toJS();
        expect(shuffleRules.shuffled).to.be.true();
        expect(shuffleRules.toLock).to.eql([3]);
      });
      it('renders ItemOverride for overridable types', function () {
        var _QuizEntryBankEntry7 = (0, _QuizEntry2.QuizEntryBankEntry)({
          itemAttrs: {
            title: 'T/F Item'
          }
        }),
            quizEntry = _QuizEntryBankEntry7.quizEntry;

        testbed.render({
          isEditing: true,
          quizEntry: quizEntry
        });
        var itemComponents = testbed.findChildrenByType(_ItemOverride.default);
        expect(itemComponents).to.have.length(1);
      });
      it('renders ItemShow for non-overridable types', function () {
        var _QuizEntryBankEntry8 = (0, _QuizEntry2.QuizEntryBankEntry)({
          itemAttrs: {
            title: 'T/F Item'
          }
        }),
            entry = _QuizEntryBankEntry8.entry,
            quizEntry = _QuizEntryBankEntry8.quizEntry;

        entry.getInteractionType.restore();
        sinon.stub(entry, 'getInteractionType').returns(new _InteractionType.default({
          slug: 'true-false'
        }));
        testbed.render({
          entry: entry,
          isEditing: true,
          quizEntry: quizEntry
        });
        var itemComponents = testbed.findChildrenByType(_ItemShow.default);
        expect(itemComponents).to.have.length(1);
      });
    });
  });
  describe('with item banking enabled', function () {
    it('includes Item Banking as additionalOptions', function () {
      testbed.render({
        isEditing: true
      });
      var component = testbed.findChildByType(_ItemEdit.default);
      expect(component.props.additionalOptions).to.have.length(1);
      expect(component.props.additionalOptions.map(function (opt) {
        return opt.key;
      })).to.include('banks');
    });
  });
  describe('is showing', function () {
    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('is shows an overlay when mouse is over', function () {
      testbed.render();
      var overlayElement = testbed.findChildrenByClass(overlayStyles.overlay)[0];
      expect(overlayElement).to.not.be.undefined();
    });
    it('passes itemBody to the interaction component', function () {
      testbed.render();
      var showElement = testbed.findChildByType(_Show.default);
      expect(Object.keys(showElement.props)).to.include('itemBody');
    });
    it('renders a QuizEntryShow child', function () {
      testbed.render();
      var showElement = testbed.findChildByType(_QuizEntryShow.default);
      expect(showElement).to.not.be.undefined();
    });
    it('passes scrollKey to QuizEntryShow', function () {
      testbed.render();
      var showElement = testbed.findChildByType(_QuizEntryShow.default);
      expect(Object.keys(showElement.props)).to.include('scrollKey');
    });
    describe('renders bank entry', function () {
      it('renders a quizEntryShow child', function () {
        var _QuizEntryBankEntry9 = (0, _QuizEntry2.QuizEntryBankEntry)(),
            entry = _QuizEntryBankEntry9.entry,
            quizEntry = _QuizEntryBankEntry9.quizEntry;

        testbed.render({
          quizEntry: quizEntry,
          entry: entry
        });
        var editElement = testbed.findChildByType(_QuizEntryShow.default);
        expect(editElement).to.not.be.undefined();
      });
    });
    describe('renders bank entry with isEditing true', function () {
      it('renders a quizEntryEdit child and respond to defaultActionCallback', function () {
        var _QuizEntryBankEntry10 = (0, _QuizEntry2.QuizEntryBankEntry)(),
            entry = _QuizEntryBankEntry10.entry,
            quizEntry = _QuizEntryBankEntry10.quizEntry;

        var setUiStub = sandbox.stub();
        var newActiveBankEntryStub = sandbox.stub();
        var newActiveBankStub = sandbox.stub();
        var switchOffEditingStub = sandbox.stub();
        testbed.render({
          quizEntry: quizEntry,
          entry: entry,
          isEditing: true,
          setUi: setUiStub,
          newActiveBankEntry: newActiveBankEntryStub,
          newActiveBank: newActiveBankStub,
          switchOffEditing: switchOffEditingStub
        });
        var editElement = testbed.findChildByType(_QuizEntryEdit.default);
        expect(editElement).to.not.be.undefined();
        var warningWrapper = testbed.findChildByType(_WarningWrapper.default);
        expect(warningWrapper).to.not.be.undefined();
        warningWrapper.props.defaultActionCallback();
        expect(setUiStub).to.be.called();
        expect(newActiveBankEntryStub).to.be.called();
        expect(newActiveBankStub).to.be.called();
        expect(setUiStub).to.be.called();
      });
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});