import React from 'react'
import {
  expect,
  mount,
  findWithText,
  findWithLabel,
  wait,
  stub
} from '@instructure/ui-test-utils'
import AccommodationsModal from '../index'

describe('Accommodations Modal', () => {
  const props = {
    studentName: 'Test Student'
  }
  let closeStub
  let saveStub
  let cancelStub

  beforeEach(()=>{
    closeStub = stub()
    saveStub = stub()
    cancelStub = stub()

    props.closeModal = closeStub
    props.onSubmit = saveStub
    props.onDismiss = cancelStub
  })

  it('displays when open', async () => {
    await mount(
      <AccommodationsModal
        {...props}
        open={true}
      />
    )
    expect(await findWithText('Test Student')).to.exist()
  })

  it('does not display when closed', async () => {
    await mount(
      <AccommodationsModal
        {...props}
        open={false}
      />
    )
    expect(await findWithText('Test Student', {expectEmpty: true})).not.to.exist()
  })

  describe('callbacks', () => {
    it('calls closeModal to close', async () => {
      await mount(
        <AccommodationsModal
          {...props}
          open={true}
        />
      )
      const closeButton = await findWithLabel('Close')
      await closeButton.click()
      await wait(() => {
        expect(closeStub).to.have.been.calledOnce()
      })
    })

    it('calls onSubmit and closeModal when Save is clicked', async () => {
      await mount(
        <AccommodationsModal
          {...props}
          open={true}
        />
      )
      const saveButton = await findWithLabel('Save')
      await saveButton.click()
      await wait(() => {
        expect(closeStub).to.have.been.calledOnce()
        expect(saveStub).to.have.been.calledOnce()
      })
    })

    it('calls onDismiss and closeModal when Cancel is clicked', async () => {
      await mount(
        <AccommodationsModal
          {...props}
          open={true}
        />
      )
      const cancelButton = await findWithLabel('Cancel')
      await cancelButton.click()
      await wait(() => {
        expect(closeStub).to.have.been.calledOnce()
        expect(cancelStub).to.have.been.calledOnce()
      })
    })
  })
})