"use strict";

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

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

var _immutable = require("immutable");

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

var _featureCheck = require("../../../../../../util/featureCheck");

var _reduxStore = require("../../../../../../../reduxStore");

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

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

describe('AddContentBody', function () {
  var createItemStub = sinon.stub();
  var createStimulusStub = sinon.stub();
  var onOpenBankTrayStub = sinon.stub();
  var onAddItemStub = sinon.stub();
  var screenreaderNotificationStub = sinon.stub();
  var setNextQuizEntryStub = sinon.stub();
  var setUiStub = sinon.stub();

  var fakeInteractionType = function fakeInteractionType() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'choice';
    return new _InteractionType.default({
      slug: name,
      name: name
    });
  };

  var defaultProps = {
    allowStimulusCreation: true,
    createItem: createItemStub,
    createStimulus: createStimulusStub,
    editingQuizEntry: false,
    interactionTypes: (0, _immutable.List)([fakeInteractionType('ONE'), fakeInteractionType('TWO'), fakeInteractionType('THREE')]),
    onOpenBankTray: onOpenBankTrayStub,
    onAddItem: onAddItemStub,
    parentEntryId: 'parent123',
    position: 4,
    quizEntryWithErrorsShowing: null,
    quizId: 'quiz123',
    screenreaderNotification: screenreaderNotificationStub,
    setNextQuizEntry: setNextQuizEntryStub,
    setUi: setUiStub,
    showHeaderText: true,
    stimulusId: 'stimulus123'
  };
  var testbed = createTestbed(withContext(_presenter.default), defaultProps);
  beforeEach(function () {
    (0, _reduxStore.unsetFakeStore)();
    createItemStub.resetHistory();
    createStimulusStub.resetHistory();
    onOpenBankTrayStub.resetHistory();
    onAddItemStub.resetHistory();
    screenreaderNotificationStub.resetHistory();
    setNextQuizEntryStub.resetHistory();
    setUiStub.resetHistory();
  });
  (0, _featureCheck.withTestFeatures)(['stimulus']);
  it('displays the interaction types', function () {
    testbed.render();
    var html = testbed.rootNode.innerHTML;
    expect(html.match(/ONE/)).to.exist();
    expect(html.match(/TWO/)).to.exist();
    expect(html.match(/THREE/)).to.exist();
  });
  it('displays an add stimulus button if allowStimulusCreation is true', function () {
    testbed.render();
    var html = testbed.rootNode.innerHTML;
    expect(html.match(/Stimulus/)).to.be.not.be.null();
  });
  it('does not display an add stimulus button if allowStimulusCreation is false', function () {
    testbed.render({
      allowStimulusCreation: false
    });
    var html = testbed.rootNode.innerHTML;
    expect(html.match(/Stimulus/)).to.be.null();
  });
  context('open bank tray button', function () {
    it('calls the callsbacks when the button is clicked', function () {
      testbed.render();
      var button = testbed.findChildrenBySelector('[data-automation="sdk-add-banked-content-button"]')[0];

      _testUtils.default.Simulate.click(button);

      expect(onOpenBankTrayStub.called).to.be.true();
      expect(setUiStub.calledWith(_constants.BUILD_TRAY_OPEN, true)).to.be.true();
      expect(setUiStub.calledWith(_constants.BUILD_TRAY_POSITION, defaultProps.position)).to.be.true();
      expect(setUiStub.calledWith(_constants.BUILD_TRAY_STIMULUS_ID, defaultProps.parentEntryId)).to.be.true();
    });
  });
  context('addItem', function () {
    it('reports an screenreader error when errors are showing', function () {
      testbed.render({
        quizEntryWithErrorsShowing: {
          position: 10
        }
      });
      var button = testbed.findChildrenByTagName('button')[1];
      button.click();
      expect(screenreaderNotificationStub.called).to.be.true();
    });
    it('does not report an screenreader error when errors are not showing', function () {
      testbed.render();
      var button = testbed.findChildrenByTagName('button')[1];
      button.click();
      expect(screenreaderNotificationStub.called).to.be.false();
    });
    it('calls setNextQuizEntry if an editingQuizEntry exists', function () {
      testbed.render({
        editingQuizEntry: {}
      });
      var button = testbed.findChildrenByTagName('button')[1];
      button.click();
      expect(setNextQuizEntryStub.called).to.be.true();
      var args = setNextQuizEntryStub.firstCall.args;
      expect(args[1]).to.eq(defaultProps.quizId);
      expect(args[2]).to.eq(defaultProps.position);
      expect(args[3]).to.eq(defaultProps.stimulusId);
      expect(args[4]).to.eq(defaultProps.parentEntryId);
    });
    it('calls createItem otherwise', function () {
      testbed.render({
        editingQuizEntry: null
      });
      var button = testbed.findChildrenByTagName('button')[1];
      button.click();
      expect(createItemStub.called).to.be.true();
      var args = createItemStub.firstCall.args;
      expect(args[0]).to.eq(defaultProps.quizId);
      expect(args[1]).to.eq(defaultProps.position);
      expect(args[2].id).to.eq(defaultProps.interactionTypes.get(0).id);
      expect(args[3]).to.eq(defaultProps.parentEntryId);
      expect(args[4]).to.eq(defaultProps.stimulusId);
    });
  });
  context('header', function () {
    it('shows a header when showHeaderText is true', function () {
      testbed.render();
      expect(testbed.rootNode.innerText.match(/Insert Content/)).to.not.be.null();
    });
    it('does not show a header when showHeaderText is true', function () {
      testbed.render({
        showHeaderText: false
      });
      expect(testbed.rootNode.innerText.match(/Insert Content/)).to.be.null();
    });
  });
  context('addStimulus', function () {
    it('calls setNextQuizEntry if editingQuizEntry exists', function () {
      testbed.render({
        editingQuizEntry: {},
        allowStimulusCreation: true
      });
      var button = testbed.findChildrenByTagName('button')[4];
      button.click();
      expect(setNextQuizEntryStub.calledWith(null, defaultProps.quizId, defaultProps.position)).to.be.true();
    });
    it('calls createStimulus otherwise', function () {
      testbed.render({
        editingQuizEntry: null,
        allowStimulusCreation: true
      });
      var button = testbed.findChildrenByTagName('button')[4];
      button.click();
      expect(createStimulusStub.calledWith(defaultProps.quizId, defaultProps.position)).to.be.true();
    });
    it('calls onAddItem', function () {
      testbed.render({
        editingQuizEntry: null,
        allowStimulusCreation: true
      });
      var button = testbed.findChildrenByTagName('button')[4];
      button.click();
      expect(onAddItemStub.called).to.be.true();
    });
  });
  describe('test with connect', function () {
    shouldRenderComponent(testbed, {}, _presenter.default);
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});