"use strict";

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

var _immutable = require("immutable");

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

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

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

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

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

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

var styles = {
  componentId: 'cbBqk',
  template: function template(theme) {
    return "\n\n.DeleteStimulusModal__styles__deleteStimulusModal {\n}\n\n.DeleteStimulusModal__styles__checkboxWrapper {\n  padding: ".concat(theme.checkboxWrapperPadding || 'inherit', " 0;\n}\n\n.DeleteStimulusModal__styles__checkboxesWrapper {\n  width: 95%;\n  margin-left: auto;\n}\n\n.DeleteStimulusModal__styles__stimulusTitle {\n  padding-bottom: ").concat(theme.stimulusTitleBottomPadding || 'inherit', ";\n  font-size: ").concat(theme.stimulusTitleFontSize || 'inherit', ";\n}\n\n.DeleteStimulusModal__styles__radioSpace {\n  padding-bottom: ").concat(theme.radioSpaceBottomPadding || 'inherit', ";\n}\n");
  },
  'deleteStimulusModal': 'DeleteStimulusModal__styles__deleteStimulusModal',
  'checkboxWrapper': 'DeleteStimulusModal__styles__checkboxWrapper',
  'checkboxesWrapper': 'DeleteStimulusModal__styles__checkboxesWrapper',
  'stimulusTitle': 'DeleteStimulusModal__styles__stimulusTitle',
  'radioSpace': 'DeleteStimulusModal__styles__radioSpace'
};
describe('DeleteStimulasModal', function () {
  var closeModalStub = sinon.stub();
  var removeStimulusStub = sinon.stub();
  var setUiStub = sinon.stub();
  var withConfirmStub = sinon.stub();
  var stimulusEntry = new _QuizEntry.default();
  var stimulus = new _Stimulus.default();

  stimulusEntry.getEntry = function () {
    return stimulus;
  };

  var itemEntry1 = new _QuizEntry.default({
    id: '201'
  });
  var item1 = new _Item.default({
    id: '301',
    itemBody: '<p>aaa</p>'
  });

  itemEntry1.getEntry = function () {
    return item1;
  };

  item1.quizEntry = itemEntry1;
  var itemEntry2 = new _QuizEntry.default({
    id: '202'
  });
  var item2 = new _Item.default({
    id: '302',
    itemBody: 'bbb'
  });

  itemEntry2.getEntry = function () {
    return item2;
  };

  item2.quizEntry = itemEntry2;
  var itemEntry3 = new _QuizEntry.default({
    id: '203'
  });
  var item3 = new _Item.default({
    id: '303',
    itemBody: 'ccc'
  });

  itemEntry3.getEntry = function () {
    return item3;
  };

  item3.quizEntry = itemEntry3;
  var defaultProps = {
    childEntries: (0, _immutable.List)([itemEntry1, itemEntry2, itemEntry3]),
    closeModal: closeModalStub,
    deletionMode: null,
    modalOpen: true,
    quizId: '456',
    removeStimulus: removeStimulusStub,
    selectedChildren: _immutable.Set.of(),
    setUi: setUiStub,
    stimulusEntry: stimulusEntry,
    withConfirm: withConfirmStub
  };
  var testbed = createTestbed(withContext(_presenter.default), defaultProps);
  afterEach(function () {
    closeModalStub.resetHistory();
    removeStimulusStub.resetHistory();
    setUiStub.resetHistory();
  });

  var getModal = function getModal() {
    return window.document.querySelector(".".concat(styles.deleteStimulusModal));
  };

  var submit = function submit() {
    var button = window.document.querySelectorAll('button')[2];
    expect(button.innerText).to.eq('Submit');
    button.click();
  };

  context('zero children', function () {
    it('renders no options', function () {
      testbed.render({
        childEntries: (0, _immutable.List)([])
      });
      var inputs = getModal().querySelectorAll('input[type=radio]');
      expect(inputs.length).to.eq(0);
    });
  });
  context('one child', function () {
    it('renders only two options', function () {
      testbed.render({
        childEntries: (0, _immutable.List)([itemEntry1])
      });
      var inputs = getModal().querySelectorAll('input[type=radio]');
      expect(inputs.length).to.eq(2);
    });
  });
  context('multiple children', function () {
    it('renders three input buttons', function () {
      testbed.render();
      var inputs = getModal().querySelectorAll('input[type=radio]');
      expect(inputs.length).to.eq(3);
    });
    it('sets the deletion mode to deleteAll when option is selected', function () {
      testbed.render();
      var inputs = getModal().querySelectorAll('input[type=radio]');
      inputs[0].click();
      expect(setUiStub.calledWith(_constants.DELETE_STIMULUS_MODAL_DELETION_MODE, 'deleteAll'));
    });
    it('deletes all children when deleteAll option is selected', function () {
      testbed.render({
        deletionMode: 'deleteAll'
      });
      submit();
      expect(removeStimulusStub.calledWith('456', stimulusEntry, defaultProps.childEntries));
    });
    it('does not show checkboxes when deleteAll option is selected', function () {
      testbed.render({
        deletionMode: 'deleteAll'
      });
      var checkboxes = getModal().querySelectorAll('input[type=checkbox]');
      expect(checkboxes.length).to.eq(0);
    });
    it('sets the deletion mode to deleteNone when option is selected', function () {
      testbed.render();
      var inputs = getModal().querySelectorAll('input[type=radio]');
      inputs[2].click();
      expect(setUiStub.calledWith(_constants.DELETE_STIMULUS_MODAL_DELETION_MODE, 'deleteNone'));
    });
    it('deletes no children when deleteNone option is selected', function () {
      testbed.render({
        deletionMode: 'deleteNone'
      });
      submit();
      expect(removeStimulusStub.calledWith('456', stimulusEntry, []));
    });
    it('does not show checkboxes when deleteNone option is selected', function () {
      testbed.render({
        deletionMode: 'deleteNone'
      });
      var checkboxes = getModal().querySelectorAll('input[type=checkbox]');
      expect(checkboxes.length).to.eq(0);
    });
    it('sets the deletion mode to chooseWhich when option is selected', function () {
      testbed.render();
      var inputs = getModal().querySelectorAll('input[type=radio]');
      inputs[1].click();
      expect(setUiStub.calledWith(_constants.DELETE_STIMULUS_MODAL_DELETION_MODE, 'chooseWhich'));
    });
    it('renders checkboxes when chooseWhich option is selected', function () {
      testbed.render({
        deletionMode: 'chooseWhich'
      });
      var checkboxes = getModal().querySelectorAll('input[type=checkbox]');
      expect(checkboxes.length).to.eq(defaultProps.childEntries.size);
    });
    it('renders the stems of each question when chooseWhich option is selected', function () {
      testbed.render({
        deletionMode: 'chooseWhich'
      });
      var inputs = getModal().querySelectorAll('input[type=checkbox]');
      var stems = Array.from(inputs, function (input) {
        return input.labels[0].innerText.trim();
      });
      expect(stems).to.deep.eq(['aaa', 'bbb', 'ccc']);
    });
    it('tries to add a child entry when checkbox is checked', function () {
      testbed.render({
        deletionMode: 'chooseWhich',
        selectedChildren: _immutable.Set.of()
      });
      var checkboxes = getModal().querySelectorAll('input[type=checkbox]');
      expect(checkboxes[0].checked).to.be.false();
      checkboxes[0].click();
      var args = setUiStub.firstCall.args;
      expect(args[0]).to.eq(_constants.DELETE_STIMULUS_MODAL_SELECTED_CHILDREN);
      expect(args[1].equals(_immutable.Set.of(itemEntry1))).to.be.true();
    });
    it('tries to remove a child entry when checkbox is unchecked', function () {
      testbed.render({
        deletionMode: 'chooseWhich',
        selectedChildren: _immutable.Set.of(itemEntry1)
      });
      var checkboxes = getModal().querySelectorAll('input[type=checkbox]');
      expect(checkboxes[0].checked).to.be.true();
      checkboxes[0].click();
      var args = setUiStub.firstCall.args;
      expect(args[0]).to.eq(_constants.DELETE_STIMULUS_MODAL_SELECTED_CHILDREN);
      expect(args[1].equals(_immutable.Set.of())).to.be.true();
    });
    it('submits the form with the selected items to delete', function () {
      testbed.render({
        deletionMode: 'chooseWhich',
        selectedChildren: _immutable.Set.of(itemEntry1, itemEntry2)
      });
      submit();
      expect(removeStimulusStub.calledWith('456', stimulusEntry, [itemEntry1.id, itemEntry2.id]));
    });
    it('opens a confirmation modal if keeping immutable items', function () {
      var immutableQuizEntry = new _QuizEntry.default({
        status: 'immutable'
      });
      testbed.render({
        childEntries: (0, _immutable.List)([immutableQuizEntry]),
        deletionMode: 'deleteNone'
      });
      submit();
      expect(withConfirmStub).to.have.been.called();
    });
  });
  describe('#anyImmutableKeepers', function () {
    it('returns true if at least one child entry which will be kept in quiz is immutable', function () {
      var immutableQuizEntry = new _QuizEntry.default({
        status: 'immutable'
      });
      var mutableQuizEntry = new _QuizEntry.default({
        status: 'mutable'
      });
      testbed.render({
        childEntries: (0, _immutable.List)([immutableQuizEntry, mutableQuizEntry]),
        selectedChildren: (0, _immutable.Set)([mutableQuizEntry])
      });
      var modal = testbed.findChildrenByType(_presenter.default)[0];
      expect(modal.anyImmutableKeepers()).to.be.true();
    });
    it('returns false if all kept entries are mutable', function () {
      var immutableQuizEntry = new _QuizEntry.default({
        status: 'immutable'
      });
      var mutableQuizEntry = new _QuizEntry.default({
        status: 'mutable'
      });
      testbed.render({
        childEntries: (0, _immutable.List)([immutableQuizEntry, mutableQuizEntry]),
        selectedChildren: (0, _immutable.Set)([immutableQuizEntry])
      });
      var modal = testbed.findChildrenByType(_presenter.default)[0];
      expect(modal.anyImmutableKeepers()).to.be.false();
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done, {
        ignores: ['label', 'label-title-only']
      });
    });
  });
  describe('test with connect', function () {
    var connectedTestbed = createTestbed(withContext(_index.default), defaultProps);
    shouldRenderComponent(connectedTestbed, {}, _presenter.default);
  });
});