"use strict";

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

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

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

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

var _Bank = _interopRequireDefault(require("../../../../common/records/Bank"));

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

var _BankSelection = _interopRequireDefault(require("../../../../common/components/resources/BankSelection"));

var _Modal = _interopRequireDefault(require("@instructure/ui-overlays/lib/components/Modal"));

describe('AddToBankModal', function () {
  var label = 'This is my modal';
  var closeModalStub = sinon.stub();
  var onContinueStub = sinon.stub();
  var focusStub = sinon.stub();
  var createBankStub = sinon.stub().returns(Promise.resolve({
    id: '33'
  }));
  var createBankEntryStub = sinon.spy(function (bankId, entryType, entryId) {
    return Promise.resolve({
      id: '22',
      bankId: bankId,
      entryType: entryType,
      entryId: entryId
    });
  });
  var getEntryStub = sinon.stub().returns(Promise.resolve({
    id: '44'
  }));
  var onAddStub = sinon.stub();
  var uiSet = sinon.stub();
  var modalProps = {
    closeModal: closeModalStub,
    createBank: createBankStub,
    createBankEntry: createBankEntryStub,
    entryId: '44',
    entryType: 'Item',
    getEntry: getEntryStub,
    label: label,
    modalOpen: true,
    onAdd: onAddStub,
    onContinue: onContinueStub,
    selectedBank: new _Bank.default({
      id: _constants.TEMPORARY_BANK_ID
    }),
    uiSet: uiSet,
    modalTrigger: {
      focus: focusStub
    }
  };
  var shallowTestbed = (0, _shallowTestbed.default)(_presenter.default, modalProps);
  beforeEach(function () {
    closeModalStub.resetHistory();
    focusStub.resetHistory();
    createBankStub.resetHistory();
    createBankEntryStub.resetHistory();
    getEntryStub.resetHistory();
    onAddStub.resetHistory();
    uiSet.resetHistory();
  });
  describe('AddToBankModal', function () {
    it('renders the modal', function () {
      shallowTestbed.render();
      var modal = shallowTestbed.find(_Modal.default);
      expect(modal).to.have.length(1);
    });
  });
  describe('Modal Title', function () {
    it('renders the correct title for the modal', function () {
      shallowTestbed.render();
      var modalheader = shallowTestbed.find('ModalHeader');
      expect(modalheader).to.have.length(1);
      expect(modalheader.render().text()).to.be.equal(label);
    });
  });
  describe('bank type radios', function () {
    it('change the bankType based on the radio selection', function () {
      shallowTestbed.render();
      var radioGroup = shallowTestbed.find('RadioInputGroup').first();
      radioGroup.prop('onChange')(null, 'new');
      expect(shallowTestbed.subject.state('bankType')).to.equal('new');
    });
  });
  context('using an existing bank', function () {
    it('renders selection for bank', function () {
      shallowTestbed.render();
      var bankInput = shallowTestbed.find(_BankSelection.default);
      expect(bankInput.length).to.equal(1);
    });
    it('calls uiSet on bank selection', function () {
      shallowTestbed.render();
      var bankSelection = shallowTestbed.find(_BankSelection.default);
      bankSelection.props().onBankSelection({}, {
        value: 'bank'
      });
      expect(uiSet).to.have.been.calledWith('ADD_TO_BANK_MODAL_BANK_SELECTION', 'bank');
    });
  });
  context('using a new bank', function () {
    it('renders an input for the new bank title', function () {
      var subject = shallowTestbed.render();
      subject.setState({
        bankType: 'new'
      });
      var textInput = shallowTestbed.find('TextInput');
      expect(textInput.length).to.equal(1);
    });
    it('updates the state when the input changes', function () {
      var subject = shallowTestbed.render();
      subject.setState({
        bankType: 'new'
      });
      var textInput = shallowTestbed.find('TextInput').first();
      textInput.prop('onChange')({
        target: {
          value: 'some new title'
        }
      });
      expect(subject.state('newBankTitle')).to.equal('some new title');
    });
  });
  describe('Cancel action', function () {
    it('renders cancel button', function () {
      shallowTestbed.render();
      var cancelButton = shallowTestbed.find('Button[variant="default"]');
      expect(cancelButton.render().text()).to.include('Cancel');
    });
    it('calls the cancel action', function () {
      shallowTestbed.render();
      var cancelButton = shallowTestbed.find('Button[variant="default"]');
      shallowTestbed.click(cancelButton);
      expect(closeModalStub).to.be.called();
    });
  });
  describe('Add to Bank action', function () {
    var submit = function submit(testbed) {
      var addToBankButton = testbed.find('Button[variant="primary"]');
      testbed.click(addToBankButton);
    };

    it('renders the add to bank button', function () {
      shallowTestbed.render();
      var modalfooter = shallowTestbed.find('ModalFooter');
      var addToBankButton = modalfooter.shallow({
        disableLifecycleMethods: true
      }).find('Button[variant="primary"]');
      expect(addToBankButton.render().text()).to.include('Add');
    });
    it('renders a disabled button if there is no title for the new bank', function () {
      var subject = shallowTestbed.render();
      subject.setState({
        bankType: 'new',
        newBankTitle: '   '
      });
      var addToBankButton = shallowTestbed.find('Button[variant="primary"]');
      expect(addToBankButton.prop('disabled')).to.be.true();
    });
    describe('add', function () {
      var addTestbed = (0, _shallowTestbed.default)(_presenter.default, (0, _objectSpread2.default)({}, modalProps, {
        selectedBank: new _Bank.default({
          id: '1'
        })
      }));
      it('closes modal', function () {
        addTestbed.render();
        submit(addTestbed);
        expect(closeModalStub).to.have.been.called();
      });
      it('calls onContinue', function () {
        addTestbed.render();
        submit(addTestbed);
        expect(onContinueStub).to.have.been.called();
      });
      it('creates a new bank if needed', function () {
        var subject = shallowTestbed.render();
        subject.setState({
          bankType: 'new',
          newBankTitle: 'my cool title'
        });
        submit(shallowTestbed);
        expect(createBankStub).to.have.been.called();
      });
      it('does not create a new bank if not needed', function () {
        addTestbed.render();
        submit(addTestbed);
        expect(createBankStub).to.have.not.been.called();
      });
      it('creates bank entry in the bank', function (done) {
        addTestbed.render();
        submit(addTestbed);
        setTimeout(function () {
          expect(createBankEntryStub).to.have.been.calledWith('1', 'Item', '44');
          done();
        }, 0);
      });
      it('allows parent to find item for bank', function () {
        addTestbed.render();
        submit(addTestbed);
        expect(getEntryStub).to.have.been.called();
      });
      it('calls onAdd with created bank entry', function (done) {
        addTestbed.render();
        submit(addTestbed);
        setTimeout(function () {
          expect(onAddStub).to.have.been.calledWith({
            bankId: '1',
            id: '22',
            entryId: '44',
            entryType: 'Item'
          });
          done();
        }, 0);
      });
    });
  });
});