import InteractionType from 'quiz-interactions/lib/records/InteractionType'
import AddBankEntryModal from '../presenter'
import Modal from '../../../../common/components/Modal'

describe('AddBankEntryModal', () => {
  const closeModalStub = sinon.stub()
  const focusStub = sinon.stub()
  const createBankItemStub = sinon.stub()
  const getInteractionTypesStub = sinon.stub()
  const navigateToBankEntryStub = sinon.stub()
  const modalProps = {
    modalOpen: true,
    closeModal: closeModalStub,
    createBankItem: createBankItemStub,
    navigateToBankEntry: navigateToBankEntryStub,
    getInteractionTypes: getInteractionTypesStub,
    modalTrigger: {
      focus: focusStub
    }
  }
  const testbed = createTestbed(AddBankEntryModal, modalProps)
  const shallowtestbed = testbed.renderShallowly()
  const connectedTestbed = createTestbed(withContext(AddBankEntryModal), modalProps)

  afterEach(() => {
    closeModalStub.resetHistory()
    focusStub.resetHistory()
  })

  describe('Create AddBankEntryModal', () => {
    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('New Item')
    })
  })

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

    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('Callback actions', () => {
    it('calls add item', () => {
      connectedTestbed.render()
      const addBankEntryModal = connectedTestbed.findChildrenByType(AddBankEntryModal)[0]
      const type = new InteractionType({ name: 'Multiple Choice', slug: 'choice' })
      addBankEntryModal.addItem(type)
      expect(createBankItemStub.called).to.be.true()
      expect(closeModalStub.called).to.be.true()
    })

    it('calls add stimulus', () => {
      connectedTestbed.render()
      const addBankEntryModal = connectedTestbed.findChildrenByType(AddBankEntryModal)[0]
      addBankEntryModal.addStimulus()
      expect(closeModalStub.called).to.be.true()
    })
  })
})
