"use strict";

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

var _immutable = require("immutable");

var _presenter = _interopRequireDefault(require("../presenter"));

var _shallowTestbed = _interopRequireDefault(require("../../../../../tests/util/shallowTestbed"));

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

describe('ImportModal', function () {
  var sandbox = sinon.createSandbox();
  var setAltTextStub = sandbox.stub();
  var defaultProps = {
    acceptFileType: '.jpg, .png',
    altText: '',
    clearAltText: sandbox.stub(),
    clearFiles: sandbox.stub(),
    closeModal: sandbox.stub(),
    failureMessage: 'this is a failure message',
    files: (0, _immutable.List)([{
      name: 'test1.jpg'
    }, {
      name: 'test2.jpg'
    }]),
    fileUpload: sandbox.stub(),
    importMessage: 'this is an import message',
    modalOpen: true,
    onContinue: sandbox.stub(),
    progressValue: 0,
    receiveFile: sandbox.stub(),
    setAltText: setAltTextStub,
    setUploadStatus: sandbox.stub(),
    uploadStatus: _constants.FILE_UPLOAD_SELECT
  };
  var testbed = createTestbed(withContext(_presenter.default), defaultProps);
  var shallowTestbed = (0, _shallowTestbed.default)(_presenter.default, defaultProps);
  afterEach(function () {
    sandbox.resetHistory();
  });

  var getModal = function getModal() {
    return testbed.getRefs().composedComponent;
  };

  var sharedCancelExample = function sharedCancelExample() {
    var uploadStatus = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _constants.FILE_UPLOAD_SELECT;
    it('clears the files and closes the modal on cancel', function () {
      testbed.click(getModal().cancelImportButton);
      expect(defaultProps.clearFiles.called).to.be.true();
      expect(defaultProps.closeModal.called).to.be.true();
    });
  };

  var sharedCancelDisabledExample = function sharedCancelDisabledExample() {
    var uploadStatus = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _constants.FILE_UPLOAD_FAILURE;
    it('sets the cancelButton to be disabled', function () {
      testbed.render({
        uploadStatus: uploadStatus
      });
      expect(getModal().cancelImportButton.props.disabled).to.be.true();
    });
  };

  var sharedContinueButtonExample = function sharedContinueButtonExample() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        _ref$uploadStatus = _ref.uploadStatus,
        uploadStatus = _ref$uploadStatus === void 0 ? _constants.FILE_UPLOAD_SELECT : _ref$uploadStatus,
        _ref$files = _ref.files,
        files = _ref$files === void 0 ? (0, _immutable.List)([]) : _ref$files,
        _ref$buttonText = _ref.buttonText,
        buttonText = _ref$buttonText === void 0 ? 'Import' : _ref$buttonText,
        _ref$disabled = _ref.disabled,
        disabled = _ref$disabled === void 0 ? true : _ref$disabled;

    it("disables the continueButton is ".concat(disabled), function () {
      testbed.render({
        uploadStatus: uploadStatus,
        files: files
      });
      expect(getModal().confirmButton.props.disabled).to.eq(disabled);
    });
    it("has the text ".concat(buttonText), function () {
      testbed.render({
        uploadStatus: uploadStatus,
        files: files
      });
      expect(testbed.getText()).to.include(buttonText);
    });
  };

  describe('#componentDidUpdate', function () {
    function testComponentDidUpdate(states, shouldCall) {
      testbed.render(states[0]);
      var instance = testbed.findChildByType(_presenter.default);
      var continueActionStub = sinon.stub(instance, 'continueAction');
      expect(continueActionStub).to.not.have.been.called();
      testbed.render(states[1]);

      if (shouldCall) {
        expect(continueActionStub).to.have.been.called();
      } else {
        expect(continueActionStub).to.not.have.been.called();
      }
    }

    it('Does not continue after filedrop', function () {
      testComponentDidUpdate([{
        files: (0, _immutable.List)([]),
        disableConfirm: false
      }, {
        files: (0, _immutable.List)([{
          name: 'file1'
        }]),
        disableConfirm: false
      }], false);
    });
    context('without disableConfirm', function () {
      it('Does not continue on sucessful upload', function () {
        testComponentDidUpdate([{
          uploadStatus: _constants.FILE_UPLOAD_IMPORTING,
          disableConfirm: false
        }, {
          uploadStatus: _constants.FILE_UPLOAD_SUCCESS,
          disableConfirm: false
        }], false);
      });
    });
    context('with disableConfirm', function () {
      it('Does continue on sucessful upload', function () {
        testComponentDidUpdate([{
          uploadStatus: _constants.FILE_UPLOAD_IMPORTING,
          disableConfirm: true
        }, {
          uploadStatus: _constants.FILE_UPLOAD_SUCCESS,
          disableConfirm: true
        }], true);
      });
      it('Does not continue on failed upload', function () {
        testComponentDidUpdate([{
          uploadStatus: _constants.FILE_UPLOAD_IMPORTING,
          disableConfirm: true
        }, {
          uploadStatus: _constants.FILE_UPLOAD_FAILURE,
          disableConfirm: true
        }], false);
      });
      it('Does not continue without change (file)', function () {
        var file = {
          name: 'file1'
        };
        testComponentDidUpdate([{
          files: (0, _immutable.List)([file]),
          disableConfirm: true
        }, {
          files: (0, _immutable.List)([file]),
          disableConfirm: true
        }], false);
      });
      it('Does not continue without change (upload)', function () {
        testComponentDidUpdate([{
          uploadStatus: _constants.FILE_UPLOAD_SUCCESS,
          disableConfirm: true
        }, {
          uploadStatus: _constants.FILE_UPLOAD_SUCCESS,
          disableConfirm: true
        }], false);
      });
    });
  });
  describe('rendering with tabs', function () {
    it('should not render tabs if a single acceptFileType string is given', function () {
      shallowTestbed.render({}, null, {
        disableLifecycleMethods: true
      });
      var tabs = shallowTestbed.find('TabPanel');
      expect(tabs.length).to.equal(0);
    });
    it('should render tabs if acceptFileType is a list of types', function () {
      shallowTestbed.render({
        acceptFileType: [{
          fileType: 'image/*',
          title: 'Image'
        }, {
          fileType: 'audio/mp3, audio/wav, audio/ogg',
          title: 'Audio'
        }, {
          fileType: 'video/mp4, video/webm, video/ogg',
          title: 'Video'
        }]
      }, null, {
        disableLifecycleMethods: true
      });
      var tabs = shallowTestbed.find('TabPanel');
      expect(tabs.length).to.equal(3);
    });
    it('should update tab index on change', function () {
      var subject = shallowTestbed.render({
        acceptFileType: [{
          fileType: 'image/*',
          title: 'Image'
        }, {
          fileType: 'audio/mp3, audio/wav, audio/ogg',
          title: 'Audio'
        }, {
          fileType: 'video/mp4, video/webm, video/ogg',
          title: 'Video'
        }]
      }, null, {
        disableLifecycleMethods: true
      });
      var tabList = shallowTestbed.find('TabList');
      tabList.simulate('change', 2);
      expect(subject.state('selectedTabIndex')).to.equal(2);
    });
  });
  describe('rendering with no file selected', function () {
    var confirmButtonSpy;
    afterEach(function () {
      if (confirmButtonSpy) {
        confirmButtonSpy.restore();
        confirmButtonSpy = null;
      }
    });
    it('renders a file selector', function () {
      testbed.render({
        files: (0, _immutable.List)([])
      });
      var content = document.body.innerText;
      expect(content).to.include('this is an import message');
      expect(content).to.include('Drag & Drop here');
      expect(content).to.include('Browse');
    });
    it('moves focus to the Import button when a file is selected', function () {
      testbed.render({
        files: (0, _immutable.List)([])
      });
      var modal = getModal();
      confirmButtonSpy = sinon.spy(modal.confirmButton, 'focus');
      modal.fileSelector.props.receiveFile('FILE');
      expect(confirmButtonSpy).to.have.been.calledOnce();
    });
    it('shared examples', function () {
      testbed.render();
      sharedCancelExample();
      sharedContinueButtonExample();
    });
    context('when uploading an image', function () {
      it('calls setAltText on altText input change', function () {
        testbed.render({
          acceptFileType: 'image/*'
        });
        var input = getModal().altTextInput;
        input.props.onChange({
          target: {
            value: 'hi'
          }
        });
        expect(setAltTextStub).to.have.been.called();
      });
    });
  });
  describe('rendering with a selected file', function () {
    it('renders an active confirm button', function () {
      testbed.render();
      expect(getModal().confirmButton.props.disabled).to.be.false();
    });
    it('renders the selected file name', function () {
      testbed.render();
      var content = document.body.innerText;
      expect(content).to.include('test1.jpg');
    });
    it('attempts to import on continue', function () {
      testbed.render();
      testbed.click(getModal().confirmButton);
      expect(defaultProps.fileUpload.called).to.be.true();
      expect(defaultProps.setUploadStatus.calledWith(_constants.FILE_UPLOAD_IMPORTING)).to.be.true();
      expect(defaultProps.onContinue.called).to.be.true();
    });
    it('shared examples', function () {
      testbed.render();
      sharedCancelExample();
      sharedContinueButtonExample({
        disabled: false,
        files: defaultProps.files
      });
    });
  });
  context('with an in progress import', function () {
    it('renders the importing progress', function () {
      testbed.render({
        uploadStatus: _constants.FILE_UPLOAD_IMPORTING
      });
      expect(getModal().progressBar).to.not.be.undefined();
    });
    it('updates the progress correctly', function () {
      testbed.render({
        uploadStatus: _constants.FILE_UPLOAD_IMPORTING,
        progressValue: 50
      });
      expect(getModal().progressBar.props.valueNow).to.eq(50);
    });
    it('shared examples', function () {
      testbed.render();
      sharedCancelExample(_constants.FILE_UPLOAD_IMPORTING);
      sharedContinueButtonExample({
        uploadStatus: _constants.FILE_UPLOAD_IMPORTING,
        files: defaultProps.files
      });
    });
  });
  context('the upload errors', function () {
    it('renders the upload error message', function () {
      testbed.render({
        uploadStatus: _constants.FILE_UPLOAD_ERROR
      });
      var alert = getModal().errorAlert;
      expect(alert).to.be.ok();
      expect(alert.props.liveRegion).to.be.ok();
      expect(testbed.getText(alert)).to.include('Error');
    });
    it('clears the files selected for upload on continue', function () {
      testbed.render({
        uploadStatus: _constants.FILE_UPLOAD_ERROR
      });
      testbed.click(getModal().confirmButton);
      expect(defaultProps.clearFiles.called).to.be.true();
    });
    it('sets the file upload status to FILE_UPLOAD_SELECT', function () {
      testbed.render({
        uploadStatus: _constants.FILE_UPLOAD_ERROR
      });
      testbed.click(getModal().confirmButton);
      expect(defaultProps.setUploadStatus.calledWith(_constants.FILE_UPLOAD_SELECT)).to.be.true();
    });
    sharedCancelDisabledExample(_constants.FILE_UPLOAD_ERROR);
    sharedContinueButtonExample({
      uploadStatus: _constants.FILE_UPLOAD_ERROR,
      files: defaultProps.files,
      disabled: false,
      buttonText: 'Start Over'
    });
    it('clears the files and sets the file upload status to select', function () {
      testbed.render({
        uploadStatus: _constants.FILE_UPLOAD_ERROR
      });
      testbed.click(getModal().confirmButton);
      expect(defaultProps.clearFiles.called).to.be.true();
      expect(defaultProps.setUploadStatus.calledWith(_constants.FILE_UPLOAD_SELECT)).to.be.true();
    });
  });

  var sharedContinueCloseModalExample = function sharedContinueCloseModalExample() {
    var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        _ref2$uploadStatus = _ref2.uploadStatus,
        uploadStatus = _ref2$uploadStatus === void 0 ? _constants.FILE_UPLOAD_FAILURE : _ref2$uploadStatus;

    it('clears the files and closes the modal', function () {
      testbed.render({
        uploadStatus: uploadStatus
      });
      testbed.click(getModal().confirmButton);
      expect(defaultProps.clearFiles.called).to.be.true();
      expect(defaultProps.closeModal.called).to.be.true();
    });
  };

  context('the import fails', function () {
    it('renders the import failed message', function () {
      testbed.render({
        uploadStatus: _constants.FILE_UPLOAD_FAILURE
      });
      var alert = getModal().failureAlert;
      expect(alert).to.be.ok();
      expect(alert.props.liveRegion).to.be.ok();
      expect(testbed.getText(alert)).to.include('Error Details');
    });
    sharedCancelDisabledExample(_constants.FILE_UPLOAD_FAILURE);
    sharedContinueButtonExample({
      uploadStatus: _constants.FILE_UPLOAD_FAILURE,
      files: defaultProps.files,
      disabled: false,
      buttonText: 'Okay'
    });
    sharedContinueCloseModalExample();
  });
  context('the import is a success', function () {
    it('renders the success alert', function () {
      testbed.render({
        uploadStatus: _constants.FILE_UPLOAD_SUCCESS
      });
      var alert = getModal().successAlert;
      expect(alert).to.be.ok();
      expect(alert.props.liveRegion).to.be.ok();
      expect(testbed.getText(alert)).to.include('Success');
    });
    sharedCancelDisabledExample(_constants.FILE_UPLOAD_SUCCESS);
    sharedContinueButtonExample({
      uploadStatus: _constants.FILE_UPLOAD_SUCCESS,
      files: defaultProps.files,
      disabled: false,
      buttonText: 'Done'
    });
    sharedContinueCloseModalExample(_constants.FILE_UPLOAD_SUCCESS);
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});