"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 _chai = require("chai");

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

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

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

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

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

describe('quizSession api actions', function () {
  var quizId = '456';
  var sessionId = '123';
  var resultId = '789';
  var quizSessionData = {
    id: sessionId
  };
  var quizSessionResult = {
    id: resultId
  };
  var fetchStub = sinon.stub();
  var dispatchStub = sinon.stub();
  var dispatchStubIgnore = sinon.stub();
  var getStateStub = sinon.stub();
  useFakeFetching({
    dispatchStub: dispatchStub,
    getStateStub: getStateStub,
    fetchStub: fetchStub
  });
  describe('getQuizSessions', function () {
    var stubbedGetQuizSessionsCall = function stubbedGetQuizSessionsCall() {
      var response = (0, _jsonify.jsoniblePromise)([quizSessionData]);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizSessions.getQuizSessions)(quizId)(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _quizSessions.getQuizSessions)(quizId)(dispatchStubIgnore, getStateStub);
      (0, _chai.expect)(fetchStub.calledWith("/api/quizzes/".concat(quizId, "/quiz_sessions"))).to.be.true();
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return stubbedGetQuizSessionsCall();

            case 2:
              expectedResponse = (0, _callHandlers.handleQuizSessionsResponse)([quizSessionData]);
              checkActionDispatched(dispatchStub, expectedResponse[0]);

            case 4:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
  });
  describe('getQuizSession', function () {
    var stubbedGetQuizSessionCall = function stubbedGetQuizSessionCall() {
      var response = (0, _jsonify.jsoniblePromise)(quizSessionResult);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizSessions.getQuizSession)(sessionId)(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _quizSessions.getQuizSession)(sessionId)(dispatchStubIgnore, getStateStub);
      (0, _chai.expect)(fetchStub.calledWith("/api/quiz_sessions/".concat(sessionId))).to.be.true();
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return stubbedGetQuizSessionCall();

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

            case 4:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
  describe('reopenQuizSession', function () {
    var stubbedGetQuizSessionCall = function stubbedGetQuizSessionCall() {
      var response = (0, _jsonify.jsoniblePromise)(quizSessionResult);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _quizSessions.reopenQuizSession)(sessionId)(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _quizSessions.reopenQuizSession)(sessionId)(dispatchStubIgnore, getStateStub);
      (0, _chai.expect)(fetchStub.calledWith("/api/quiz_sessions/".concat(sessionId, "/reopen"))).to.be.true();
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee3() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return stubbedGetQuizSessionCall();

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

            case 4:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
  });
});