"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

var _immutable = require("immutable");

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

var _constants = require("@instructure/quiz-common/lib/constants");

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

var _reduxStore = _interopRequireWildcard(require("../../../../../reduxStore"));

describe('ModerateTable', function () {
  var sandbox = sinon.createSandbox();
  var props = {
    onModerateLogClick: function onModerateLogClick() {},
    getQuizSessions: function getQuizSessions() {},
    tableData: [{
      avatar: 'zomgModerateTable',
      id: '42',
      name: 'lolwut',
      quizSessionIds: ['1']
    }, {
      avatar: 'lulzModerateTable',
      id: '77',
      name: 'wutlol',
      quizSessionIds: ['2']
    }],
    quizId: '1'
  };
  afterEach(function () {
    sandbox.resetHistory();
  });
  stubComponent(_Avatar.default);
  var testbedWithContext = createTestbed(withContext(_index.default, {}), props);
  useFakeFetching({
    dispatchStub: sandbox.stub(),
    getStateStub: sandbox.stub(),
    fetchStub: sandbox.stub()
  });
  shouldRenderComponent(testbedWithContext, {}, _index.default);
  context('mapStateToProps', function () {
    afterEach(function () {
      (0, _reduxStore.unsetFakeStore)();
    });
    it('computes isFetching', function () {
      var result;
      (0, _reduxStore.setFakeStore)((0, _immutable.fromJS)({
        calls: (0, _defineProperty2.default)({}, _constants.QUIZ_SESSIONS_FETCH_CALL, {
          requestStatus: 'calling'
        })
      }));
      result = (0, _index.mapStateToProps)(_reduxStore.default.getAppState(), props);
      expect(result).to.deep.eq({
        isFetching: true
      });
      (0, _reduxStore.setFakeStore)((0, _immutable.fromJS)({
        calls: (0, _defineProperty2.default)({}, _constants.QUIZ_SESSIONS_FETCH_CALL, {
          requestStatus: 'success'
        })
      }));
      result = (0, _index.mapStateToProps)(_reduxStore.default.getAppState(), props);
      expect(result).to.deep.eq({
        isFetching: false
      });
    });
  });
  context('mapDispatchToProps', function () {
    it('receives getQuizSessions', function () {
      var getQuizSessions = _index.mapDispatchToProps.getQuizSessions;
      expect(getQuizSessions).to.be.a('function');
    });
  });
  context('#getQuizSessions', function () {
    // NOTE: test w/o connect component wrapper
    var testbedWithContext = createTestbed(withContext(_index.ModerateTable, {}), props);
    beforeEach(function () {
      sandbox.spy(props, 'getQuizSessions');
    });
    afterEach(function () {
      sandbox.restore();
    });
    it('is invoked on mount', function () {
      testbedWithContext.render();
      expect(props.getQuizSessions).to.have.been.calledOnce();
    });
    it('is invoked on update from new props', function () {
      testbedWithContext.render();
      testbedWithContext.render({
        tableData: [{
          avatar: 'lolol',
          id: '99',
          name: 'wutwut',
          quizSessionIds: ['3']
        }]
      });
      expect(props.getQuizSessions).to.have.been.calledTwice();
    });
    it('does not fetch on update if the sessions are the same', function () {
      testbedWithContext.render();
      testbedWithContext.render({
        tableData: props.tableData
      });
      expect(props.getQuizSessions).to.have.been.calledOnce();
    });
    it('does not fetch when there are no quizSessionIds', function () {
      testbedWithContext.render({
        tableData: [{
          avatar: 'nope',
          id: '101',
          name: 'empty',
          quizSessionIds: []
        }]
      });
      expect(props.getQuizSessions).to.not.have.been.called();
    });
  });
});