"use strict";

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

var _interactionTypePropsHelper = require("../interactionTypePropsHelper");

var _Item = _interopRequireDefault(require("../../records/Item"));

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

function noop() {}

describe('interactionTypePropsHelpers', function () {
  describe('#propsForInteractionEdit', function () {
    var defaultInputProps = {
      workingItem: new _Item.default({}),
      parentType: _constants.PARENT_TYPE_QUIZ,
      parentId: '2'
    };
    it('replaces null item bodies with an empty string', function () {
      var newProps = (0, _interactionTypePropsHelper.propsForInteractionEdit)({
        itemBody: null,
        workingItem: {
          toJS: function toJS() {
            return {};
          },
          getErrors: function getErrors() {
            return {
              toJS: function toJS() {
                return {};
              }
            };
          },
          errorsFor: function errorsFor() {
            return {
              toJS: function toJS() {
                return {};
              }
            };
          }
        }
      });
      expect(newProps.itemBody).to.be.eql('');
    });
    describe('mediaUpload', function () {
      it('calls interactionFileUpload with the right arguments', function () {
        var interactionFileUploadStub = sinon.stub();
        var inputProps = Object.assign({}, defaultInputProps, {
          interactionFileUpload: interactionFileUploadStub
        });
        var returnedProps = (0, _interactionTypePropsHelper.propsForInteractionEdit)(inputProps);
        returnedProps.mediaUpload('file', noop);
        expect(interactionFileUploadStub.calledWith(_constants.PARENT_TYPE_QUIZ, '2', 'file')).to.be.true();
      });
      it('calls the callback in its onSuccess arg', function () {
        var clock = sinon.useFakeTimers();

        var execSuccess = function execSuccess(_parent, _id, _file, onSuccess) {
          onSuccess('someReturnedUrl');
        };

        var inputProps = Object.assign({}, defaultInputProps, {
          interactionFileUpload: execSuccess
        });
        var returnedProps = (0, _interactionTypePropsHelper.propsForInteractionEdit)(inputProps);
        var asyncSuccessCb = sinon.stub();
        returnedProps.mediaUpload('file', asyncSuccessCb);
        clock.tick(1200);
        expect(asyncSuccessCb.calledWith('someReturnedUrl')).to.be.true();
        clock.restore();
      });
      it('calls addError with the right args', function () {
        var execError = function execError(_parent, _id, _file, _onSuccess, onError) {
          onError();
        };

        var addErrorStub = sinon.stub();
        var inputProps = Object.assign({}, defaultInputProps, {
          addError: addErrorStub,
          interactionFileUpload: execError
        });
        var returnedProps = (0, _interactionTypePropsHelper.propsForInteractionEdit)(inputProps);
        returnedProps.mediaUpload('file', noop);
        expect(addErrorStub.calledWith('There was an error uploading your file.')).to.be.true();
      });
    });
  });
  describe('#propsForInteractionShow', function () {
    it('returns an object with the expected keys', function () {
      var exampleItem = {
        id: '1'
      };
      var returnedProps = (0, _interactionTypePropsHelper.propsForInteractionShow)(exampleItem);
      var keySet = Object.keys(returnedProps).sort();
      var expectedKeys = ['itemId', 'interactionData', 'itemBody', 'properties', 'notifyScreenreader', 'scoringData'].sort();
      expect(keySet).to.be.eql(expectedKeys);
    });
  });
  it('replaces null item bodies with an empty string', function () {
    var newProps = (0, _interactionTypePropsHelper.propsForInteractionShow)({
      itemBody: null
    });
    expect(newProps.itemBody).to.be.eql('');
  });
});