"use strict";

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

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

var _Checkbox = _interopRequireDefault(require("@instructure/ui-forms/lib/components/Checkbox"));

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

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

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

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

var _Modal = _interopRequireDefault(require("../../../../common/components/Modal"));

describe('CopyMoveBankEntryModal', function () {
  var label = 'This is my modal';
  var closeModalStub = sinon.stub();
  var focusStub = sinon.stub();
  var createBankEntryStub = sinon.spy(function (bankId, entryType, entryId) {
    return Promise.resolve({
      id: '22',
      bankId: bankId,
      entryType: entryType,
      entryId: entryId
    });
  });
  var createBankStub = sinon.stub().returns(Promise.resolve({
    id: '33'
  }));
  var createEntryStub = sinon.stub().returns(Promise.resolve({
    id: '44'
  }));
  var disassociateBankEntry = sinon.stub();
  var onAddStub = sinon.stub();
  var uiSet = sinon.stub();
  var updateBankEntryStub = sinon.spy(function (oldBankId, id, _ref) {
    var bankId = _ref.bankId;
    return Promise.resolve({
      id: id,
      bankId: bankId
    });
  });
  var modalProps = {
    closeModal: closeModalStub,
    createBank: createBankStub,
    createBankEntry: createBankEntryStub,
    createEntry: createEntryStub,
    disassociateBankEntry: disassociateBankEntry,
    entryId: '44',
    entryType: 'Item',
    label: label,
    modalOpen: true,
    onAdd: onAddStub,
    selectedBank: new _Bank.default({
      id: '0'
    }),
    uiSet: uiSet,
    updateBankEntry: updateBankEntryStub,
    modalTrigger: {
      focus: focusStub
    }
  };
  var shallowTestbed = (0, _shallowTestbed.default)(_presenter.default, modalProps);
  beforeEach(function () {
    closeModalStub.resetHistory();
    focusStub.resetHistory();
    createBankEntryStub.resetHistory();
    createBankStub.resetHistory();
    disassociateBankEntry.resetHistory();
    onAddStub.resetHistory();
    uiSet.resetHistory();
    updateBankEntryStub.resetHistory();
  });
  describe('CopyMoveBankEntryModal', 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('COPY_MOVE_BANK_ENTRY_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('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();
    });
    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('keep copy checkbox', function () {
    it('renders keep copy checkbox when source bank entry is present', function () {
      shallowTestbed.render({
        sourceBankId: '1',
        sourceBankEntryId: '1'
      });
      var keepCopyInput = shallowTestbed.find('Checkbox');
      expect(keepCopyInput.render().text()).to.include('Keep a copy in this bank');
    });
  });
  describe('onKeepCopyChange', function () {
    it('calls uiSet', function () {
      shallowTestbed.render();
      var checkbox = shallowTestbed.find(_Checkbox.default);
      checkbox.props().onChange({
        target: {
          checked: true
        }
      });
      expect(uiSet).to.have.been.calledWith('COPY_MOVE_BANK_ENTRY_MODAL_KEEP_COPY', true);
    });
  });
  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();
      expect(focusStub).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().find('Button[variant="primary"]');
      expect(addToBankButton.render().text()).to.include('Add');
    });
    describe('copy', function () {
      var copyTestbed = (0, _shallowTestbed.default)(_presenter.default, (0, _objectSpread2.default)({}, modalProps, {
        selectedBank: new _Bank.default({
          id: '1'
        }),
        keepCopy: true,
        sourceBankId: '2',
        sourceBankEntryId: '3'
      }));
      it('creates a new bank if needed', function () {
        var subject = shallowTestbed.render({}, {
          locale: 'es'
        });
        subject.setState({
          bankType: 'new',
          newBankTitle: 'my cool title'
        });
        submit(shallowTestbed);
        expect(createBankStub).to.have.been.calledWith(_Bank.default.create({
          title: 'my cool title',
          language: 'es'
        }));
      });
      it('does not create a new bank if not needed', function () {
        copyTestbed.render();
        submit(copyTestbed);
        expect(createBankStub).to.have.not.been.called();
      });
      it('creates bank entry in the bank', function (done) {
        copyTestbed.render();
        submit(copyTestbed);
        setTimeout(function () {
          expect(createBankEntryStub).to.have.been.calledWith('1', 'Item', '44');
          done();
        }, 0);
      });
      it('allows parent to create item for bank', function () {
        copyTestbed.render();
        submit(copyTestbed);
        expect(createEntryStub).to.have.been.called();
      });
      it('calls onAdd with created bank entry', function (done) {
        copyTestbed.render();
        submit(copyTestbed);
        setTimeout(function () {
          expect(onAddStub).to.have.been.calledWith({
            bankId: '1',
            id: '22',
            entryId: '44',
            entryType: 'Item'
          });
          done();
        }, 0);
      });
    });
    describe('move', function () {
      var moveTestbed = (0, _shallowTestbed.default)(_presenter.default, (0, _objectSpread2.default)({}, modalProps, {
        selectedBank: new _Bank.default({
          id: '1'
        }),
        keepCopy: false,
        sourceBankId: '2',
        sourceBankEntryId: '3'
      }));
      it('updates bank entry to selected bank', function (done) {
        moveTestbed.render();
        submit(moveTestbed);
        setTimeout(function () {
          expect(updateBankEntryStub).to.have.been.calledWith('2', '3', {
            bankId: '1'
          });
          done();
        }, 0);
      });
      it('calls onAdd with updated bank entry', function (done) {
        moveTestbed.render();
        submit(moveTestbed);
        setTimeout(function () {
          expect(onAddStub).to.have.been.calledWith(sinon.match({
            bankId: '1',
            id: '3'
          }));
          done();
        }, 0);
      });
    });
  });
});