"use strict";

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

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

var _immutable = require("immutable");

var _Progress = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Progress"));

var _Alert = _interopRequireDefault(require("@instructure/ui-alerts/lib/components/Alert"));

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

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

var _index = _interopRequireDefault(require("../index"));

var _FormattedDuration = _interopRequireDefault(require("../../../../shared/FormattedDuration"));

var styles = {
  componentId: 'dZbOF',
  template: function template(theme) {
    return "\n\n.Info__styles__alertWrapper {\n  display: flex;\n  flex-direction: row;\n  align-items: center;\n  justify-content: center;\n  margin-top: ".concat(theme.alertWrapperMargin || 'inherit', ";\n}\n\n.Info__styles__alertWidth {\n  width: 300px;\n}\n\n.Info__styles__wrapper {\n  padding: none;\n  margin: none;\n\n  display: flex;\n\n  align-items: center;\n  justify-content: center;\n}\n\n.Info__styles__info {\n  align-self: center;\n\n  display: flex;\n  flex-wrap: wrap;\n  align-items: center;\n  justify-content: center;\n}\n\n.Info__styles__scoreStats {\n  display: flex;\n  flex-wrap: wrap;\n  align-items: center;\n  justify-content: flex-start;\n  flex: 1 1 auto;\n}\n\n.Info__styles__score {\n  display: flex;\n  align-items: center;\n  flex: 1 1 auto;\n\n  margin: ").concat(theme.scoreMargin || 'inherit', ";\n  height: 150px;\n  width: 150px;\n\n  \n  z-index: ").concat(theme.scoreZIndex || 'inherit', ";\n}\n\n.Info__styles__scorePercentage {\n  font-size: ").concat(theme.scorePercentageFontSize || 'inherit', ";\n  font-weight: ").concat(theme.scorePercentageFontWeight || 'inherit', ";\n}\n\n.Info__styles__infoWrapper {\n  flex: 1 1 auto;\n  margin: ").concat(theme.infoWrapperMargin || 'inherit', ";\n  padding-left: ").concat(theme.inforWrapperPadding || 'inherit', ";\n}\n");
  },
  'alertWrapper': 'Info__styles__alertWrapper',
  'alertWidth': 'Info__styles__alertWidth',
  'wrapper': 'Info__styles__wrapper',
  'info': 'Info__styles__info',
  'scoreStats': 'Info__styles__scoreStats',
  'score': 'Info__styles__score',
  'scorePercentage': 'Info__styles__scorePercentage',
  'infoWrapper': 'Info__styles__infoWrapper'
};
describe('QSRInfo', function () {
  var quizSession = new _QuizSession.default({
    elapsedTime: 3600
  });
  var quizSessionResult = new _QuizSessionResult.default({
    sessionItemResults: (0, _immutable.List)(),
    score: 9,
    pointsPossible: 10,
    percentage: 0.9
  });
  var sessionItemResults = (0, _immutable.fromJS)([{
    scoredData: {
      gradeStatus: 'graded'
    }
  }, {
    scoredData: {
      gradeStatus: 'waiting'
    }
  }, {
    scoredData: {
      gradeStatus: 'waiting'
    }
  }]);
  var testbed = createTestbed(_index.default, {
    lang: 'en',
    quizSessionResult: quizSessionResult,
    quizSession: quizSession,
    sessionItemResults: sessionItemResults
  });
  describe('score display', function () {
    context('unknown value', function () {
      var resultWithoutScore = new _QuizSessionResult.default({
        pointsPossible: 10
      });
      var resultWithoutPointsPossible = new _QuizSessionResult.default({
        score: 10
      });
      it('is hidden if score is unknown', function () {
        testbed.render({
          quizSessionResult: resultWithoutScore
        });
        var progressComponent = testbed.findChildrenByType(_Progress.default)[0];
        expect(progressComponent).to.be.undefined();
      });
      it('is hidden if points possible is unknown', function () {
        testbed.render({
          quizSessionResult: resultWithoutPointsPossible
        });
        var progressComponent = testbed.findChildrenByType(_Progress.default)[0];
        expect(progressComponent).to.be.undefined();
      });
    });
    context('known value', function () {
      var resultWithPercentage = new _QuizSessionResult.default({
        score: 1,
        pointsPossible: 2,
        percentage: 0.5
      });
      it('shows their percentage as a float', function () {
        testbed.render({
          quizSessionResult: resultWithPercentage
        });
        var progressComponent = testbed.findChildByType(_Progress.default);

        var progressText = _reactDom.default.findDOMNode(progressComponent).textContent;

        expect(progressText).to.include('50%');
      });
      it('shows the percentage as a float in the in the aria-valuetext', function () {
        testbed.render({
          quizSessionResult: resultWithPercentage
        });

        var tag = _reactDom.default.findDOMNode(testbed.findChildByTagName('progress'));

        var ariaText = tag.getAttribute('aria-valuetext');
        expect(ariaText).to.include('Your score was 50%.');
      });
    });
  });
  describe('ungraded question alert', function () {
    var quizSessionWith3Items = quizSession.merge({
      sessionItems: (0, _immutable.fromJS)([{
        position: 1
      }, {
        position: 2
      }, {
        position: 3
      }])
    });
    var quizSessionResultWith1Result = quizSessionResult.merge({
      sessionItemResults: (0, _immutable.fromJS)([{
        position: 1
      }]),
      id: '2'
    });
    it('renders an Alert if there are ungraded questions', function () {
      testbed.render({
        quizSessionResult: quizSessionResultWith1Result,
        quizSession: quizSessionWith3Items
      });
      var alertComponent = testbed.findChildrenByType(_Alert.default)[0];
      expect(alertComponent).to.not.be.undefined();
    });
    it('shows the number of questions needing grading', function () {
      testbed.render({
        quizSessionResult: quizSessionResultWith1Result,
        quizSession: quizSessionWith3Items
      });
      expect(testbed.getText()).to.include('2 questions require grading');
    });
    it('does not render an alert if all questions have been graded', function () {
      var quizSessionResultWith3Results = quizSessionResult.merge({
        sessionItemResults: (0, _immutable.fromJS)([{
          position: 1
        }, {
          position: 2
        }, {
          position: 3
        }]),
        id: '2'
      });
      var sessionItemResults = (0, _immutable.fromJS)([{
        score: 0
      }, {
        score: 1
      }, {
        score: 2
      }]);
      testbed.render({
        quizSessionResult: quizSessionResultWith3Results,
        quizSession: quizSessionWith3Items,
        sessionItemResults: sessionItemResults
      });
      var alertComponent = testbed.findChildrenByType(_Alert.default)[0];
      expect(alertComponent).to.be.undefined();
    });
  });
  describe('render tests', function () {
    context('points display', function () {
      context('score and points possible present', function () {
        it('should display score', function () {
          testbed.render();
          var pointsElem = testbed.findChildrenByClass(styles.info_wrapper)[0];
          expect(testbed.getText(pointsElem)).to.include('9');
        });
        it('should display points possible', function () {
          testbed.render();
          var pointsElem = testbed.findChildrenByClass(styles.info_wrapper)[0];
          expect(testbed.getText(pointsElem)).to.include('Out of 10 points');
        });
        it('should display percentage', function () {
          testbed.render();
          var percentageElem = testbed.findChildrenByClass(styles.score_percentage)[0];
          expect(testbed.getText(percentageElem)).to.include(90);
        });
      });
      context('only score present', function () {
        var resultWithOnlyScore = new _QuizSessionResult.default({
          score: 9
        });
        it('should only display score', function () {
          testbed.render({
            quizSessionResult: resultWithOnlyScore
          });
          var pointsElem = testbed.findChildrenByClass(styles.info_wrapper)[0];
          expect(testbed.getText(pointsElem)).to.include('9 Earned points');
        });
        it('should not display percentage', function () {
          testbed.render({
            quizSessionResult: resultWithOnlyScore
          });
          var percentageElem = testbed.findChildrenByClass(styles.score_percentage)[0];
          expect(percentageElem).to.be.undefined();
        });
      });
      context('only points possible present', function () {
        var resultWithOnlyPointsPossible = new _QuizSessionResult.default({
          pointsPossible: 10
        });
        it('should only display points possible', function () {
          testbed.render({
            quizSessionResult: resultWithOnlyPointsPossible
          });
          var pointsElem = testbed.findChildrenByClass(styles.info_wrapper)[0];
          expect(testbed.getText(pointsElem)).to.include('10 Points possible');
        });
        it('should not display percentage', function () {
          testbed.render({
            quizSessionResult: resultWithOnlyPointsPossible
          });
          var percentageElem = testbed.findChildrenByClass(styles.score_percentage)[0];
          expect(percentageElem).to.be.undefined();
        });
      });
      context('only points possible present', function () {
        var resultWithNoPointsValues = new _QuizSessionResult.default();
        it('should display a points placeholder', function () {
          testbed.render({
            quizSessionResult: resultWithNoPointsValues
          });
          var pointsElem = testbed.findChildrenByClass(styles.info_wrapper)[0];
          expect(testbed.getText(pointsElem)).to.include('-- Points possible');
        });
        it('should not display percentage', function () {
          testbed.render({
            quizSessionResult: resultWithNoPointsValues
          });
          var percentageElem = testbed.findChildrenByClass(styles.score_percentage)[0];
          expect(percentageElem).to.be.undefined();
        });
      });
    });
    context('elapsed time display', function () {
      it('should display time', function () {
        testbed.render();
        var formattedTime = testbed.findChildrenByType(_FormattedDuration.default)[0];
        expect(formattedTime.props.timeLimitInSeconds).to.eq(3600);
      });
      it('should display Time for this attempt', function () {
        testbed.render();
        var percentageElem = testbed.findChildrenByClass(styles.info_wrapper)[1];
        expect(testbed.getText(percentageElem)).to.include('Time for this attempt');
      });
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render({});
      testbed.checkA11yStandards(done);
    });
  });
});