"use strict";

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

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

var _immutable = require("immutable");

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

var _uiTestUtils = require("@instructure/ui-test-utils");

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);
  describe('Accommodations column', function () {
    it('renders "None" when there is no accommodation data',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var subject, row;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return (0, _uiTestUtils.mount)(_react.default.createElement(_presenter.default, Object.assign({}, props, {
                onAccommodationsClick: Function.prototype
              })));

            case 2:
              subject = _context.sent;
              row = (0, _uiTestUtils.within)(subject.getDOMNode());
              _context.t0 = _uiTestUtils.expect;
              _context.next = 7;
              return row.find(':textContent(None)');

            case 7:
              _context.t1 = _context.sent;
              (0, _context.t0)(_context.t1).to.exist();

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
    it('renders the name and value of accommodations that are present',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var accRow, subject, row;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              accRow = (0, _objectSpread2.default)({}, props.rowData, {
                accommodations: [{
                  name: 'Attempts',
                  value: '+1'
                }, {
                  name: 'Time',
                  value: '+1 hr 20 min'
                }]
              });
              _context2.next = 3;
              return (0, _uiTestUtils.mount)(_react.default.createElement(_presenter.default, {
                onAccommodationsClick: Function.prototype,
                onModerateButtonClick: props.onModerateButtonClick,
                onModerateLogClick: props.onModerateLogClick,
                quizSessions: props.quizSessions,
                rowData: accRow
              }));

            case 3:
              subject = _context2.sent;
              row = (0, _uiTestUtils.within)(subject.getDOMNode());
              _context2.t0 = _uiTestUtils.expect;
              _context2.next = 8;
              return row.find(':textContent(Attempts: +1)');

            case 8:
              _context2.t1 = _context2.sent;
              (0, _context2.t0)(_context2.t1).to.exist();
              _context2.t2 = _uiTestUtils.expect;
              _context2.next = 13;
              return row.find(':textContent(Time: +1 hr 20 min)');

            case 13:
              _context2.t3 = _context2.sent;
              (0, _context2.t2)(_context2.t3).to.exist();

            case 15:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
    it('does not render accommodations column if the cb is not present',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee3() {
      var subject, row;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return (0, _uiTestUtils.mount)(_react.default.createElement(_presenter.default, props));

            case 2:
              subject = _context3.sent;
              row = (0, _uiTestUtils.within)(subject.getDOMNode());
              _context3.t0 = _uiTestUtils.expect;
              _context3.next = 7;
              return row.find(':textContent(+ add)', {
                expectEmpty: true
              });

            case 7:
              _context3.t1 = _context3.sent;
              (0, _context3.t0)(_context3.t1).not.to.exist();

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
    it('calls the accommodations cb when accommodation button is clicked',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee4() {
      var cb, link;
      return _regenerator.default.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              cb = (0, _uiTestUtils.stub)();
              _context4.next = 3;
              return (0, _uiTestUtils.mount)(_react.default.createElement(_presenter.default, Object.assign({}, props, {
                onAccommodationsClick: cb
              })));

            case 3:
              _context4.next = 5;
              return (0, _uiTestUtils.findWithLabel)('Edit student accommodations');

            case 5:
              link = _context4.sent;
              _context4.next = 8;
              return link.click();

            case 8:
              _context4.next = 10;
              return (0, _uiTestUtils.wait)(function () {
                (0, _uiTestUtils.expect)(cb).to.have.been.calledOnce();
              });

            case 10:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
  });
  it('renders a moderate button by default', function () {
    var wrapper = testbed.renderShallowly();
    (0, _uiTestUtils.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
    });
    (0, _uiTestUtils.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');
    (0, _uiTestUtils.expect)(onModerateButtonClick).to.have.been.calledOnce();
    (0, _uiTestUtils.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');
    (0, _uiTestUtils.expect)(onModerateLogClick).to.have.been.calledOnce();
    (0, _uiTestUtils.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();
    (0, _uiTestUtils.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();
    (0, _uiTestUtils.expect)(output).to.include('N/A');
  });
  it('renders empty score message', function () {
    connectedTestbed.render({
      quizSessions: (0, _immutable.List)()
    });
    var output = connectedTestbed.getText();
    (0, _uiTestUtils.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();
    (0, _uiTestUtils.expect)(output).to.include('In progress, score not available');
    (0, _uiTestUtils.expect)(output).to.include('---');
  });
  it('renders attempts when present', function () {
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    (0, _uiTestUtils.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

    (0, _uiTestUtils.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

    (0, _uiTestUtils.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();
    (0, _uiTestUtils.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();
    (0, _uiTestUtils.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');
    (0, _uiTestUtils.expect)(onResultsLinkClick).to.have.been.calledOnce();
    (0, _uiTestUtils.expect)(onResultsLinkClick).to.have.been.calledWithMatch('1');
  });
  it('renders empty attempt message', function () {
    connectedTestbed.render({
      quizSessions: (0, _immutable.List)()
    });
    var output = connectedTestbed.getText();
    (0, _uiTestUtils.expect)(output).to.include('No Attempt');
  });
  it('renders empty time message', function () {
    connectedTestbed.render({
      quizSessions: (0, _immutable.List)()
    });
    var output = connectedTestbed.getText();
    (0, _uiTestUtils.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();
    (0, _uiTestUtils.expect)(output).to.include('In progress, time not available');
    (0, _uiTestUtils.expect)(output).to.include('---');
  });
  it('renders empty log message', function () {
    connectedTestbed.render({
      quizSessions: (0, _immutable.List)()
    });
    var output = connectedTestbed.getText();
    (0, _uiTestUtils.expect)(output).to.include('No Log');
  });
  it('renders view log message', function () {
    sandbox.stub(quizSession, 'inProgress').returns(false);
    connectedTestbed.render();
    var output = connectedTestbed.getText();
    (0, _uiTestUtils.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();
    (0, _uiTestUtils.expect)(output).to.include('In Progress');
  });
});