import Menu from '@instructure/ui-menu/lib/components/Menu'

import BuildingButtons from '../presenter'

describe('BuildingButtons', () => {
  const testbed = createTestbed(BuildingButtons, {
    quizSaving: false
  })

  function getShallowMenuItemByKey (key) {
    // shallow render menu, since items are rendered in a portal
    const menu = testbed.subject.renderMenu()
    const children = menu.props.children
    return (Array.isArray(children) ? children : [children])
      .find((child) => child.key === key)
  }

  it('shows saving if quizSaving is true', () => {
    testbed.render({
      quizSaving: true
    })

    const buttonsText = testbed.getText(testbed.rootNode)
    expect(buttonsText).to.include('Saving...')
  })

  context('popover menu', () => {
    it('shows when in a building app context', () => {
      testbed.render({
        app: 'building',
        hasQtiImport: true
      })

      const menu = testbed.findChildByType(Menu)
      expect(menu).to.exist()
    })

    it('shows when in a settings app context', () => {
      testbed.render({
        app: 'settings',
        hasQtiImport: true
      })

      const menu = testbed.findChildByType(Menu)
      expect(menu).to.exist()
    })

    it('disables when in a reporting app context', () => {
      testbed.render({
        app: 'reporting',
        hasQtiImport: true
      })

      expect(() => testbed.findChildByType(Menu)).to.throw()
    })
  })

  context('QTI import menu', () => {
    it('shows when in a building app context', () => {
      testbed.render({
        app: 'building',
        canImport: true,
        hasQtiImport: true
      })

      const importItem = getShallowMenuItemByKey('import')
      expect(importItem.props.disabled).to.be.false()
    })

    it('disables import menu item when canImport is false', () => {
      testbed.render({
        app: 'building',
        hasQtiImport: true
      })

      const importItem = getShallowMenuItemByKey('import')
      expect(importItem.props.disabled).to.be.true()
    })
  })

  context('Manage banks menu', () => {
    it('shows when in a building app context', () => {
      testbed.render({
        app: 'building'
      })

      const menu = testbed.findChildByType(Menu)
      expect(menu).to.exist()
    })

    it('does not show when disabled via props', () => {
      testbed.render({
        app: 'building',
        enableItemBanking: false
      })

      const banksItem = getShallowMenuItemByKey('banks')
      expect(banksItem).to.be.undefined()
    })

    it('passes the specified navigation callback to the underlying menu item', () => {
      const navigateToBanks = function () {}
      testbed.render({
        app: 'building',
        navigateToBanks
      })

      const menu = testbed.findChildByType(Menu)
      expect(menu).to.exist()

      const banksItem = getShallowMenuItemByKey('banks')
      expect(banksItem.props.onSelect).to.eq(navigateToBanks)
    })
  })

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