"use strict";

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

var _immutable = require("immutable");

var _quizNumberInput = _interopRequireDefault(require("@instructure/quiz-number-input"));

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

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

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

var _EntrySave = _interopRequireDefault(require("../../../../../../../common/components/resources/entry/EntrySave"));

describe('QuizEntryBankEdit', function () {
  var fakeQuizEntry = new _QuizEntry.default();
  var sandbox = sinon.createSandbox();
  var changeQuizEntryPointsStub = sandbox.stub();
  var changeQuizEntryPropertiesStub = sandbox.stub();
  var changeQuizEntryEntryStub = sandbox.stub();
  var clearModificationsStub = sandbox.stub();
  var switchOffEditingStub = sandbox.stub();
  var updateQuizEntryStub = sandbox.stub();
  var quizEntryBankProps = {
    displayPosition: 1,
    guid: 'myguid',
    pointsPossible: '0',
    workingBank: new _Bank.default({
      entryCount: 5
    }),
    quizEntry: fakeQuizEntry,
    workingQuizEntry: fakeQuizEntry,
    changeQuizEntryPoints: changeQuizEntryPointsStub,
    changeQuizEntryProperties: changeQuizEntryPropertiesStub,
    changeQuizEntryEntry: changeQuizEntryEntryStub,
    clearModifications: clearModificationsStub,
    switchOffEditing: switchOffEditingStub,
    updateQuizEntry: updateQuizEntryStub
  };
  afterEach(function () {
    sandbox.resetHistory();
  });
  var testbed = createTestbed(_presenter.default, quizEntryBankProps);
  var shallowtestbed = testbed.renderShallowly();
  it('renders the quiz entry bank', function () {
    var text = shallowtestbed.find('Text').first().render().text();
    expect(text).to.include('Number of questions');
  });
  it('renders the quiz entry bank with number of questions pulled and points possible', function () {
    var banktestbed = testbed.renderShallowly({
      pointsPossible: '121',
      quizEntry: new _QuizEntry.default({
        properties: (0, _immutable.Map)({
          sampleNum: 2
        })
      })
    });
    var text = banktestbed.find('Text').first().node.props.children;
    expect(text).to.include('Number of questions');
    var numberInput = banktestbed.find(_quizNumberInput.default).last();
    expect(numberInput.node.props.value).to.equal('121');
  });
  it('renders bank one input when questionSelection is set to all', function () {
    var output = testbed.renderShallowly({
      questionSelection: 'all'
    });
    var inputs = output.find('Text');
    expect(inputs.length).to.eq(1);
  });
  it('renders bank both inputs when questionSelection is set to rand', function () {
    var output = testbed.renderShallowly({
      questionSelection: 'rand'
    });
    var inputs = output.find('Text');
    expect(inputs.length).to.eq(2);
  });
  it('renders an error alert when the workingBank is archived', function () {
    var output = testbed.renderShallowly({
      workingBank: new _Bank.default({
        archived: true
      })
    });
    var alert = output.find('Alert');
    expect(alert.length).to.eq(1);
  });
  describe('clicks done', function () {
    it('submitHandler calls updateQuizEntry', function () {
      var entrySave = shallowtestbed.find(_EntrySave.default);
      entrySave.props().submitHandler();
      expect(updateQuizEntryStub.called).to.be.true();
      expect(clearModificationsStub.called).to.be.true();
    });
    it('passes the guid to the EntrySave to ensure correct editable actions are called', function () {
      var entrySave = shallowtestbed.find(_EntrySave.default);
      expect(entrySave.props().guid).to.eq(quizEntryBankProps.guid);
    });
  });
  it('Sets modification state for points possible', function () {
    var numberInput = shallowtestbed.find(_quizNumberInput.default).last();
    numberInput.node.props.onChange({
      target: {
        value: '6'
      }
    }, '6', '6');
    expect(changeQuizEntryPointsStub.calledWith('0', '6')).to.be.true();
  });
  it('Sets modification state for sampleNum', function () {
    var numberInput = testbed.renderShallowly({
      questionSelection: 'rand'
    }).find(_quizNumberInput.default).first();
    numberInput.node.props.onChange({
      target: {
        value: '9'
      }
    }, '9', '9');
    expect(changeQuizEntryPropertiesStub.calledWith('0', {
      selection: 'rand',
      sampleNum: '9'
    })).to.be.true();
  });
});