"use strict";

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

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

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

var _callHelpers = require("../../util/callHelpers");

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

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

var _fileUpload = require("../fileUpload");

describe('fileUpload', function () {
  // reset in useFakeFetching
  var dispatchStub = sinon.stub();
  var getStateStub = sinon.stub();
  var fetchStub = sinon.stub();
  useFakeFetching({
    dispatchStub: dispatchStub,
    getStateStub: getStateStub,
    fetchStub: fetchStub
  });
  var onErrorStub = sinon.stub();
  var onSuccessStub = sinon.stub();
  afterEach(function () {
    onErrorStub.resetHistory();
    onSuccessStub.resetHistory();
  });
  describe('sendFileToS3', function () {
    var fakeS3Url = 'http://s3/fake/url';
    var fakeQueryString = '?query_string';
    var uploadUrlResponse = {
      url: fakeS3Url + fakeQueryString
    };
    var dummyFile = {
      type: 'filetype',
      name: 'BLOB'
    };
    var errorResponse = (0, _jsonify.jsoniblePromise)({
      status: 500
    });
    var fakeErrorFetch = (0, _fakeFetch.default)(errorResponse);
    var emptyResponse = (0, _jsonify.jsoniblePromise)({});
    var fakeEmptyFetch = (0, _fakeFetch.default)(emptyResponse);

    var fakeSendFileToS3 = function fakeSendFileToS3() {
      var file = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : dummyFile;
      var sendFileToS3Thunk = (0, _fileUpload.sendFileToS3)(file, onErrorStub, onSuccessStub, dispatchStub, getStateStub);
      return sendFileToS3Thunk(uploadUrlResponse);
    };

    it('should mark the file upload url fetch call as started', function () {
      fakeSendFileToS3();
      checkActionDispatched(dispatchStub, (0, _callHelpers.startCallFor)(_constants.FILE_UPLOAD_CALL));
    });
    it('should upload to the provided url', function () {
      fakeSendFileToS3();
      expect(fetchStub.calledWith(uploadUrlResponse.url)).to.be.true();
    });
    it('should remove the query string', function (done) {
      _Fetcher.default.fakeFetch = fakeEmptyFetch;
      fakeSendFileToS3().then(function () {
        done();
        expect(onSuccessStub.calledWith(fakeS3Url)).to.be.true();
      });
    });
    it('should mark the create call as a success on return', function (done) {
      _Fetcher.default.fakeFetch = fakeEmptyFetch;
      fakeSendFileToS3().then(function () {
        checkActionDispatched(dispatchStub, (0, _callHelpers.successCallFor)(_constants.FILE_UPLOAD_CALL));
        done();
      });
    });
    it('supports unicode filenames', function () {
      fakeSendFileToS3({
        type: 'txt',
        name: '𐌱𐍂𐌹𐌲𐌲𐌹𐌸 '
      });
      expect(fetchStub.calledOnce).to.be.true();
    });
    context('error on uploading the file', function () {
      it('should call the onError callback', function (done) {
        _Fetcher.default.fakeFetch = fakeErrorFetch;
        fakeSendFileToS3().then(done());
        expect(onErrorStub.called).to.be.true();
      });
    });
  });
  describe('getUploadUrl', function () {
    it('calls to the proper url', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _fileUpload.getUploadUrl)('foo.com');
      expect(fetchStub.calledWith('foo.com')).to.be.true();
    });
  });
});