import React from 'react'
import Avatar from '@instructure/ui-elements/lib/components/Avatar'
import {
  expect,
  mount,
  findWithText
} from '@instructure/ui-test-utils'

import Spinner from '../../../../../common/components/shared/spinner/Spinner'

import ModerateTable from '../presenter'
import ModerateTableRow from '../../ModerateTableRow'

describe('ModerateTable presenter', () => {
  const props = {
    isFetching: false,
    openTray: false,
    tableData: [
      { avatar: 'zomgModerateTablePresenter', id: '42', name: 'lolwut', quizSessionIds: ['1'] },
      { avatar: 'lulzModerateTablePresenter', id: '77', name: 'wutlol', quizSessionIds: ['2'] }
    ],
    onModerateButtonClick () {},
    onModerateLogClick () {}
  }

  const ContextTable = withContext(ModerateTable, {})
  const testbed = createTestbed(ContextTable, props)

  shouldRenderComponent(testbed, {}, ModerateTable)
  stubComponent(Avatar)

  it('renders accommodations column when callback is provided', async () => {
    await mount(
      <ContextTable
        {...props}
        onAccommodationsClick={Function.prototype}
      />
    )
    expect(await findWithText('Accommodations')).to.exist()
  })

  it('does not render accommodations column when collback is not provided', async () => {
    await mount(
      <ContextTable {...props} />
    )
    expect(await findWithText('Accommodations', { expectEmpty: true })).not.to.exist()
  })

  it('renders a spinner while fetching', () => {
    testbed.render({ isFetching: true })
    const result = testbed.findChildByType(Spinner)
    expect(result).to.be.ok()
  })

  it('includes a helpful caption', () => {
    testbed.render()
    const output = testbed.getText()
    expect(output).to.include('Moderation Table')
  })

  it('includes a helpful message without table data', () => {
    testbed.render({ tableData: [] })
    const output = testbed.getText()
    expect(output).to.include('There is no moderation information')
  })

  it('allows for providing a custom emptyMessage via props', () => {
    testbed.render({ tableData: [], emptyMessage: 'quizzin aint easy' })
    const output = testbed.getText()
    expect(output).to.include('quizzin aint easy')
  })

  it('renders ModerateTableRow children as expected', () => {
    testbed.render()
    const result = testbed.findChildrenByType(ModerateTableRow)
    expect(result).to.have.length(2)
  })

  it('passes onModerateLogClick to table row children', () => {
    testbed.render()
    const { onModerateLogClick } = testbed.findChildrenByType(ModerateTableRow)[0].props
    expect(onModerateLogClick).to.eq(props.onModerateLogClick)
  })
})
