"use strict";

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

var _chai = require("chai");

var _immutable = require("immutable");

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

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

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

var _QtiImport = _interopRequireDefault(require("../QtiImport"));

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

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

describe('Quiz Record', function () {
  describe('#constructor', function () {
    it('defaults shuffleQuestions to false', function () {
      (0, _chai.expect)(new _Quiz.default().shuffleQuestions).to.be.false();
    });
  });
  describe('isOneQuestionAtATime', function () {
    it('is true when quiz has oneAtATimeType "question"', function () {
      (0, _chai.expect)(new _Quiz.default({
        oneAtATimeType: 'question'
      }).isOneQuestionAtATime()).to.be.true();
    });
    it('is false when quiz does not have oneAtATimeType "question"', function () {
      (0, _chai.expect)(new _Quiz.default({
        oneAtATimeType: null
      }).isOneQuestionAtATime()).to.be.false();
    });
  });
  describe('#getTimeLimit', function () {
    context('hasTimeLimit not set', function () {
      it('returns 0', function () {
        var quiz = new _Quiz.default({
          hasTimeLimit: false,
          sessionTimeLimitInSeconds: 100
        });
        (0, _chai.expect)(quiz.getTimeLimit()).to.eq(0);
      });
    });
    context('hasTimeLimit set', function () {
      context('sessionTimeLimitInSeconds set', function () {
        it('returns the correct time limit', function () {
          var quiz = new _Quiz.default({
            hasTimeLimit: true,
            sessionTimeLimitInSeconds: 100
          });
          (0, _chai.expect)(quiz.getTimeLimit()).to.eq(100);
        });
      });
      context('sessionTimeLimitInSeconds not set', function () {
        it('returns null', function () {
          var quiz = new _Quiz.default({
            hasTimeLimit: true
          });
          (0, _chai.expect)(quiz.getTimeLimit()).to.eq(null);
        });
      });
    });
  });
  describe('#isLoaded', function () {
    it('returns true if id is not \'0\'', function () {
      var quiz = new _Quiz.default({
        id: '1'
      });
      (0, _chai.expect)(quiz.isLoaded()).to.be.true();
    });
    it('returns false if id is \'0\'', function () {
      var quiz = new _Quiz.default({
        id: '0'
      });
      (0, _chai.expect)(quiz.isLoaded()).to.be.false();
    });
  });
  describe('#isImporting', function () {
    it('returns true if status is "importing"', function () {
      var quiz = new _Quiz.default({
        status: 'importing'
      });
      (0, _chai.expect)(quiz.isImporting()).to.be.true();
    });
    it('returns false otherwise', function () {
      var quiz = new _Quiz.default();
      (0, _chai.expect)(quiz.isImporting()).to.be.false();
    });
  });
  describe('#canImport', function () {
    it('returns true if status is "uninitialized" and there are no quizEntries', function () {
      var quiz = new _Quiz.default({
        status: 'uninitialized',
        quizEntries: (0, _immutable.List)()
      });
      (0, _chai.expect)(quiz.canImport()).to.be.true();
    });
    it('returns false if status is not "uninitialized"', function () {
      var quiz = new _Quiz.default({
        status: 'unpublished',
        quizEntries: []
      });
      (0, _chai.expect)(quiz.canImport()).to.be.false();
    });
    it('returns false a quizEntry is associated to it', function () {
      var quiz = new _Quiz.default({
        status: 'uninitialized',
        quizEntries: ['2']
      });
      (0, _chai.expect)(quiz.canImport()).to.be.false();
    });
  });
  describe('#getWorkingInstance', function () {
    it('merges the right modifications into itself', function () {
      var modifiedTitle = 'Quick Quiz 1';
      (0, _reduxStore.setFakeStore)((0, _immutable.fromJS)({
        modifications: {
          quizzes: {
            22: {
              id: '22',
              title: modifiedTitle
            }
          }
        }
      }));
      var quiz = new _Quiz.default({
        id: '22'
      });
      (0, _chai.expect)(quiz.getWorkingInstance().title).to.be.equal(modifiedTitle);
      (0, _reduxStore.unsetFakeStore)();
    });
  });
  describe('relationships', function () {
    var quizId = '1';
    describe('#getQuizSessions', function () {
      var exampleState = {
        quizSessions: {
          3: {
            id: '3',
            quiz: quizId
          }
        }
      };
      var options = {
        RelationRecordType: _QuizSession.default,
        relationAttrs: {
          id: '3',
          quiz: quizId
        },
        recordAttributes: {
          id: quizId
        },
        necessaryState: (0, _immutable.fromJS)(exampleState)
      };
      (0, _recordTests.manyRelationCheck)(_Quiz.default, 'getQuizSessions', options);
    });
    describe('#getQuizSessionsForUser', function () {
      it('only returns sessions that match the given id', function () {
        var userId = '42';
        var goodSessionAttrs = {
          id: '3',
          quiz: quizId,
          user: {
            id: userId
          }
        };
        var badSessionAttrs = {
          id: '4',
          quiz: quizId,
          user: {
            id: '999'
          }
        };
        var quizSessionsForUser = (0, _immutable.List)([goodSessionAttrs, badSessionAttrs].map(function (sessionData) {
          return new _QuizSession.default((0, _immutable.fromJS)(sessionData));
        }));
        var quiz = new _Quiz.default({
          id: quizId
        });
        sinon.stub(quiz, 'getQuizSessions').returns(quizSessionsForUser);
        var foundSessionsForUser = quiz.getQuizSessionsForUser(userId);
        (0, _chai.expect)(foundSessionsForUser.size).to.be.equal(1);
        (0, _chai.expect)((0, _immutable.is)(quizSessionsForUser.first(), foundSessionsForUser.first())).to.be.true();
      });
    });
    describe('#getQuizEntries', function () {
      var qiAttrs = {
        id: '3',
        entry: '2',
        quiz: quizId
      };
      var exampleState = {
        quizEntries: {
          3: qiAttrs
        }
      };
      var options = {
        RelationRecordType: _QuizEntry.default,
        relationAttrs: qiAttrs,
        recordAttributes: {
          id: quizId,
          quizEntries: ['3']
        },
        necessaryState: (0, _immutable.fromJS)(exampleState)
      };
      (0, _recordTests.manyRelationCheck)(_Quiz.default, 'getQuizEntries', options);
    });
    describe('#getLatestImport', function () {
      var laterImportData = {
        parentId: quizId,
        parentType: 'Quiz',
        id: '2'
      };
      var exampleState = {
        qtiImports: {
          1: {
            parentId: quizId,
            parentType: 'Quiz',
            id: '1'
          },
          2: laterImportData
        }
      };
      var options = {
        RelationRecordType: _QtiImport.default,
        relationAttrs: laterImportData,
        recordAttributes: {
          id: quizId
        },
        necessaryState: (0, _immutable.fromJS)(exampleState)
      };
      (0, _recordTests.singleRelationCheck)(_Quiz.default, 'getLatestImport', options);
    });
  });
  describe('#isOneQuestionAtATime', function () {
    it('returns true if not null value for oneAtATimeType', function () {
      var quiz = new _Quiz.default({
        oneAtATimeType: 'question'
      });
      (0, _chai.expect)(quiz.isOneQuestionAtATime()).to.eq(true);
    });
    it('returns false if null value for oneAtATimeType', function () {
      var quiz = new _Quiz.default({
        oneAtATimeType: null
      });
      (0, _chai.expect)(quiz.isOneQuestionAtATime()).to.eq(false);
    });
  });
});