"use strict";

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

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

var _immutable = require("immutable");

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

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

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

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

describe('ModerateTableRow presenter', function () {
  var sandbox = sinon.createSandbox();
  stubComponent(_Avatar.default);
  afterEach(function () {
    sandbox.restore();
    sandbox.resetHistory();
  });
  var quizSession = new _QuizSession.default({
    id: '1'
  });
  var props = {
    onModerateButtonClick: function onModerateButtonClick() {},
    onModerateLogClick: function onModerateLogClick() {},
    quizSessions: _immutable.List.of(quizSession),
    rowData: {
      avatar: 'zomgModerateTableRowPresenter',
      id: '42',
      name: 'lolwut',
      quizSessionIds: ['1']
    }
  };
  var testbed = createTestbed(_presenter.default, props);
  var connectedTestbed = createTestbed(withContext(_presenter.default), props);
  shouldRenderComponent(connectedTestbed, {}, _presenter.default);
  it('renders a moderate button by default', function () {
    var wrapper = testbed.renderShallowly();
    expect(wrapper.find('Button').exists()).to.be.true();
  });
  it('does not render a moderate button when shouldRenderModerateButton is false', function () {
    var wrapper = testbed.renderShallowly({
      shouldRenderModerateButton: false
    });
    expect(wrapper.find('Button').exists()).to.be.false();
  });
  it('invokes onModerateButtonClick when the moderate button is clicked', function () {
    var onModerateButtonClick = sandbox.spy();
    var wrapper = testbed.renderShallowly({
      onModerateButtonClick: onModerateButtonClick
    });
    wrapper.find('Button').simulate('click');
    expect(onModerateButtonClick).to.have.been.calledOnce();
    expect(onModerateButtonClick.firstCall.args[0]).to.deep.equal(props.rowData);
  });
  it('invokes onModerateLogClick when the log link is clicked', function () {
    var onModerateLogClick = sandbox.spy();
    var wrapper = testbed.renderShallowly({
      onModerateLogClick: onModerateLogClick
    });
    var viewLoglink = wrapper.find('Link').last();
    viewLoglink.simulate('click');
    expect(onModerateLogClick).to.have.been.calledOnce();
    expect(onModerateLogClick).to.have.been.calledWithMatch({
      id: '42',
      quizSessionId: '1'
    });
  });
  it('renders scores', function () {
    var quizSessionResult = new _QuizSessionResult.default({
      score: 1,
      pointsPossible: 2
    });
    sandbox.stub(quizSession, 'findAuthoritativeResult').returns(quizSessionResult);
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    expect(output).to.include('50%');
  });
  it('renders non applicable scores', function () {
    var quizSessionResult = new _QuizSessionResult.default({
      pointsPossible: 0
    });
    sandbox.stub(quizSession, 'findAuthoritativeResult').returns(quizSessionResult);
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    expect(output).to.include('N/A');
  });
  it('renders empty score message', function () {
    connectedTestbed.render({
      quizSessions: (0, _immutable.List)()
    });
    var output = connectedTestbed.getText();
    expect(output).to.include('No Score');
  });
  it('renders in progress score message', function () {
    sandbox.stub(quizSession, 'inProgress').returns(true);
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    expect(output).to.include('In progress, score not available');
    expect(output).to.include('---');
  });
  it('renders attempts when present', function () {
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    expect(output).to.include('Attempt 1 / 1');
  });
  it('renders attempt as link if session is graded', function () {
    sandbox.stub(quizSession, 'graded').returns(true);
    var onResultsLinkClick = sandbox.spy();
    var wrapper = testbed.renderShallowly({
      onResultsLinkClick: onResultsLinkClick
    }); // We have one other rendering for the Log no matter what

    expect(wrapper.find('Link').length).to.equal(2);
  });
  it('does not render attempt as link if session is not graded', function () {
    sandbox.stub(quizSession, 'graded').returns(false);
    var onResultsLinkClick = sandbox.spy();
    var wrapper = testbed.renderShallowly({
      onResultsLinkClick: onResultsLinkClick
    }); // We have one other rendering for the Log no matter what

    expect(wrapper.find('Link').length).to.equal(1);
  });
  it('renders total attempt count in attempt output if present', function () {
    var maxAttempts = 5;
    connectedTestbed.render({
      rowData: (0, _objectSpread2.default)({}, props.rowData, {
        maxAttempts: maxAttempts
      })
    });
    var output = connectedTestbed.getText();
    expect(output).to.include('Attempt 1 / 5');
  });
  it('ignores second part of attempt output if maxAttempts is Infinity', function () {
    var maxAttempts = Infinity;
    connectedTestbed.render({
      rowData: (0, _objectSpread2.default)({}, props.rowData, {
        maxAttempts: maxAttempts
      })
    });
    var output = connectedTestbed.getText();
    expect(output).to.include('Attempt 1');
  });
  it('invokes onResultsLinkClick when the attempt result link is clicked', function () {
    sandbox.stub(quizSession, 'graded').returns(true);
    var onResultsLinkClick = sandbox.spy();
    var wrapper = testbed.renderShallowly({
      onResultsLinkClick: onResultsLinkClick
    });
    var attemptLink = wrapper.find('Link').first();
    attemptLink.simulate('click');
    expect(onResultsLinkClick).to.have.been.calledOnce();
    expect(onResultsLinkClick).to.have.been.calledWithMatch('1');
  });
  it('renders empty attempt message', function () {
    connectedTestbed.render({
      quizSessions: (0, _immutable.List)()
    });
    var output = connectedTestbed.getText();
    expect(output).to.include('No Attempt');
  });
  it('renders empty time message', function () {
    connectedTestbed.render({
      quizSessions: (0, _immutable.List)()
    });
    var output = connectedTestbed.getText();
    expect(output).to.include('No Time');
  });
  it('renders in progress time message', function () {
    sandbox.stub(quizSession, 'inProgress').returns(true);
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    expect(output).to.include('In progress, time not available');
    expect(output).to.include('---');
  });
  it('renders empty log message', function () {
    connectedTestbed.render({
      quizSessions: (0, _immutable.List)()
    });
    var output = connectedTestbed.getText();
    expect(output).to.include('No Log');
  });
  it('renders view log message', function () {
    sandbox.stub(quizSession, 'inProgress').returns(false);
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    expect(output).to.include('View Log');
  });
  it('renders in progress log message', function () {
    sandbox.stub(quizSession, 'inProgress').returns(true);
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    expect(output).to.include('In Progress');
  });
});