import ReactDOM from 'react-dom'
import Button from '@instructure/ui-buttons/lib/components/Button'

import Banks from '../presenter'
import createShallowTestbed from '../../../../../tests/util/shallowTestbed'
import Page from '../../../../common/components/layout/Page'
import PaginatedCollection from '../../../../common/components/shared/PaginatedCollection'
import { BANKS_BANK_PAGINATION } from '@instructure/quiz-common/lib/constants'

describe('Banks', () => {
  const ConnectedBanks = withContext(Banks)
  const openModalStub = sinon.stub()
  const createBankStub = sinon.stub()
  const props = {
    getBanks: () => {},
    openModal: openModalStub,
    navigateToBank: () => {},
    createBank: createBankStub,
    userToken: 'token'
  }
  const testbed = createTestbed(ConnectedBanks, props)
  const shallow = createShallowTestbed(Banks, props)

  it('renders a Page component', () => {
    testbed.render()
    expect(testbed.findChildrenByType(Page)).to.have.length(1)
  })

  it('renders Banks app navigation', () => {
    testbed.render()
    expect(testbed.getText()).to.include('Item Banks')
  })

  it('renders the create bank button', () => {
    testbed.render()
    const createBankButton = testbed.findChildByType(Button)
    expect(createBankButton).to.not.be.undefined()
  })

  it('opens the modal when create bank button is clicked', () => {
    const subject = shallow.render({}, { locale: 'es' }, {disableLifecycleMethods: true})
    const createBankButton = subject.find('Button').first()
    createBankButton.props().onClick()
    expect(createBankStub).to.have.been.calledWith({ language: 'es' })
    expect(openModalStub.called).to.be.true()
  })

  it('renders a return-to-build button when passed a navigateToBuild prop', () => {
    testbed.render({ navigateToBuild () {} })
    const buttons = testbed.findChildrenByType(Button)
    const texts = buttons.map(button => ReactDOM.findDOMNode(button).innerText)
    expect(texts.join()).to.include('Return')
  })

  it('does not render a return-to-build button when not passed a navigateToBuild prop', () => {
    testbed.render({ navigateToBuild: null })
    const buttons = testbed.findChildrenByType(Button)
    const texts = buttons.map(button => ReactDOM.findDOMNode(button).innerText)
    expect(texts.join()).to.not.include('Return')
  })

  it(`renders a paginated collection keyed on ${BANKS_BANK_PAGINATION}`, () => {
    const wrapper = testbed.mount()
    const paginatedCollection = wrapper.find(PaginatedCollection)
    expect(paginatedCollection).to.have.length(1)
    expect(paginatedCollection.prop('stateKey')).to.equal(BANKS_BANK_PAGINATION)
  })

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