import EditBankModal from '../presenter'
import Bank from '../../../../common/records/Bank'
import Modal from '@instructure/ui-overlays/lib/components/Modal'

describe('EditBankModal', () => {
  const updateBankStub = sinon.stub()
  const closeModalStub = sinon.stub()
  const focusStub = sinon.stub()
  const modalProps = {
    modalOpen: true,
    workingBank: {
      isValid: () => true
    },
    displayError: false,
    updateBank: updateBankStub,
    changeBank: () => {},
    clearModifications: () => {},
    hideError: () => {},
    showError: () => {},
    closeModal: closeModalStub,
    modalTrigger: {
      focus: focusStub
    }
  }
  const testbed = createTestbed(EditBankModal, modalProps)
  const shallowtestbed = testbed.renderShallowly()

  describe('EditBankModal', () => {
    it('renders the modal', () => {
      const modal = shallowtestbed.find(Modal)
      expect(modal).to.have.length(1)
    })
  })

  describe('Modal Title', () => {
    it('renders the correct title for the modal', () => {
      const modalheader = shallowtestbed.find('ModalHeader')
      expect(modalheader).to.have.length(1)
      expect(modalheader.render().text()).to.be.equal('Edit Item Bank')
    })
  })

  describe('Bank Title input', () => {
    it('renders text input for bank title', () => {
      const bankInput = shallowtestbed.find('TextInput')
      expect(bankInput.render().text()).to.include('Bank Name')
    })
  })

  describe('Cancel action', () => {
    it('renders cancel button', () => {
      const cancelButton = shallowtestbed.find('Button[variant="default"]')
      expect(cancelButton.render().text()).to.include('Cancel')
    })

    it('calls the cancel action', () => {
      const cancelButton = shallowtestbed.find('Button[variant="default"]')
      cancelButton.simulate('click')
      expect(closeModalStub).to.be.called()
      expect(focusStub).to.be.called()
    })
  })

  describe('Update Bank action', () => {
    it('renders the Save Changes button', () => {
      const modalfooter = shallowtestbed.find('ModalFooter')
      const createButton = modalfooter.shallow({disableLifecycleMethods: true}).find('Button[variant="primary"]')
      expect(createButton.render().text()).to.include('Save Changes')
    })

    it('does not create the bank when the name is blank', () => {
      const errortestbed = testbed.renderShallowly(
        {
          workingBank: new Bank({id: 'temp_id'}),
          errorKey: 'temp_id_create_bank',
          displayError: true
        })
      const createButton = errortestbed.find('Button[variant="primary"]')
      createButton.simulate('click')
      const bankInput = errortestbed.find('TextInput')
      expect(bankInput.render().text()).to.include('cannot be blank')
      expect(updateBankStub.called).to.be.false()
    })

    it('updates the bank when the name is entered', () => {
      const createButton = shallowtestbed.find('Button[variant="primary"]')
      const bankInput = shallowtestbed.find('TextInput')
      bankInput.simulate('change', { target: { value: 'After Change' } })
      createButton.simulate('click')
      expect(updateBankStub.called).to.be.true()
    })
  })
})
