import { v4 as uuid } from 'uuid'
import { List } from 'immutable'
import ReactDOM from 'react-dom'

import Alert from '@instructure/ui-alerts/lib/components/Alert'
import ScreenReaderContent from '@instructure/ui-a11y/lib/components/ScreenReaderContent'

import Alerts from '../presenter'
import AlertRecord from '../../../records/Alert'

describe('Alerts', () => {
  const firstId = uuid()
  const secondId = uuid()
  const removeAlertStub = sinon.stub()

  const testbed = createTestbed(Alerts, {
    alerts: List([
      new AlertRecord({ message: 'hey', uuid: firstId }),
      new AlertRecord({ message: 'hola', uuid: secondId })
    ]),
    sidebarOpen: false,
    removeAlert: removeAlertStub
  })

  shouldRenderComponent(
    testbed, {}, Alerts
  )

  afterEach(() => {
    removeAlertStub.resetHistory()
  })

  it('should render given alerts', () => {
    testbed.render()
    const renderedAlerts = testbed.findChildrenByType(Alert)
    expect(renderedAlerts.length).to.be.equal(2)
  })

  it('should render screenreader alerts', () => {
    testbed.render({
      alerts: List([
        new AlertRecord({
          message: 'sr only',
          uuid: '1',
          variant: 'screenreader'
        })
      ])
    })

    const srContent = testbed.findChildrenByType(ScreenReaderContent)[0]
    const srNode = ReactDOM.findDOMNode(srContent)
    expect(srNode.innerText).to.include('sr only')
  })

  it('should call the removeAlert action when alert is dismissed', () => {
    testbed.render()
    const alert = testbed.findChildrenByType(Alert)[0]
    alert.props.onDismiss()
    expect(removeAlertStub).to.have.been.called()
  })

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