"use strict";

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

var _chai = require("chai");

var _immutable = require("immutable");

var _persistedStorageMiddleware = _interopRequireDefault(require("../persistedStorageMiddleware"));

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

describe('persistedStorageMiddleware', function () {
  var stubNext = sinon.stub();

  var testNextAction = function testNextAction(dispatch, action) {
    it('returns the result of next(action)', function () {
      stubNext.resetHistory();
      stubNext.returns(action);
      var returned = dispatch(action);
      (0, _chai.expect)(stubNext.calledWith(action)).to.be.true();
      (0, _chai.expect)(returned).to.be.equal(action);
    });
  };

  context('storing in local storage', function () {
    var setStub = sinon.stub();
    var fakeLocalStore = {
      enabled: true,
      set: setStub
    };
    var currentSessionItemPosition = 22;
    var activeQuizSessionId = '44';
    var fakeAppStore = {
      getState: function getState() {
        return (0, _immutable.fromJS)({
          taking: {
            currentSessionItemPosition: currentSessionItemPosition
          },
          quizSessions: {
            activeQuizSessionId: activeQuizSessionId
          }
        });
      }
    };
    var persistedStorageMiddleware = (0, _persistedStorageMiddleware.default)(void 0, fakeLocalStore);
    var dispatch = persistedStorageMiddleware(fakeAppStore)(stubNext);
    context('action needs to persistTakingInfo', function () {
      var checkTakingDataPersisted = function checkTakingDataPersisted(type) {
        context("action of type: ".concat(type), function () {
          it('saves activeQuizSessionId', function () {
            var action = {
              type: type
            };
            setStub.resetHistory();
            dispatch(action);
            (0, _chai.expect)(setStub.calledWith(_constants.CACHED_SESSION_ID, activeQuizSessionId)).to.be.true();
          });
          it('saves currentSessionItemPosition', function () {
            var action = {
              type: type
            };
            setStub.resetHistory();
            dispatch(action);
            (0, _chai.expect)(setStub.calledWith(_constants.ACTIVE_QUESTION, currentSessionItemPosition)).to.be.true();
          });
        });
      };

      var actionTypes = [_constants.ACTIVE_QUIZ_SESSION_CHANGE, _constants.BACKTRACKING_NEXT_QUESTION, _constants.PREVIOUS_QUESTION, _constants.SET_CURRENT_SESSION_ITEM_POSITION];
      actionTypes.forEach(checkTakingDataPersisted);
    });
  });
  context('storage is not enabled', function () {
    var fakeSessionStore = {
      enabled: false
    };
    var fakeLocalStore = {
      enable: false
    };
    var persistedStorageMiddleware = (0, _persistedStorageMiddleware.default)(fakeSessionStore, fakeLocalStore);
    var dispatch = persistedStorageMiddleware()(stubNext);
    testNextAction(dispatch);
  });
  context('action type is unknown', function () {
    var persistedStorageMiddleware = (0, _persistedStorageMiddleware.default)();
    var dispatch = persistedStorageMiddleware()(stubNext);
    var dummyAction = {
      type: 'dummy action'
    };
    testNextAction(dispatch, dummyAction);
  });
});