"use strict";

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

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

var _updateResults = require("../updateResults");

var _postMessage = require("../../../common/actions/postMessage");

var _quizSessionResults = require("../../../common/actions/quizSessionResults");

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

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

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

describe('postQuizSessionResults action', function () {
  var fetchStub = sinon.stub();
  var dispatchStub = sinon.stub();
  var fetchQuizSessionCb = sinon.stub();
  var getStateStub = sinon.stub();
  useFakeFetching({
    dispatchStub: dispatchStub,
    getStateStub: getStateStub,
    fetchStub: fetchStub
  }); // for the async tests

  var quizSessionResultsResponse = {
    id: '99'
  };
  var response = (0, _jsonify.jsoniblePromise)(quizSessionResultsResponse);
  var fakeFetch = (0, _fakeFetch.default)(response);
  var sessionId = '1';
  var sessionItemResultsMods = [{}];
  var fudgePoints = 5;
  var postQuizSessionResultsThunk = (0, _updateResults.postQuizSessionResults)(sessionId, sessionItemResultsMods, fudgePoints, fetchQuizSessionCb);

  var stubbedPostQuizSessionResultsCall = function stubbedPostQuizSessionResultsCall() {
    _Fetcher.default.fakeFetch = fetchStub;
    return postQuizSessionResultsThunk(dispatchStub, getStateStub);
  };

  var fakedAsyncPostQuizSessionResultsCall = function fakedAsyncPostQuizSessionResultsCall() {
    _Fetcher.default.fakeFetch = fakeFetch;
    return postQuizSessionResultsThunk(dispatchStub, getStateStub);
  };

  it('makes a call to the quizSessionResults endpoint', function () {
    stubbedPostQuizSessionResultsCall();
    expect(fetchStub.calledWith("/api/quiz_sessions/".concat(sessionId, "/results"))).to.be.true();
  });
  it('posts a submissionUpdate update message to the parent window', function (done) {
    _Fetcher.default.fakeFetch = fakeFetch;
    var fetchPromise = fakedAsyncPostQuizSessionResultsCall();
    var expectedPostMessageAction = (0, _postMessage.postToParent)({
      subject: _constants.SUBMISSION_UPDATE_SUBJECT
    });
    fetchPromise.then(function () {
      checkActionDispatched(dispatchStub, expectedPostMessageAction);
      done();
    });
  });
  it('updates the quizSessionResults on success', function (done) {
    _Fetcher.default.fakeFetch = fakeFetch;
    var fetchPromise = fakedAsyncPostQuizSessionResultsCall();
    var expectedQuizSessionResultsAction = (0, _quizSessionResults.addQuizSessionResults)([quizSessionResultsResponse]);
    fetchPromise.then(function () {
      checkActionDispatched(dispatchStub, expectedQuizSessionResultsAction);
      done();
    });
  });
  it('fetches a new quiz session on success', function (done) {
    _Fetcher.default.fakeFetch = fakeFetch;
    var fetchPromise = fakedAsyncPostQuizSessionResultsCall();
    fetchPromise.then(function () {
      expect(fetchQuizSessionCb.called).to.be.true();
      done();
    });
  });
});