import { expect } from 'chai'

import alertTimerMiddleware from '../alertTimerMiddleware'
import { ADD_ALERT } from '@instructure/quiz-common/lib/constants'

import {
  removeAlert
} from '../../actions/alerts'

const dispatchStub = sinon.stub()
const fakeStore = {
  dispatch: dispatchStub
}

describe('alertTimerMiddleware', () => {
  it('sets a timeout to remove an alert', () => {
    const timeToWait = 1000
    const alertID = '12345'
    const fakeAction = {type: ADD_ALERT, payload: {uuid: alertID, removeIn: timeToWait}}
    const expectedAction = removeAlert(alertID)

    const callSyncAndAssertTime = (fn, time) => {
      fn()
      expect(time).to.be.equal(timeToWait)
    }
    alertTimerMiddleware(fakeStore)(() => {})(fakeAction, callSyncAndAssertTime)

    expect(
      dispatchStub.calledWith(expectedAction)
    ).to.be.true()

    dispatchStub.resetHistory()
  })
})
