"use strict";

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

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

var _react = _interopRequireDefault(require("react"));

var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));

var _immutable = require("immutable");

var _OutcomeList = _interopRequireDefault(require("outcomes-react/lib/containers/OutcomeList"));

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

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

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

var _AnswerFrequencySummary = _interopRequireDefault(require("../../AnswerFrequencySummary"));

// Components for render checks and interrogating the DOM
describe('ItemAnalysisRow', function () {
  var quizId = '1';
  var quizEntryId = '1';
  var getItemAnalysisStub = sinon.stub();
  var toggleExpandedStub = sinon.stub();
  var screenreaderNotificationStub = sinon.stub();
  var itemAnalysisData = {
    itemId: quizEntryId,
    itemBody: 'itemBody',
    position: 25,
    itemAnalysis: {
      scoreInfo: {
        resultSum: 5,
        resultCount: 1
      },
      difficultyInfo: {
        responseCount: 5,
        correctAnswerCount: 1
      },
      choiceInfo: [{
        choiceCount: 2,
        id: 'id',
        itemBody: 'choiceItemBody'
      }],
      quintile: [1, 2, 3, 4, 5]
    }
  };
  var standardItemAnalysis = new _ItemAnalysis.default((0, _immutable.fromJS)(itemAnalysisData));
  var testbed = createTestbed(withContext(_presenter.default), {
    expanded: false,
    getItemAnalysis: getItemAnalysisStub,
    itemAnalysis: standardItemAnalysis,
    itemId: quizEntryId,
    position: 25,
    quizId: quizId,
    toggleExpanded: toggleExpandedStub,
    screenreaderNotification: screenreaderNotificationStub
  });
  afterEach(function () {
    screenreaderNotificationStub.resetHistory();
  });
  shouldRenderComponent(testbed, {}, _presenter.default);
  it('should render the item analysis itemBody', function () {
    testbed.render();
    var output = testbed.getText(); // Add more stuff here when we render an overview

    expect(output).to.include('itemBody');
  });
  it('should render the choice info', function () {
    testbed.render();
    var output = testbed.getText();
    expect(output).to.include('Difficulty Index');
    expect(output).to.include('Discrimination Index');
    expect(output).to.include('RPB');
    expect(output).to.include('Mean Earned Score');
  });
  it('should render only the metrics supplied as children', function () {
    testbed.render({
      children: [_react.default.createElement(_MetricsListItem.default, {
        label: 'Difficulty Index',
        value: 5,
        key: 'DifficultyIndex'
      })]
    });
    var output = testbed.getText();
    expect(output).to.include('Difficulty Index');
    expect(output).to.not.include('Discrimination Index');
    expect(output).to.not.include('RPB');
    expect(output).to.not.include('Mean Earned Score');
  });
  it('should render empty text when not found', function () {
    var notFoundItemAnalysis = new _ItemAnalysis.default((0, _immutable.fromJS)({
      itemId: quizEntryId,
      notFound: true
    }));
    testbed.render({
      itemAnalysis: notFoundItemAnalysis
    });
    var output = testbed.getText();
    expect(output).to.include('No submissions yet');
    expect(output).to.not.include('Discrimination Index');
    expect(testbed.findChildrenByTagName('button').length).to.eq(0);
  });
  describe('expandable', function () {
    shouldRenderComponent(testbed, {
      expanded: true
    }, _AnswerFrequencySummary.default);
    it('should read correctly', function () {
      testbed.render();
      expect(testbed.getText()).to.include('Expand item 25');
      expect(testbed.getText()).to.not.include('Collapse item 25');
      testbed.render({
        expanded: true
      });
      expect(testbed.getText()).to.include('Collapse item 25');
      expect(testbed.getText()).to.not.include('Expand item 25');
    });
    it('clicking down arrow should trigger expand toggle', function () {
      testbed.render();
      var icon = testbed.findChildrenByTagName('button')[0];

      _testUtils.default.Simulate.click(icon);

      expect(toggleExpandedStub).to.be.calledOnce();
      expect(screenreaderNotificationStub).to.be.calledOnce();
    });
    it('should render the expanded answer info', function () {
      testbed.render({
        expanded: true
      });
      var output = testbed.getText();
      expect(output).to.include('choiceItemBody');
      expect(output).to.include('Respondents');
    });
    it('should render the expanded answer info', function () {
      var newItemAnalysisData = (0, _objectSpread2.default)({}, itemAnalysisData);
      newItemAnalysisData.itemAnalysis.choiceInfo = [];
      var newItemAnalysis = new _ItemAnalysis.default((0, _immutable.fromJS)(itemAnalysisData));
      testbed.render({
        itemAnalysis: newItemAnalysis,
        expanded: true
      });
      var output = testbed.getText();
      expect(output).to.not.include('choiceItemBody');
      expect(output).to.not.include('Respondents');
    });
  });
  describe('outcome alignments', function () {
    var outcomesEndpoint = 'http://outcomes';
    var outcomesToken = 'token';
    var outcomesItemAnalysis = new _ItemAnalysis.default((0, _immutable.fromJS)((0, _objectSpread2.default)({}, itemAnalysisData, {
      outcomeAlignmentSetGuid: 'foo'
    })));
    var outcomesParams = {
      expanded: true,
      outcomesEndpoint: outcomesEndpoint,
      outcomesToken: outcomesToken,
      itemAnalysis: outcomesItemAnalysis
    };
    it('should render alignments if expanded, configured, and guid present', function () {
      testbed.render(outcomesParams);
      expect(testbed.findChildrenByType(_OutcomeList.default)).to.have.length(1);
    });
    it('should not render alignments if not expanded', function () {
      testbed.render((0, _objectSpread2.default)({}, outcomesParams, {
        expanded: false
      }));
      expect(testbed.findChildrenByType(_OutcomeList.default)).to.have.length(0);
    });
    it('should not render alignments if outcomes host not configured', function () {
      testbed.render((0, _objectSpread2.default)({}, outcomesParams, {
        outcomesEndpoint: null
      }));
      expect(testbed.findChildrenByType(_OutcomeList.default)).to.have.length(0);
    });
    it('should not render alignments if outcomes guid not present', function () {
      testbed.render((0, _objectSpread2.default)({}, outcomesParams, {
        itemAnalysis: standardItemAnalysis
      }));
      expect(testbed.findChildrenByType(_OutcomeList.default)).to.have.length(0);
    });
  });
});