"use strict";

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

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

var _react = _interopRequireDefault(require("react"));

var _immutable = require("immutable");

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

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

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

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

var _StimulusShow = _interopRequireDefault(require("../../StimulusShow"));

var _StimulusEdit = _interopRequireDefault(require("../../StimulusEdit"));

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

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

var _Popover = _interopRequireDefault(require("../../../quiz/AddContent/Popover"));

var _QuizEntry2 = _interopRequireDefault(require("../../../../../../building/components/resources/quizEntry/QuizEntry"));

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

describe('Common Main Body Stimulus ', function () {
  var clearModificationsStub = sinon.stub();
  var openDeleteStimulusModalStub = sinon.stub();
  var setUiStub = sinon.stub();
  var switchOnEditingStub = sinon.stub();
  var switchOffEditingStub = sinon.stub();
  var quizEntry = new _QuizEntry.default({
    id: '33',
    entryType: 'Stimulus',
    entry: '55',
    position: 5
  });
  sinon.stub(quizEntry, 'getEntry').returns(new _Item.default());
  var propsForQuizEntryStub = sinon.stub().returns({
    quizEntries: (0, _immutable.List)(),
    quizEntry: quizEntry,
    entry: new _Item.default()
  });
  var stimulus = new _Stimulus.default({
    id: '55',
    title: 'Title',
    instructions: 'Instructions',
    body: 'Body',
    sourceUrl: 'Source URL'
  });
  var defaultProps = {
    childEntries: (0, _immutable.List)(),
    clearModifications: clearModificationsStub,
    deleteStimulusButton: _react.default.createElement("div", {
      className: "fakeDeleteButton"
    }),
    displayPosition: 3,
    propsForQuizEntry: propsForQuizEntryStub,
    openDeleteStimulusModal: openDeleteStimulusModalStub,
    quizEntry: quizEntry,
    quizEntries: (0, _immutable.List)(),
    quizId: '456',
    setUi: setUiStub,
    shouldClone: false,
    stimulus: stimulus,
    switchOffEditing: switchOffEditingStub,
    switchOnEditing: switchOnEditingStub,
    updateStimulus: function updateStimulus() {
      return new Promise(function () {});
    },
    guid: 'iamunique'
  };
  var testbed = createTestbed(wrapInDragAndDropTestContext(withContext(_presenter.default)), defaultProps);
  afterEach(function () {
    clearModificationsStub.resetHistory();
    openDeleteStimulusModalStub.resetHistory();
    propsForQuizEntryStub.resetHistory();
    setUiStub.resetHistory();
    switchOnEditingStub.resetHistory();
    switchOffEditingStub.resetHistory();
  });
  context('with children', function () {
    var qe1 = new _QuizEntry.default({
      id: '1',
      position: 6,
      entryType: 'Item'
    });
    var qe2 = new _QuizEntry.default({
      id: '2',
      position: 7,
      entryType: 'Item'
    });
    var bankQuizEntry = new _QuizEntry.default({
      entryType: 'Bank',
      properties: (0, _immutable.Map)({
        sampleNum: 3
      })
    });
    var quizEntries = [qe1, qe2, bankQuizEntry];
    quizEntries.forEach(function (qe) {
      sinon.stub(qe, 'getEntry').returns(new _Item.default());
    });

    var fakePropsForQuizEntry = function fakePropsForQuizEntry(quizEntry, displayPosition) {
      return {
        quizEntries: (0, _immutable.List)(),
        quizEntry: quizEntry
      };
    };

    var render = function render() {
      var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      testbed.render((0, _objectSpread2.default)({
        childEntries: (0, _immutable.List)([qe1, qe2]),
        propsForQuizEntry: fakePropsForQuizEntry
      }, props));
    };

    it('renders child entries', function () {
      render();
      var childEntries = testbed.findChildrenByType(_QuizEntry2.default);
      expect(childEntries.length).to.equal(2);
    });
    it('renders with the correct display positions', function () {
      testbed.render({
        childEntries: (0, _immutable.List)([bankQuizEntry, qe1])
      });
      expect(propsForQuizEntryStub).to.have.been.calledTwice();
      expect(propsForQuizEntryStub.getCall(0).args[1]).to.equal(3);
      expect(propsForQuizEntryStub.getCall(1).args[1]).to.equal(6);
    });
    it('renders an AddContentPopover with the correct position', function () {
      render();
      var elem = testbed.findChildrenByType(_Popover.default)[1];
      expect(elem).to.not.be.undefined();
      expect(elem.props.position).to.eq(8);
    });
    it('renders an AddContentPopover for each quizEntry', function () {
      render();
      expect(testbed.findChildrenByType(_Popover.default).length).to.eq(2);
    });
    it('renders no AddContentPopover between questions when isOver is true', function () {
      render({
        draggingOnBuild: true
      });
      expect(testbed.findChildrenByType(_Popover.default).length).to.eq(0);
    });
    it('creates an openDeleteStimulusModal callback', function () {
      render();
      var elem = testbed.findChildByType(_StimulusShow.default);
      elem.props.openDeleteStimulusModal();
      var args = setUiStub.firstCall.args;
      expect(args[0]).to.eq(_constants.DELETE_STIMULUS_DATA);
      var data = args[1].toObject();
      expect(data.quizId).to.eq('456');
      expect(data.stimulusEntry).to.eql(quizEntry);
      expect(data.childEntries.toArray()).to.eql([qe1, qe2]);
      expect(openDeleteStimulusModalStub.called).to.be.true();
    });
  });
  it('renders a StimulusShow component when editing is off', function () {
    testbed.render({
      isEditing: false
    });
    var elem = testbed.findChildByType(_StimulusShow.default);
    expect(elem).to.not.be.undefined();
  });
  it('renders a StimulusEdit component when editing is on', function () {
    testbed.render({
      isEditing: true
    });
    var elem = testbed.findChildByType(_StimulusEdit.default);
    expect(elem).to.not.be.undefined();
  });
  it('renders the StimulusEdit component with the working stimulus', function () {
    var workingStimulus = stimulus.merge({
      title: 'working stimulus title'
    });
    sinon.stub(stimulus, 'getWorkingInstance').returns(workingStimulus);
    testbed.render({
      stimulus: workingStimulus,
      isEditing: true
    });
    var elem = testbed.findChildByType(_StimulusEdit.default);
    expect(elem.props.stimulus.title).to.eq('working stimulus title');
    stimulus.getWorkingInstance.restore();
  });
  it('does not render an immutability warning wrapper when banked', function () {
    testbed.render({
      isBankedContent: true
    });
    var wrappers = testbed.findChildrenByType(_WarningWrapper.default);
    expect(wrappers.length).to.equal(0);
  });
  describe('test with connect', function () {
    var connectedTestbed = createTestbed(wrapInDragAndDropTestContext(withContext(_index.default)), defaultProps);
    shouldRenderComponent(connectedTestbed, {}, _presenter.default);
  });
});