"use strict";

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

var _immutable = require("immutable");

var _reactDom = _interopRequireDefault(require("react-dom"));

var _Spinner = _interopRequireDefault(require("../../../../../common/components/shared/spinner/Spinner"));

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

var _QuizAnalysis = _interopRequireDefault(require("../../../../../common/records/QuizAnalysis"));

var _ItemAnalysis = _interopRequireDefault(require("../../../../../common/records/ItemAnalysis"));

var _presenter = _interopRequireDefault(require("../presenter"));

var _withContext = _interopRequireDefault(require("../../../../../../tests/util/withContext"));

var _MetricsListItem = _interopRequireDefault(require("../../../MetricsListItem"));

describe('QuizAndItemAnalysis', function () {
  var activeQuizId = '1';
  var getItemAnalysesStub = sinon.stub();
  var getQuizAnalysisStub = sinon.stub();
  var getExistingQuizStub = sinon.stub();
  var toggleItemAnalysisRowExpandedStub = sinon.stub();
  var quiz = new _Quiz.default({
    id: activeQuizId,
    title: 'Test Quiz'
  });
  var itemAnalysis1 = new _ItemAnalysis.default((0, _immutable.fromJS)({
    itemId: '3',
    itemBody: 'This is the item body',
    itemAnalysis: {
      scoreInfo: {
        resultSum: 3,
        resultCount: 1,
        scoreStddevPop: 1.2
      },
      correctAnswerInfo: {
        resultSum: 50,
        resultCount: 8,
        appearedCount: 20
      },
      topCorrectAnswerInfo: {
        resultCount: 8,
        appearedCount: 20
      },
      bottomCorrectAnswerInfo: {
        resultCount: 2,
        appearedCount: 20
      },
      incorrectAnswerInfo: {
        resultSum: 5,
        resultCount: 8
      },
      unansweredInfo: 1,
      difficultyInfo: {
        responseCount: 8,
        correctAnswerCount: 1
      },
      choiceInfo: [{
        choiceCount: 2,
        id: 'id',
        itemBody: '<p>Item&gt;Body</p>',
        correct: true
      }, {
        choiceCount: 3,
        id: 'not_id',
        itemBody: 'choice Item Body not correct',
        correct: false
      }, {
        choiceCount: 4,
        id: 'not_id_again',
        itemBody: 'choice Item Body not correct still',
        correct: false
      }],
      quintile: [1, 2, 3, 4, 5]
    }
  }));
  var itemAnalysis2 = new _ItemAnalysis.default((0, _immutable.fromJS)({
    itemId: '5',
    itemBody: 'This is another item body',
    itemAnalysis: {
      scoreInfo: {
        resultSum: 3,
        resultCount: 1,
        scoreStddevPop: 1.2
      },
      correctAnswerInfo: {
        resultSum: 50,
        resultCount: 8,
        appearedCount: 20
      },
      topCorrectAnswerInfo: {
        resultCount: 8,
        appearedCount: 20
      },
      bottomCorrectAnswerInfo: {
        resultCount: 2,
        appearedCount: 20
      },
      incorrectAnswerInfo: {
        resultSum: 5,
        resultCount: 8
      },
      unansweredInfo: 1,
      difficultyInfo: {
        responseCount: 8,
        correctAnswerCount: 1
      },
      choiceInfo: [{
        choiceCount: 2,
        id: 'id',
        itemBody: '<p>Item&gt;Body</p>',
        correct: true
      }, {
        choiceCount: 3,
        id: 'not_id',
        itemBody: 'choice Item Body not correct',
        correct: false
      }, {
        choiceCount: 4,
        id: 'not_id_again',
        itemBody: 'choice Item Body not correct still',
        correct: false
      }],
      quintile: [1, 2, 1, 3, 4]
    }
  }));
  var quizAnalysis = new _QuizAnalysis.default((0, _immutable.fromJS)({
    quizId: '1',
    quizAnalysis: {
      centile: Array.from({
        length: 101
      }, function (v, i) {
        return i / 100;
      }),
      decile: Array.from({
        length: 10
      }, function (v, i) {
        return i;
      }),
      scoreInfo: {
        resultSum: 5,
        resultCount: 1,
        scoreMax: 100,
        scoreMin: 2,
        scoreSum: 1000,
        scoreCount: 40,
        pointsPossible: 100,
        scoreStddevPop: 20
      },
      cronbachsAlpha: 5
    },
    batchTimestamp: new Date('Thu Feb 07 2019 15:35:17 GMT-0500 (Eastern Standard Time)')
  }));

  quiz.getItemAnalyses = function () {
    return new _immutable.List([itemAnalysis1, itemAnalysis2]);
  };

  quiz.getQuizAnalysis = function () {
    return quizAnalysis;
  };

  var QuizAndItemAnalysisWithContext = (0, _withContext.default)(_presenter.default);
  var testbed = createTestbed(QuizAndItemAnalysisWithContext, {
    activeQuizId: activeQuizId,
    getExistingQuiz: getExistingQuizStub,
    getItemAnalyses: getItemAnalysesStub,
    getQuizAnalysis: getQuizAnalysisStub,
    quiz: quiz,
    quizAnalysis: quizAnalysis,
    toggleItemAnalysisRowExpanded: toggleItemAnalysisRowExpandedStub
  });
  shouldRenderComponent(testbed, {}, QuizAndItemAnalysisWithContext);
  it('should render spinner if quiz has not loaded', function () {
    testbed.render({
      quiz: new _Quiz.default()
    });
    var spinner = testbed.findChildrenByType(_Spinner.default)[0];
    expect(spinner).to.exist();
  });
  it('should render not found card if quiz analysis is not found', function () {
    testbed.render({
      quizAnalysisNotFound: true
    });
    expect(testbed.getText()).to.include('No reports yet');
  });
  it('should trigger reload of data if quizId changes', function () {
    testbed.render({});
    expect(getExistingQuizStub.calledWith('1')).to.be.true();
    testbed.render({
      activeQuizId: '2'
    });
    expect(getExistingQuizStub.calledWith('2')).to.be.true();
  });
  describe('Quiz and Item analysis metrics', function () {
    it('renders all metrics with none specified', function () {
      testbed.render({});
      var listItems = testbed.findChildrenByType(_MetricsListItem.default); // 6 for the quiz, 4 for each item = 14

      expect(listItems.length).to.eq(14);
    });
    it('only shows requested item metrics, when requested', function () {
      testbed.render({
        itemMetrics: ['rpb', 'difficultyIndex']
      });
      var listItems = testbed.findChildrenByType(_MetricsListItem.default); // 6 for the quiz, 2 for each item = 10

      expect(listItems.length).to.eq(10);
      listItems.forEach(function (item) {
        var label = _reactDom.default.findDOMNode(item).innerText;

        expect(label).not.to.contain('Discrimination Index');
        expect(label).not.to.contain('Mean Earned Score');
      });
    });
    it('only shows requested quiz metrics, when requested', function () {
      testbed.render({
        quizMetrics: ['highScore', 'lowScore', 'meanScore']
      });
      var listItems = testbed.findChildrenByType(_MetricsListItem.default); // 3 for the quiz, 4 for each item = 11

      expect(listItems.length).to.eq(11);
      listItems.forEach(function (item) {
        var label = _reactDom.default.findDOMNode(item).innerText;

        expect(label).not.to.contain('Standard Deviation');
        expect(label).not.to.contain('Mean Elapsed Time');
        expect(label).not.to.contain('Cronbach\'s Alpha');
      });
    });
  });
});