"use strict";

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

var _immutable = require("immutable");

var _chai = require("chai");

var _taking = _interopRequireDefault(require("../taking"));

var _taking2 = require("../../actions/taking");

var _quizSessions = require("../../actions/quizSessions");

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

var _SessionItem = _interopRequireDefault(require("../../records/SessionItem"));

var sessionItem = new _SessionItem.default({
  position: 1,
  item: {
    id: '1'
  },
  currentSessionItemPosition: 1
});
describe('taking Reducer', function () {
  context('sessionIdForCurrentItem equal to payload', function () {
    it('ACTIVE_QUIZ_SESSION_CHANGE does not change taking state', function () {
      var initialState = (0, _immutable.fromJS)({
        currentSessionItemPosition: 3,
        sessionIdForCurrentItem: 7
      });
      var action = (0, _quizSessions.newActiveQuizSession)(7);
      (0, _chai.expect)((0, _taking.default)(initialState, action)).to.eq(initialState);
    });
  });
  context('sessionIdForCurrentItem not equal to payload', function () {
    it('ACTIVE_QUIZ_SESSION_CHANGE clears taking state', function () {
      var initialState = (0, _immutable.fromJS)({
        currentSessionItemPosition: 3,
        sessionIdForCurrentItem: 5
      });
      var action = (0, _quizSessions.newActiveQuizSession)(7);
      (0, _chai.expect)((0, _taking.default)(initialState, action).isEmpty()).to.be.true();
    });
  });
  it('handles UPDATE_TIMER_MINIMIZED', function () {
    var initialState = (0, _immutable.fromJS)({
      timerData: {
        timerMinimized: false
      }
    });
    var action = (0, _taking2.updateTimerMinimized)(true);
    (0, _chai.expect)((0, _taking.default)(initialState, action).getIn(['timerData', 'timerMinimized'])).to.be.true();
  });
  it('handles UPDATE_TIMER_MINIMIZED', function () {
    var initialState = (0, _immutable.Map)({});
    var action = (0, _taking2.updateQuizSubmitFlag)(true);
    (0, _chai.expect)((0, _taking.default)(initialState, action).get('submitFlag')).to.be.true();
  });
  it('handles UPDATE_QUIZ_SESSION_STATUS_ON_PAGE_LOAD', function () {
    var initialState = (0, _immutable.fromJS)({
      quizSessionStatusOnPageLoad: _constants.PENDING
    });
    var action = (0, _taking2.updateQuizSessionStatusOnPageLoad)(_constants.IN_PROGRESS);
    (0, _chai.expect)((0, _taking.default)(initialState, action).get('quizSessionStatusOnPageLoad')).to.eq(_constants.IN_PROGRESS);
  });
  it('handles PREVIOUS_QUESTION', function () {
    var initialState = (0, _immutable.fromJS)({
      currentSessionItemPosition: 3
    });
    var action = (0, _taking2.previousQuestion)();
    (0, _chai.expect)((0, _taking.default)(initialState, action).get('currentSessionItemPosition')).to.eq(2);
  });
  it('handles BACKTRACKING_NEXT_QUESTION', function () {
    var initialState = (0, _immutable.fromJS)({
      currentSessionItemPosition: 3
    });
    var action = (0, _taking2.backtrackingNextQuestion)();
    (0, _chai.expect)((0, _taking.default)(initialState, action).get('currentSessionItemPosition')).to.eq(4);
  });
  it('handles SET_CURRENT_SESSION_ITEM_POSITION', function () {
    var initialState = (0, _immutable.fromJS)({
      currentSessionItemPosition: 3
    });
    var action = (0, _taking2.setCurrentSessionItemPosition)(20);
    (0, _chai.expect)((0, _taking.default)(initialState, action).get('currentSessionItemPosition')).to.eq(20);
  });
  it('handles UPDATE_IS_TAKING', function () {
    var initialState = (0, _immutable.Map)({});
    var action = (0, _taking2.updateIsTaking)(true);
    var nextState = (0, _taking.default)(initialState, action);
    (0, _chai.expect)(nextState.get('isTaking')).to.be.true();
  });
  it('handles UPDATE_TIMER_INFO', function () {
    var initialState = (0, _immutable.Map)({});
    var action = (0, _taking2.updateTimerInfo)({
      timerRunning: true,
      timeLeftInSeconds: 3,
      timeRecording: false
    });
    var nextState = (0, _taking.default)(initialState, action);
    (0, _chai.expect)(nextState.get('timerData').toJS()).to.eql({
      timerRunning: true,
      timeLeftInSeconds: 3,
      timeRecording: false
    });
  }); // testrail test_id: C2954596, C2954597

  it('handles SESSION_STARTED_OR_RESUMED_EVENT', function () {
    var initialState = (0, _immutable.Map)({});
    var action = (0, _taking2.sessionStartOrResumeEvent)('userAgent');
    var nextState = (0, _taking.default)(initialState, action);
    (0, _chai.expect)(nextState.get('browserSessionId')).to.have.lengthOf(36);
  });
  it('should add response with RESPONSE action if none exists', function () {
    var initialState = (0, _immutable.Map)({});
    var action = (0, _taking2.editResponse)(sessionItem, 'some arbitrary value');
    var nextState = (0, _taking.default)(initialState, action);
    var nextValue = nextState.getIn(['responses', 1]).get('userResponse');
    (0, _chai.expect)(nextValue.toJS()).to.deep.equal({
      value: 'some arbitrary value'
    });
  });
  it('should replace a response value with RESPONSE action if response exists', function () {
    var initialState = (0, _immutable.fromJS)({
      1: {
        userResponse: 'initial val'
      }
    });
    var action = (0, _taking2.editResponse)(sessionItem, 'a new val');
    var nextState = (0, _taking.default)(initialState, action);
    var nextValue = nextState.getIn(['responses', 1]).get('userResponse');
    (0, _chai.expect)(nextValue.toJS()).to.deep.equal({
      value: 'a new val'
    });
  });
  it('should add multiple responses when loading resume data', function () {
    var initialState = (0, _immutable.Map)({});
    var respOne = {
      position: 1,
      userResponse: 'hey',
      itemId: '1'
    };
    var respTwo = {
      position: 2,
      userResponse: 'hello',
      itemId: '2'
    };
    var loadResumeAction = (0, _taking2.loadResumeData)([respOne, respTwo]);
    var nextState = (0, _taking.default)(initialState, loadResumeAction);
    var nextValue = nextState.getIn(['responses', 2]).get('userResponse');
    (0, _chai.expect)(nextValue).to.equal('hello');
  });
  it('returns the original state with bad action', function () {
    var initialState = (0, _immutable.Map)({
      foo: 'bar'
    });
    var action = (0, _immutable.Map)({
      type: 'something else'
    });
    var nextState = (0, _taking.default)(initialState, action);
    (0, _chai.expect)(nextState).to.equal(initialState);
  });
});