"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 _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");

var _quizzes = require("../../../common/actions/quizzes");

var _itemAnalyses = require("../itemAnalyses");

describe('reporting api actions', function () {
  var quizId = '123';
  var itemId = '456';
  var itemAnalysisData = [{
    item_id: itemId,
    item_analysis: {}
  }];
  var fetchStub = sinon.stub();
  var dispatchStub = sinon.stub();
  var dispatchStubIgnore = sinon.stub();
  var getStateStub = sinon.stub();
  useFakeFetching({
    dispatchStub: dispatchStub,
    getStateStub: getStateStub,
    fetchStub: fetchStub
  });
  describe('getItemAnalyses', function () {
    var stubbedGetItemAnalysisCall = function stubbedGetItemAnalysisCall() {
      var response = (0, _jsonify.jsoniblePromise)([itemAnalysisData]);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _itemAnalyses.getItemAnalyses)(quizId)(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _itemAnalyses.getItemAnalyses)(quizId)(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWithMatch("/api/quizzes/".concat(quizId, "/stats/item_analyses/"));
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var expectedHandleResponse, expectedAssociateResponse;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return stubbedGetItemAnalysisCall();

            case 2:
              expectedHandleResponse = (0, _callHandlers.handleItemAnalysesResponse)([itemAnalysisData]);
              expectedAssociateResponse = (0, _quizzes.associateItemAnalyses)(quizId, [itemAnalysisData]);
              checkActionDispatched(dispatchStub, expectedHandleResponse[0]);
              checkActionDispatched(dispatchStub, expectedAssociateResponse);

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