"use strict";

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

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

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

var _reduxMockStore = _interopRequireDefault(require("redux-mock-store"));

var _reduxMulti = _interopRequireDefault(require("redux-multi"));

var _reduxThunk = _interopRequireDefault(require("redux-thunk"));

var _immutable = require("immutable");

var _chai = require("chai");

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

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

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

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

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

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

var _quizEntries = require("../../../common/actions/quizEntries");

var _quizzes = require("../../../common/actions/quizzes");

var _ui = require("../../../common/actions/ui");

var _scrolling = require("../../../common/actions/scrolling");

var _quizEntries2 = require("../quizEntries");

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

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

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

describe('quizEntries api actions creators', function () {
  var mockStoreFactory = (0, _reduxMockStore.default)([_reduxMulti.default, _reduxThunk.default]);

  var mockStore = function mockStore() {
    var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.Map)();
    var store = mockStoreFactory(state); // Fetcher dispatches CALL_STATUS actions. This makes it dispatch the
    // actions to the mock store

    _Fetcher.default.fakeDispatch = store.dispatch; // We also need to do this to make all the ReduxRecord relational stuff use
    // our fake state

    (0, _reduxStore.setFakeStore)(state);
    return store;
  };

  afterEach(function () {
    (0, _reduxStore.unsetFakeStore)();
  });
  useFakeFetching();
  describe('getQuizEntries', function () {
    var quizId = '7';
    var fakeQuizEntries = [{
      id: '2',
      entryId: '3',
      quiz: '7',
      entry_type: 'Item',
      entry: {
        id: '3'
      }
    }];
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _quizEntries2.getQuizEntries)(quizId));
      (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWith("/api/quizzes/".concat(quizId, "/quiz_entries"));
    });

    var fakeGetQuizEntries = function fakeGetQuizEntries() {
      var response = (0, _jsonify.jsoniblePromise)(fakeQuizEntries);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizEntries2.getQuizEntries)(quizId);
    };

    it('should try to add to the quiz entries in the store',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var actionPayload, store, handleItemAction;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              actionPayload = [{
                id: '2',
                entryId: '3',
                quiz: '7',
                entryType: 'Item',
                entry: {
                  id: '3',
                  schema: 'Item'
                }
              }];
              store = mockStore();
              handleItemAction = (0, _quizEntries.addQuizEntries)(actionPayload);
              _context.next = 5;
              return store.dispatch(fakeGetQuizEntries());

            case 5:
              (0, _chai.expect)(store.getActions()).to.deep.include(handleItemAction);

            case 6:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
    it('should try to associate the entry with the quiz',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var store, associateEntryAction;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              store = mockStore();
              associateEntryAction = (0, _quizzes.associateQuizEntries)(quizId, fakeQuizEntries);
              _context2.next = 4;
              return store.dispatch(fakeGetQuizEntries());

            case 4:
              (0, _chai.expect)(store.getActions()).to.deep.include(associateEntryAction);

            case 5:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
  describe('updateQuizEntry', function () {
    var quizId = '1';
    var quizEntryId = '22';
    var quizEntryMod = {
      id: quizEntryId,
      pointsPossible: '5',
      position: '4',
      quiz: quizId,
      entryId: null
    };

    var fakeUpdateQuizEntry = function fakeUpdateQuizEntry(opts) {
      var response = (0, _jsonify.jsoniblePromise)(quizEntryMod);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizEntries2.updateQuizEntry)(quizId, quizEntryId, quizEntryMod, opts);
    };

    it('should hit the right endpoint',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee3() {
      var store;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              store = mockStore();
              _context3.next = 3;
              return store.dispatch(fakeUpdateQuizEntry());

            case 3:
              (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWith("/api/quizzes/".concat(quizId, "/quiz_entries/").concat(quizEntryId));

            case 4:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
    it('should filter out null quiz_entry values',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee4() {
      var store, callJson;
      return _regenerator.default.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              store = mockStore();
              _context4.next = 3;
              return store.dispatch(fakeUpdateQuizEntry());

            case 3:
              callJson = _Fetcher.default.fakeFetch.getCall(0).args[1];
              (0, _chai.expect)(callJson.body).to.not.include('entry_id');

            case 5:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
    it('should mark the update quiz entry call as started',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee5() {
      var store, updateCallStartAction;
      return _regenerator.default.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              store = mockStore();
              _context5.next = 3;
              return store.dispatch(fakeUpdateQuizEntry());

            case 3:
              updateCallStartAction = (0, _callHelpers.startCallFor)(_constants.UPDATE_QUIZ_ENTRY_CALL);
              (0, _chai.expect)(store.getActions()).to.deep.include(updateCallStartAction);

            case 5:
            case "end":
              return _context5.stop();
          }
        }
      }, _callee5);
    })));
    it('should mark the update call as a success on return',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee6() {
      var store, successfulUpdate;
      return _regenerator.default.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              store = mockStore();
              _context6.next = 3;
              return store.dispatch(fakeUpdateQuizEntry());

            case 3:
              successfulUpdate = (0, _callHelpers.successCallFor)(_constants.UPDATE_QUIZ_ENTRY_CALL);
              (0, _chai.expect)(store.getActions()).to.deep.include(successfulUpdate);

            case 5:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    })));
    it('should try to update the quiz entry in the store',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee7() {
      var store, handleItemAction;
      return _regenerator.default.wrap(function _callee7$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              store = mockStore();
              _context7.next = 3;
              return store.dispatch(fakeUpdateQuizEntry());

            case 3:
              handleItemAction = (0, _quizEntries.addQuizEntry)([quizEntryMod]);
              (0, _chai.expect)(store.getActions()).to.deep.include(handleItemAction);

            case 5:
            case "end":
              return _context7.stop();
          }
        }
      }, _callee7);
    })));
    it('should not update the quiz entry in the store if handleResponse is false',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee8() {
      var store, handleItemAction;
      return _regenerator.default.wrap(function _callee8$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              store = mockStore();
              _context8.next = 3;
              return store.dispatch(fakeUpdateQuizEntry({
                handleResponse: false
              }));

            case 3:
              handleItemAction = (0, _quizEntries.addQuizEntry)([quizEntryMod]);
              (0, _chai.expect)(store.getActions()).not.to.deep.include(handleItemAction);

            case 5:
            case "end":
              return _context8.stop();
          }
        }
      }, _callee8);
    })));
  });
  describe('moveQuizEntry', function () {
    // testrail test_id: C3003422
    var quizEntryId = '22';
    var quizId = '1';
    var position = 1;
    var stimulusQuizEntryId = 5;
    var moveTempItemStub = sinon.stub();
    var actionStub = sinon.stub();
    beforeEach(function () {
      actionStub.returns({
        type: _constants.DO_NOTHING
      });
    });
    afterEach(function () {
      actionStub.reset();
      moveTempItemStub.reset();
    });

    var stubbedMoveQuizEntryCall = function stubbedMoveQuizEntryCall() {
      return (0, _quizEntries2.moveQuizEntry)(quizId, position, stimulusQuizEntryId, quizEntryId, moveTempItemStub, actionStub);
    };

    it('moves the entry',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee9() {
      var store, moveAction;
      return _regenerator.default.wrap(function _callee9$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              store = mockStore();
              _context9.next = 3;
              return store.dispatch(stubbedMoveQuizEntryCall());

            case 3:
              moveAction = (0, _quizEntries.moveQuizEntry)(quizEntryId, position, stimulusQuizEntryId);
              (0, _chai.expect)(store.getActions()).to.deep.include(moveAction);

            case 5:
            case "end":
              return _context9.stop();
          }
        }
      }, _callee9);
    })));
    it('should setUI with the moved quiz entry id if position changed',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee10() {
      var store;
      return _regenerator.default.wrap(function _callee10$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              store = mockStore();
              _context10.next = 3;
              return store.dispatch(stubbedMoveQuizEntryCall());

            case 3:
              (0, _chai.expect)(store.getActions()).to.deep.include((0, _ui.set)(_constants.MOVED_QUIZ_ENTRY, quizEntryId));

            case 4:
            case "end":
              return _context10.stop();
          }
        }
      }, _callee10);
    })));
    it('calls updateQuizEntry with the right argument',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee11() {
      var store;
      return _regenerator.default.wrap(function _callee11$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              store = mockStore();
              _context11.next = 3;
              return store.dispatch(stubbedMoveQuizEntryCall());

            case 3:
              (0, _chai.expect)(actionStub).to.have.been.calledWith(quizId, quizEntryId, {
                position: position,
                stimulusQuizEntryId: stimulusQuizEntryId
              });

            case 4:
            case "end":
              return _context11.stop();
          }
        }
      }, _callee11);
    })));
    it('calls moveTempItem',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee12() {
      var store;
      return _regenerator.default.wrap(function _callee12$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              store = mockStore();
              _context12.next = 3;
              return store.dispatch(stubbedMoveQuizEntryCall());

            case 3:
              (0, _chai.expect)(moveTempItemStub).to.have.been.calledOnce();

            case 4:
            case "end":
              return _context12.stop();
          }
        }
      }, _callee12);
    })));
    it('re-fetches all quiz entries if the request fails',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee13() {
      var state, store;
      return _regenerator.default.wrap(function _callee13$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              actionStub.returns(function () {
                return Promise.reject();
              });
              state = (0, _immutable.Map)({
                quizzes: (0, _immutable.Map)({
                  activeQuizId: quizId
                })
              });
              store = mockStore(state);
              _context13.next = 5;
              return (0, _chai.expect)(store.dispatch(stubbedMoveQuizEntryCall())).to.be.rejected();

            case 5:
              (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWithMatch("/api/quizzes/".concat(quizId, "/quiz_entries"), {
                method: 'GET'
              });

            case 6:
            case "end":
              return _context13.stop();
          }
        }
      }, _callee13);
    })));
  });
  describe('createStimulusAndQuizEntry', function () {
    var quizId = '7';
    var quizEntry = new _QuizEntry.default({
      id: '22',
      entry: (0, _immutable.Map)({
        id: 'foo'
      }),
      position: '1',
      entryType: 'Stimulus',
      quizEntryStimulusId: 55
    });
    var fakeStimulusData = {
      id: 'foo'
    };
    var stubbedStimulusCreation = sinon.stub().returns(function () {
      return Promise.resolve(fakeStimulusData);
    });
    var stubbedQuizEntryCreation = sinon.stub().returns({
      type: _constants.DO_NOTHING
    });
    afterEach(function () {
      stubbedStimulusCreation.resetHistory();
      stubbedQuizEntryCreation.resetHistory();
    });

    var fakeCreateStimulusAndQuizEntry = function fakeCreateStimulusAndQuizEntry() {
      var response = (0, _jsonify.jsoniblePromise)(quizEntry);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizEntries2.createStimulusAndQuizEntry)(quizId, fakeStimulusData, quizEntry, stubbedStimulusCreation, stubbedQuizEntryCreation);
    };

    it('calls createStimulus with the right argument',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee14() {
      var store;
      return _regenerator.default.wrap(function _callee14$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              store = mockStore();
              _context14.next = 3;
              return store.dispatch(fakeCreateStimulusAndQuizEntry());

            case 3:
              (0, _chai.expect)(stubbedStimulusCreation).to.have.been.calledWith(fakeStimulusData);

            case 4:
            case "end":
              return _context14.stop();
          }
        }
      }, _callee14);
    })));
    it('calls createQuizEntry with the right arguments',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee15() {
      var store;
      return _regenerator.default.wrap(function _callee15$(_context15) {
        while (1) {
          switch (_context15.prev = _context15.next) {
            case 0:
              store = mockStore();
              _context15.next = 3;
              return store.dispatch(fakeCreateStimulusAndQuizEntry());

            case 3:
              (0, _chai.expect)(stubbedQuizEntryCreation).to.have.been.calledWith('7', 'foo', 'Stimulus');

            case 4:
            case "end":
              return _context15.stop();
          }
        }
      }, _callee15);
    })));
  });
  describe('createItemAndQuizEntry', function () {
    var quizId = '7';
    var quizEntry = new _QuizEntry.default({
      id: '22',
      pointsPossible: '5',
      position: '1',
      entryType: 'Item',
      stimulusQuizEntryId: '55'
    });
    var fakeItemData = {
      id: 'foo'
    };
    var stubbedItemCreation = sinon.stub().returns(function () {
      return Promise.resolve(fakeItemData);
    });
    var stubbedQuizEntryCreation = sinon.stub().returns({
      type: _constants.DO_NOTHING
    });

    var fakeCreateItemAndQuizEntry = function fakeCreateItemAndQuizEntry() {
      var response = (0, _jsonify.jsoniblePromise)(quizEntry);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizEntries2.createItemAndQuizEntry)(quizId, fakeItemData, quizEntry, stubbedItemCreation, stubbedQuizEntryCreation);
    };

    it('calls createItem with the right argument',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee16() {
      var store;
      return _regenerator.default.wrap(function _callee16$(_context16) {
        while (1) {
          switch (_context16.prev = _context16.next) {
            case 0:
              store = mockStore();
              _context16.next = 3;
              return store.dispatch(fakeCreateItemAndQuizEntry());

            case 3:
              (0, _chai.expect)(stubbedItemCreation).to.have.been.calledWith(fakeItemData);

            case 4:
            case "end":
              return _context16.stop();
          }
        }
      }, _callee16);
    })));
    it('calls createQuizEntry with the right arguments',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee17() {
      var store, expectedEntryProperties;
      return _regenerator.default.wrap(function _callee17$(_context17) {
        while (1) {
          switch (_context17.prev = _context17.next) {
            case 0:
              store = mockStore();
              expectedEntryProperties = {
                points_possible: 5,
                position: 1,
                stimulus_quiz_entry_id: '55'
              };
              _context17.next = 4;
              return store.dispatch(fakeCreateItemAndQuizEntry());

            case 4:
              (0, _chai.expect)(stubbedQuizEntryCreation).to.have.been.calledWith('7', 'foo', 'Item', expectedEntryProperties);

            case 5:
            case "end":
              return _context17.stop();
          }
        }
      }, _callee17);
    })));
  });
  describe('createQuizEntry', function () {
    var quizId = '7';
    var itemId = '1';
    var quizEntryId = '2';

    var stubbedCreateQuizEntryCall = function stubbedCreateQuizEntryCall() {
      var fakeQuizEntry = {
        id: quizEntryId,
        entryId: itemId,
        quiz: quizId
      };
      var entryResponse = (0, _jsonify.jsoniblePromise)(fakeQuizEntry);
      var entriesResponse = (0, _jsonify.jsoniblePromise)([fakeQuizEntry]); // 2nd call stubs fetch in getQuizEntries, which is called from createQuizEntry

      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(entryResponse).onCall(1).resolves(entriesResponse);
      return (0, _quizEntries2.createQuizEntry)(quizId, itemId, 'Item', {});
    };

    it('should hit the right endpoints',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee18() {
      var store;
      return _regenerator.default.wrap(function _callee18$(_context18) {
        while (1) {
          switch (_context18.prev = _context18.next) {
            case 0:
              store = mockStore();
              _context18.next = 3;
              return store.dispatch(stubbedCreateQuizEntryCall());

            case 3:
              (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWith("/api/quizzes/".concat(quizId, "/quiz_entries"));

            case 4:
            case "end":
              return _context18.stop();
          }
        }
      }, _callee18);
    })));
    it('should try to scroll to get the quiz entries and scroll to the item',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee19() {
      var store, scrollAction;
      return _regenerator.default.wrap(function _callee19$(_context19) {
        while (1) {
          switch (_context19.prev = _context19.next) {
            case 0:
              store = mockStore();
              _context19.next = 3;
              return store.dispatch(stubbedCreateQuizEntryCall());

            case 3:
              scrollAction = (0, _scrolling.scrollToItem)(quizEntryId, true);
              (0, _chai.expect)(store.getActions()).to.deep.include(scrollAction);

            case 5:
            case "end":
              return _context19.stop();
          }
        }
      }, _callee19);
    })));
  });
  describe('removeQuizEntry', function () {
    var quizId = '7';
    var quizEntry = {
      id: '1',
      pointsPossible: '5'
    };

    var stubbedRemoveQuizEntryCall = function stubbedRemoveQuizEntryCall() {
      var response = (0, _jsonify.jsoniblePromise)(null);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizEntries2.removeQuizEntry)(quizId, quizEntry);
    };

    it('should hit the right endpoint',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee20() {
      var store, url;
      return _regenerator.default.wrap(function _callee20$(_context20) {
        while (1) {
          switch (_context20.prev = _context20.next) {
            case 0:
              store = mockStore();
              _context20.next = 3;
              return store.dispatch(stubbedRemoveQuizEntryCall());

            case 3:
              url = "/api/quizzes/".concat(quizId, "/quiz_entries/").concat(quizEntry.id);
              (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWithMatch(url, {
                method: 'DELETE'
              });

            case 5:
            case "end":
              return _context20.stop();
          }
        }
      }, _callee20);
    })));
  });
  describe('getQuizEntry', function () {
    var quizId = '7';
    var quizEntryId = '1';

    var stubbedGetQuizEntryCall = function stubbedGetQuizEntryCall() {
      var response = (0, _jsonify.jsoniblePromise)({
        id: quizEntryId,
        quiz: quizId
      });
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizEntries2.getQuizEntry)(quizId, quizEntryId);
    };

    it('should hit the right endpoint',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee21() {
      var store;
      return _regenerator.default.wrap(function _callee21$(_context21) {
        while (1) {
          switch (_context21.prev = _context21.next) {
            case 0:
              store = mockStore();
              _context21.next = 3;
              return store.dispatch(stubbedGetQuizEntryCall());

            case 3:
              (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWith("/api/quizzes/".concat(quizId, "/quiz_entries/").concat(quizEntryId));

            case 4:
            case "end":
              return _context21.stop();
          }
        }
      }, _callee21);
    })));
  });
  describe('removeStimulusQuizEntry', function () {
    var quizId = '7';
    var quizEntry = {
      id: '1',
      pointsPossible: '5'
    };
    var childEntries = [{
      id: '555'
    }, {
      id: '666'
    }];

    var stubbedRemoveQuizEntryCall = function stubbedRemoveQuizEntryCall() {
      var response = (0, _jsonify.jsoniblePromise)(null);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizEntries2.removeStimulusQuizEntry)(quizId, quizEntry, childEntries);
    };

    it('should hit the right endpoint',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee22() {
      var store, url, params;
      return _regenerator.default.wrap(function _callee22$(_context22) {
        while (1) {
          switch (_context22.prev = _context22.next) {
            case 0:
              store = mockStore();
              _context22.next = 3;
              return store.dispatch(stubbedRemoveQuizEntryCall());

            case 3:
              url = "/api/quizzes/".concat(quizId, "/quiz_entries/").concat(quizEntry.id, "/stimuli");
              params = {
                body: JSON.stringify({
                  child_ids: childEntries.map(function (e) {
                    return e.id;
                  })
                })
              };
              (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWithMatch(url, (0, _objectSpread2.default)({
                method: 'DELETE'
              }, params));

            case 6:
            case "end":
              return _context22.stop();
          }
        }
      }, _callee22);
    })));
  });
  describe('moveImmutableQuizEntry', function () {
    var _Map, _Map2;

    var quizId = '3';
    var itemId = '13';
    var stimulusId = '15';
    var quizEntryId = '42';
    var stimulusQuizEntryId = '43';
    var state = (0, _immutable.Map)({
      quizzes: (0, _immutable.Map)({
        activeQuizId: quizId
      }),
      items: (0, _immutable.Map)((_Map = {}, (0, _defineProperty2.default)(_Map, itemId, new _Item.default({
        id: itemId,
        status: 'immutable'
      })), (0, _defineProperty2.default)(_Map, stimulusId, new _Stimulus.default({
        id: stimulusId
      })), _Map)),
      quizEntries: (0, _immutable.Map)((_Map2 = {}, (0, _defineProperty2.default)(_Map2, quizEntryId, new _QuizEntry.default({
        id: quizEntryId,
        entryId: itemId,
        entryType: 'Item',
        position: 1
      })), (0, _defineProperty2.default)(_Map2, stimulusQuizEntryId, new _QuizEntry.default({
        id: stimulusQuizEntryId,
        entryId: stimulusId,
        entryType: 'Stimulus',
        position: 2
      })), _Map2))
    });
    var position = 3;
    var newItemId = '16';
    var createItemStub = sinon.stub();
    var updateStub = sinon.stub();

    var fakeMoveImmutableQuizEntry = function fakeMoveImmutableQuizEntry() {
      return (0, _quizEntries2.moveImmutableQuizEntry)(quizId, position, stimulusQuizEntryId, quizEntryId, createItemStub, updateStub);
    };

    beforeEach(function () {
      createItemStub.returns(function () {
        return Promise.resolve({
          id: newItemId
        });
      });
      updateStub.returns(function () {
        return Promise.resolve();
      });
    });
    afterEach(function () {
      createItemStub.reset();
      updateStub.reset();
    });
    it('moves the entry',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee23() {
      var store, moveAction;
      return _regenerator.default.wrap(function _callee23$(_context23) {
        while (1) {
          switch (_context23.prev = _context23.next) {
            case 0:
              store = mockStore(state);
              _context23.next = 3;
              return store.dispatch(fakeMoveImmutableQuizEntry());

            case 3:
              moveAction = (0, _quizEntries.moveQuizEntry)(quizEntryId, position, stimulusQuizEntryId);
              (0, _chai.expect)(store.getActions()).to.deep.include(moveAction);

            case 5:
            case "end":
              return _context23.stop();
          }
        }
      }, _callee23);
    })));
    it('creates a new item',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee24() {
      var store;
      return _regenerator.default.wrap(function _callee24$(_context24) {
        while (1) {
          switch (_context24.prev = _context24.next) {
            case 0:
              store = mockStore(state);
              _context24.next = 3;
              return store.dispatch(fakeMoveImmutableQuizEntry());

            case 3:
              (0, _chai.expect)(createItemStub).to.have.been.calledWithMatch(function (item) {
                return item instanceof _Item.default;
              }, quizId, 'quiz', itemId);

            case 4:
            case "end":
              return _context24.stop();
          }
        }
      }, _callee24);
    })));
    it('associates the quiz entry with the new item and moves it',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee25() {
      var store;
      return _regenerator.default.wrap(function _callee25$(_context25) {
        while (1) {
          switch (_context25.prev = _context25.next) {
            case 0:
              store = mockStore(state);
              _context25.next = 3;
              return store.dispatch(fakeMoveImmutableQuizEntry());

            case 3:
              (0, _chai.expect)(updateStub).to.have.been.calledWithMatch(quizId, quizEntryId, {
                entryId: newItemId,
                position: position,
                stimulusQuizEntryId: stimulusQuizEntryId
              }, {
                handleResponse: false
              });

            case 4:
            case "end":
              return _context25.stop();
          }
        }
      }, _callee25);
    })));
    it('re-fetches all quiz entries if createItem fails',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee26() {
      var store;
      return _regenerator.default.wrap(function _callee26$(_context26) {
        while (1) {
          switch (_context26.prev = _context26.next) {
            case 0:
              createItemStub.returns(function () {
                return Promise.reject();
              });
              store = mockStore(state);
              _context26.next = 4;
              return (0, _chai.expect)(store.dispatch(fakeMoveImmutableQuizEntry())).to.be.rejected();

            case 4:
              (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWithMatch("/api/quizzes/".concat(quizId, "/quiz_entries"), {
                method: 'GET'
              });

            case 5:
            case "end":
              return _context26.stop();
          }
        }
      }, _callee26);
    })));
    it('re-fetches all quiz entries if updateQuizEntry fails',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee27() {
      var store;
      return _regenerator.default.wrap(function _callee27$(_context27) {
        while (1) {
          switch (_context27.prev = _context27.next) {
            case 0:
              updateStub.returns(function () {
                return Promise.reject();
              });
              store = mockStore(state);
              _context27.next = 4;
              return (0, _chai.expect)(store.dispatch(fakeMoveImmutableQuizEntry())).to.be.rejected();

            case 4:
              (0, _chai.expect)(_Fetcher.default.fakeFetch).to.have.been.calledWithMatch("/api/quizzes/".concat(quizId, "/quiz_entries"), {
                method: 'GET'
              });

            case 5:
            case "end":
              return _context27.stop();
          }
        }
      }, _callee27);
    })));
  });
});