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

import HeaderMenu from '../presenter'

import BankRecord from '../../../../common/records/Bank'
import QtiImportRecord from '../../../../common/records/QtiImport'

describe('HeaderMenuBanks', () => {
  const openModalStub = sinon.stub()
  const ConnectedHeaderMenu = withContext(HeaderMenu, {calls: {}})
  const qtiImport = new QtiImportRecord({message: 'test message'})
  const testbed = createTestbed(ConnectedHeaderMenu, {openImportModal: openModalStub})

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

  it('should render header menu without Import Content', () => {
    const bank = new BankRecord()
    sinon.stub(bank, 'getLatestImport').returns(qtiImport)

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

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

  it('should include Import Content', () => {
    const bank = new BankRecord({status: 'waiting'})
    sinon.stub(bank, 'getLatestImport').returns(qtiImport)

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

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