"use strict";

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

var _immutable = require("immutable");

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

var _Fetcher = _interopRequireDefault(require("../../../common/util/Fetcher"));

var _callHelpers = require("../../../common/util/callHelpers");

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

var _stimuli = require("../stimuli");

var _stimuli2 = require("../../../common/actions/stimuli");

var _fakeFetch = _interopRequireDefault(require("../../../../tests/util/fakeFetch"));

var _jsonify = require("../../../common/util/jsonify");

describe('stimulus api actions creators', function () {
  var dispatchStub = sinon.stub();
  var getStateStub = sinon.stub().returns((0, _immutable.fromJS)({
    userSessions: {}
  }));
  var fetchStub = sinon.stub();
  useFakeFetching({
    dispatchStub: dispatchStub,
    getStateStub: getStateStub,
    fetchStub: fetchStub
  });
  var stimulus = new _Stimulus.default({
    id: '22',
    stimulusType: 'text',
    title: 'the title',
    instructions: 'the instructions',
    body: 'the body',
    sourceUrl: 'sourceUrl'
  });
  describe('createStimulus', function () {
    var stubbedCreateStimulusCall = function stubbedCreateStimulusCall() {
      _Fetcher.default.fakeFetch = fetchStub;
      var createStimulusThunk = (0, _stimuli.createStimulus)(stimulus, '3', 'quiz');
      createStimulusThunk(dispatchStub, getStateStub);
    };

    it('should mark the create stimulus call as started', function () {
      var createCallAction = (0, _callHelpers.startCallFor)(_constants.CREATE_STIMULUS_CALL);
      stubbedCreateStimulusCall();
      checkActionDispatched(dispatchStub, createCallAction);
    });
    it('should hit the right endpoint', function () {
      stubbedCreateStimulusCall();
      expect(fetchStub).to.be.calledWith('/api/quizzes/3/stimuli');
    });
    it('should serialize data properly', function () {
      stubbedCreateStimulusCall();
      var expectedData = JSON.stringify({
        stimulus: {
          body: 'the body',
          instructions: 'the instructions',
          parent_stimulus_id: null,
          source_url: 'sourceUrl',
          stimulus_type: 'text',
          title: 'the title'
        }
      });
      expect(fetchStub.getCall(0).args[1].body).to.be.eql(expectedData);
    });

    var fakeCreateStimulus = function fakeCreateStimulus() {
      var response = (0, _jsonify.jsoniblePromise)(stimulus.toJS());
      var fakeFetch = (0, _fakeFetch.default)(response);
      _Fetcher.default.fakeFetch = fakeFetch;
      var createStimulusThunk = (0, _stimuli.createStimulus)(stimulus);
      return createStimulusThunk(dispatchStub, getStateStub);
    };

    it('should mark the create call as a success on return', function (done) {
      var successfulCreate = (0, _callHelpers.successCallFor)(_constants.CREATE_STIMULUS_CALL);
      var fetchPromise = fakeCreateStimulus();
      fetchPromise.then(function () {
        checkActionDispatched(dispatchStub, successfulCreate);
        done();
      });
    });
    it('should try to add the stimulus to the store', function (done) {
      var fetchPromise = fakeCreateStimulus();
      var handleStimulusAction = (0, _stimuli2.addStimuli)([stimulus.toJS()]);
      fetchPromise.then(function () {
        checkActionDispatched(dispatchStub, handleStimulusAction);
        done();
      });
    });
  });
  describe('updateStimulus', function () {
    var stimulusData = {
      id: '22',
      title: 'stimulus title',
      instructions: 'stimulus instructions',
      body: 'stimulus body',
      sourceUrl: 'stimulus sourceUrl'
    };
    var quizId = '3';

    var stubbedUpdateStimulusCall = function stubbedUpdateStimulusCall() {
      _Fetcher.default.fakeFetch = fetchStub;
      var stimulus = new _Stimulus.default(stimulusData);
      var updateStimulusThunk = (0, _stimuli.updateStimulus)(stimulus, quizId);
      updateStimulusThunk(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      stubbedUpdateStimulusCall();
      expect(fetchStub).to.be.calledWith('/api/quizzes/3/stimuli/22');
    });
    it('should mark the update item call as started', function () {
      stubbedUpdateStimulusCall();
      var updateCallStartAction = (0, _callHelpers.startCallFor)(_constants.UPDATE_STIMULUS_CALL);
      checkActionDispatched(dispatchStub, updateCallStartAction);
    });
    it('should serialize data properly', function () {
      stubbedUpdateStimulusCall();
      var expectedData = JSON.stringify({
        stimulus: {
          title: 'stimulus title',
          instructions: 'stimulus instructions',
          body: 'stimulus body',
          source_url: 'stimulus sourceUrl'
        }
      });
      expect(fetchStub.getCall(0).args[1].body).to.be.eql(expectedData);
    });

    var fakeUpdateStimulus = function fakeUpdateStimulus() {
      var response = (0, _jsonify.jsoniblePromise)(stimulusData);
      var fakeFetch = (0, _fakeFetch.default)(response);
      _Fetcher.default.fakeFetch = fakeFetch;
      var stimulus = new _Stimulus.default(stimulusData);
      var updateStimulusThunk = (0, _stimuli.updateStimulus)(stimulus);
      return updateStimulusThunk(dispatchStub, getStateStub);
    };

    it('should mark the update call as a success on return', function (done) {
      var fetchPromise = fakeUpdateStimulus();
      var successfulUpdate = (0, _callHelpers.successCallFor)(_constants.UPDATE_STIMULUS_CALL);
      fetchPromise.then(function () {
        checkActionDispatched(dispatchStub, successfulUpdate);
        done();
      });
    });
    it('should try to update the stimulus in the store', function (done) {
      var fetchPromise = fakeUpdateStimulus();
      var handleStimulusAction = (0, _stimuli2.addStimuli)([stimulusData]);
      fetchPromise.then(function () {
        checkActionDispatched(dispatchStub, handleStimulusAction);
        done();
      });
    });
  });
});