"use strict";

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

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

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

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

var _Fetcher = _interopRequireDefault(require("../../util/Fetcher"));

var _jsonify = require("../../util/jsonify");

var _fakeFetch = _interopRequireDefault(require("../../../../tests/util/fakeFetch"));

var _quizSessions = require("../quizSessions");

var _callHandlers = require("../callHandlers");

describe('quizSessions api actions', function () {
  var quizSessionId = '22';
  var fetchStub = sinon.stub();
  var dispatchStub = sinon.stub();
  var getStateStub = sinon.stub();
  var callBackStub = sinon.stub();
  afterEach(function () {
    callBackStub.resetHistory();
  });
  useFakeFetching({
    dispatchStub: dispatchStub,
    getStateStub: getStateStub,
    fetchStub: fetchStub
  });

  var actAsApiCall = function actAsApiCall(apiCallHandler, urlParts, expectedUrl) {
    it('hits the correct url', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      apiCallHandler.apply(void 0, (0, _toConsumableArray2.default)(urlParts));
      expect(fetchStub.getCall(0).args[0]).to.contain(expectedUrl);
    });
    it('calls the callback',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var response;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              response = [{
                faktFetch: 33
              }];
              _Fetcher.default.fakeFetch = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)(response));
              _context.next = 4;
              return apiCallHandler.apply(void 0, (0, _toConsumableArray2.default)(urlParts).concat([new Promise(function () {
                return callBackStub();
              })]));

            case 4:
              expect(callBackStub.calledOnce).to.be.true();

            case 5:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
  };

  describe('fetchQuizSession', function () {
    actAsApiCall(_quizSessions.fetchQuizSession, [22], '/api/quiz_sessions/22');
  });
  describe('fetchSessionItems', function () {
    actAsApiCall(_quizSessions.fetchSessionItems, [22, 'session_items'], '/api/quiz_sessions/22/session_items');
  });
  describe('takeQuizSession', function () {
    actAsApiCall(_quizSessions.takeQuizSession, [22], '/api/quiz_sessions/22/take');
  });
  describe('fetchResumeData', function () {
    actAsApiCall(_quizSessions.fetchResumeData, [22], '/api/quiz_sessions/22/session_item_responses/resume_data');
  });
  describe('fetchTime', function () {
    actAsApiCall(_quizSessions.fetchTime, [22], '/api/quiz_sessions/22/time');
  });
  describe('recordTime', function () {
    actAsApiCall(_quizSessions.recordTime, [22], '/api/quiz_sessions/22/record');
  });
  describe('singleResponseSubmit', function () {
    actAsApiCall(_quizSessions.singleResponseSubmit, [22], '/api/quiz_sessions/22/session_item_responses/single_response_submit');
  });
  describe('submitQuizSession', function () {
    actAsApiCall(_quizSessions.submitQuizSession, [22], '/api/quiz_sessions/22/submit');
  });
  describe('fetchSessionItemResults', function () {
    actAsApiCall(_quizSessions.fetchSessionItemResults, [22, 1], '/api/quiz_sessions/22/results/1/session_item_results');
  });
  describe('fetchQuizSessionResult', function () {
    actAsApiCall(_quizSessions.fetchQuizSessionResult, [22, 1], '/api/quiz_sessions/22/results/1');
  });
  describe('submitStudentAccessCode', function () {
    actAsApiCall(_quizSessions.submitStudentAccessCode, [22, 'code'], '/api/quiz_sessions/22/submit_student_access_code');
  });
  describe('pinSessionItem', function () {
    it('hits the correct url', function () {
      var expectedUrl = '/api/quiz_sessions/22/toggle_session_item_pin';
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _quizSessions.pinSessionItem)(quizSessionId, 11)(dispatchStub, getStateStub);
      expect(fetchStub.getCall(0).args[0]).to.eq(expectedUrl);
    });
    it('adds a quiz session response on success',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var fakeQuizSessionResult, stubbedPinSessionItemCall, expectedResponse;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              fakeQuizSessionResult = {
                id: '22',
                takingData: {
                  pins: [11]
                }
              };

              stubbedPinSessionItemCall = function stubbedPinSessionItemCall() {
                var response = (0, _jsonify.jsoniblePromise)(fakeQuizSessionResult);
                _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
                return (0, _quizSessions.pinSessionItem)('22', 11)(dispatchStub, getStateStub);
              };

              _context2.next = 4;
              return stubbedPinSessionItemCall();

            case 4:
              expectedResponse = (0, _callHandlers.handleQuizSessionResponse)(fakeQuizSessionResult);
              checkActionDispatched(dispatchStub, expectedResponse[0]);

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
});