import Tray from '@instructure/ui-overlays/lib/components/Tray'
import Portal from '@instructure/ui-portal/lib/components/Portal'
import { BANKS_BANK_PAGINATION, TRAY_ALERT_OPEN } from '@instructure/quiz-common/lib/constants'
import BanksTray from '../presenter'

describe('Banks Tray', () => {
  const clearStub = sinon.stub()
  const focusStub = sinon.stub()
  const newActiveBankStub = sinon.stub()
  const newActiveBankEntryStub = sinon.stub()
  const setUiStub = sinon.stub()

  const testbed = createTestbed(withContext(BanksTray), {
    activeBankId: '4',
    activeBankEntryId: '3',
    clear: clearStub,
    newActiveBank: newActiveBankStub,
    newActiveBankEntry: newActiveBankEntryStub,
    trayOpen: true,
    setUI: setUiStub,
    trayTrigger: { focus: focusStub }
  })

  afterEach(() => {
    clearStub.resetHistory()
    focusStub.resetHistory()
    newActiveBankStub.resetHistory()
    newActiveBankEntryStub.resetHistory()
    setUiStub.resetHistory()
  })

  shouldRenderComponent(testbed, {}, BanksTray)

  it('renders tray', () => {
    testbed.render()
    const tray = testbed.findChildByType(Tray)
    expect(tray.props.open).to.be.true()
  })

  it('sets focus and clears UI when closing the bank tray', () => {
    testbed.render()
    const tray = testbed.findChildByType(BanksTray)
    tray.closeTray()
    expect(focusStub).to.be.called()
    expect(setUiStub).to.be.called()
  })

  it('renders with a BankEntry component', () => {
    testbed.render({
      activeBankId: '4',
      activeBankEntryId: '3'
    })
    const tray = testbed.findChildByType(Tray)
    expect(tray.props.children.type.displayName).to.equal('Connect(BankEntry)')
  })

  it('renders with a Bank component', () => {
    testbed.render({
      activeBankId: '4',
      activeBankEntryId: null
    })
    const tray = testbed.findChildByType(Tray)
    expect(tray.props.children.type.displayName).to.equal('Connect(Bank)')
  })

  it('renders with a Banks component', () => {
    testbed.render({
      activeBankId: null,
      activeBankEntryId: null
    })
    const tray = testbed.findChildByType(Tray)
    expect(tray.props.children.type.displayName).to.equal('Connect(Banks)')
  })

  describe('trayAlert', () => {
    it('renders the tray alert when the prop is set', () => {
      testbed.render({ trayAlertOpen: true })
      const portals = testbed.findChildrenByType(Portal)
      expect(portals.every((portal) => portal.props.open)).to.be.true()
    })

    it('refreshes the bank tray when closed', () => {
      testbed.render()
      const tray = testbed.findChildByType(BanksTray)
      tray.handleCloseAlert()
      expect(setUiStub).to.have.been.calledWith(TRAY_ALERT_OPEN, false)
      expect(clearStub).to.have.been.calledWith(BANKS_BANK_PAGINATION)
      expect(newActiveBankStub).to.have.been.calledWith(void 0)
      expect(newActiveBankEntryStub).to.have.been.calledWith(void 0)
    })
  })

  it('should meet a11y standards', (done) => {
    testbed.render()
    testbed.checkA11yStandards(done)
  })
})
