"use strict";

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

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

var _forEach = _interopRequireDefault(require("lodash/forEach"));

var _immutable = require("immutable");

var _recordTests = require("../../../../tests/util/recordTests");

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

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

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

var _QuizSession = _interopRequireDefault(require("../QuizSession"));

var _Quiz = _interopRequireDefault(require("../Quiz"));

var _QuizSessionResult = _interopRequireDefault(require("../QuizSessionResult"));

var _QuizSessionEvent = _interopRequireDefault(require("../QuizSessionEvent"));

var _Response = _interopRequireDefault(require("../Response"));

describe('QuizSession Record', function () {
  describe('#constructor', function () {
    var defaultsToCheck = {
      id: '0'
    };
    (0, _recordTests.defaultChecks)(_QuizSession.default, defaultsToCheck);
  });
  describe('relationships', function () {
    var quiz = {
      id: '1'
    };
    var sessionItem = {
      position: '2'
    };
    var quizSessionResult = {
      position: '4'
    };
    var resumeData = {
      id: '1_22'
    };
    var exampleState = {
      quizzes: (0, _defineProperty2.default)({}, quiz.id, quiz),
      sessionItems: (0, _defineProperty2.default)({}, sessionItem.position, sessionItem),
      quizSessionResults: (0, _defineProperty2.default)({}, quizSessionResult.position, quizSessionResult),
      taking: {
        responses: {
          '1_22': resumeData
        }
      }
    };
    describe('#getQuiz', function () {
      var options = {
        RelationRecordType: _Quiz.default,
        relationAttrs: quiz,
        recordAttributes: {
          quiz: quiz.id
        },
        necessaryState: (0, _immutable.fromJS)(exampleState)
      };
      (0, _recordTests.singleRelationCheck)(_QuizSession.default, 'getQuiz', options);
    });
    describe('#findAuthoritativeResult', function () {
      var options = {
        RelationRecordType: _QuizSessionResult.default,
        relationAttrs: quizSessionResult,
        recordAttributes: {
          authoritativeResult: quiz.id
        },
        necessaryState: (0, _immutable.fromJS)(exampleState)
      };
      (0, _recordTests.singleRelationCheck)(_QuizSession.default, 'findAuthoritativeResult', options);
    });
    describe('#getSessionItems', function () {
      var options = {
        RelationRecordType: _SessionItem.default,
        relationAttrs: sessionItem,
        recordAttributes: {
          sessionItems: [sessionItem.position]
        },
        necessaryState: (0, _immutable.fromJS)(exampleState)
      };
      (0, _recordTests.manyRelationCheck)(_QuizSession.default, 'getSessionItems', options);
    });
    describe('#getQuizSessionResults', function () {
      var options = {
        RelationRecordType: _QuizSessionResult.default,
        relationAttrs: quizSessionResult,
        recordAttributes: {
          quizSessionResults: [quizSessionResult.position]
        },
        necessaryState: (0, _immutable.fromJS)(exampleState)
      };
      (0, _recordTests.manyRelationCheck)(_QuizSession.default, 'getQuizSessionResults', options);
    });
    describe('#getEvents', function () {
      it('returns records for event data matching the session id', function () {
        var _fromJS;

        var dateA = '2016-09-28T18:45:29.929Z|462090529';
        var dateB = '2015-05-28T18:45:29.929Z|462090522';
        var dateC = '2014-01-28T18:45:29.929Z|462090521';
        var quizSessionId = '22';
        var otherQuizSessionId = '44';
        var eventData = (0, _immutable.fromJS)((_fromJS = {}, (0, _defineProperty2.default)(_fromJS, quizSessionId, [{
          createdAt: dateA,
          quizSessionId: quizSessionId
        }, {
          createdAt: dateB,
          quizSessionId: quizSessionId
        }]), (0, _defineProperty2.default)(_fromJS, otherQuizSessionId, [{
          createdAt: dateC,
          quizSessionId: otherQuizSessionId
        }]), _fromJS));
        var fakeState = (0, _immutable.fromJS)({
          quizSessionEvents: eventData
        });
        (0, _reduxStore.setFakeStore)(fakeState);
        var quizSession = new _QuizSession.default({
          id: quizSessionId
        });
        var eventRecords = quizSession.getEvents();
        var expectedRecords = (0, _immutable.fromJS)([new _QuizSessionEvent.default({
          createdAt: dateA,
          quizSessionId: quizSessionId
        }), new _QuizSessionEvent.default({
          createdAt: dateB,
          quizSessionId: quizSessionId
        })]);
        expect((0, _immutable.is)(eventRecords, expectedRecords)).to.be.true();
        (0, _reduxStore.unsetFakeStore)();
      });
    });
    describe('#getResumeData', function () {
      it('returns the proper resume data', function () {
        var options = {
          RelationRecordType: _Response.default,
          relationAttrs: resumeData,
          recordAttributes: {
            resumeData: ['1_22']
          },
          necessaryState: (0, _immutable.fromJS)(exampleState)
        };
        (0, _recordTests.manyRelationCheck)(_Response.default, 'getResumeData', options);
      });
    });
  });
  describe('#pins', function () {
    it('returns the pins from takingData', function () {
      var qs = new _QuizSession.default((0, _immutable.fromJS)({
        takingData: {
          pins: [1]
        }
      }));
      expect(qs.pins()).to.be.eql((0, _immutable.List)([1]));
    });
    it('returns an empty List if no takingData', function () {
      var qs = new _QuizSession.default({});
      expect(qs.pins()).to.be.eql((0, _immutable.List)([]));
    });
  });
  describe('#userDisplayName', function () {
    var fullName = 'Some Full Name';
    var givenName = 'SomeGivenName';
    var email = 'some-email@email.edu';
    it('displays the fullName if it has it', function () {
      var user = (0, _immutable.Map)({
        fullName: fullName,
        givenName: givenName,
        email: email
      });
      var qs = new _QuizSession.default({
        user: user
      }, {});
      expect(qs.userDisplayName()).to.be.equal(fullName);
    });
    it('displays the givenName if there is no full name', function () {
      var user = (0, _immutable.Map)({
        givenName: givenName,
        email: email
      });
      var qs = new _QuizSession.default({
        user: user
      }, {});
      expect(qs.userDisplayName()).to.be.equal(givenName);
    });
    it('displays the email if there is no full or given name', function () {
      var user = (0, _immutable.Map)({
        email: email
      });
      var qs = new _QuizSession.default({
        user: user
      }, {});
      expect(qs.userDisplayName()).to.be.equal(email);
    });
    it('displays empty string if no user is given', function () {
      var qs = new _QuizSession.default(null, {});
      expect(qs.userDisplayName()).to.be.empty();
    });
  });
  describe('#isOneQuestionAtATime', function () {
    it('returns true if not null value for oneAtATimeType', function () {
      var qs = new _QuizSession.default({
        oneAtATimeType: 'question'
      });
      expect(qs.isOneQuestionAtATime()).to.eq(true);
    });
    it('returns false if null value for oneAtATimeType', function () {
      var qs = new _QuizSession.default({
        oneAtATimeType: null
      });
      expect(qs.isOneQuestionAtATime()).to.eq(false);
    });
  });
  describe('status helpers', function () {
    describe('#isLoaded', function () {
      it('should be loaded if id is not "0"', function () {
        var qs = new _QuizSession.default({
          id: '1'
        }, {});
        expect(qs.isLoaded()).to.be.true();
      });
      it('should not be loaded if id is "0"', function () {
        var qs = new _QuizSession.default({
          id: '0'
        }, {});
        expect(qs.isLoaded()).to.be.false();
      });
    });
    var helperStatusMap = {
      pending: _constants.PENDING,
      starting: _constants.STARTING,
      creatingSessionItems: _constants.CREATING_SESSION_ITEMS,
      failedToStart: _constants.FAILED_TO_START,
      inProgress: _constants.IN_PROGRESS,
      reopeningGraded: _constants.REOPENING_GRADED,
      creatingResponses: _constants.CREATING_RESPONSES,
      needsGrading: _constants.NEEDS_GRADING,
      autoGrading: _constants.AUTOGRADING,
      failed: _constants.FAILURE,
      graded: _constants.GRADED
    };
    (0, _forEach.default)(helperStatusMap, function (status, check) {
      describe(check, function () {
        it("returns true if status is ".concat(status), function () {
          var qs = new _QuizSession.default({
            status: status
          }, {});
          expect(qs[check]()).to.be.true();
        });
      });
      it('returns false if status is invalid', function () {
        var qs = new _QuizSession.default({
          status: 'invalid'
        }, {});
        expect(qs[check]()).to.be.false();
      });
    });
    describe('taking', function () {
      ['starting', 'creating_session_items', 'in_progress', 'reopening_graded'].forEach(function (status) {
        it("returns true if status is ".concat(status), function () {
          var qs = new _QuizSession.default({
            status: status
          }, {});
          expect(qs.taking()).to.be.true();
        });
      });
      it('returns false if status is invalid', function () {
        var qs = new _QuizSession.default({
          status: 'invalid'
        }, {});
        expect(qs.taking()).to.be.false();
      });
    });
    describe('submitted', function () {
      ['creating_responses', 'autograding', 'needs_grading', 'graded', 'failure'].forEach(function (status) {
        it("returns true if status is ".concat(status), function () {
          var qs = new _QuizSession.default({
            status: status
          }, {});
          expect(qs.submitted()).to.be.true();
        });
      });
      it('returns false if status is invalid', function () {
        var qs = new _QuizSession.default({
          status: 'invalid'
        }, {});
        expect(qs.submitted()).to.be.false();
      });
    });
  });
});