element-plus/packages/hooks/__tests__/use-timeout.spec.ts
jeremywu 6b4c04fd0a
feat(hooks): add use-timeout hook (#2439)
* feat(hooks): add use-timeout hook

- Add use timeout hook for register timeout callbacks
- Add test cases against add-timeout

* resolve linter issue
2021-07-06 07:49:49 +08:00

57 lines
1.1 KiB
TypeScript

import { mount } from '@vue/test-utils'
import useTimeout from '../use-timeout'
const _mount = (cb: () => void) => {
return mount({
setup() {
const { cancelTimeout, registerTimeout } = useTimeout()
registerTimeout(cb, 0)
return {
cancelTimeout,
}
},
render() {
return null
},
})
}
jest.useFakeTimers()
describe('use-timeout', () => {
let wrapper
const cb = jest.fn()
beforeEach(() => {
cb.mockClear()
wrapper = _mount(cb)
})
it('should register timeout correctly', async () => {
expect(cb).not.toHaveBeenCalled()
jest.runOnlyPendingTimers()
expect(cb).toHaveBeenCalled()
wrapper.unmount()
})
it('should cancel the timeout correctly', async () => {
wrapper.vm.cancelTimeout()
jest.runOnlyPendingTimers()
expect(cb).not.toHaveBeenCalled()
wrapper.unmount()
})
it('should cancel timeout before unmount', () => {
expect(cb).not.toHaveBeenCalled()
wrapper.unmount()
jest.runOnlyPendingTimers()
expect(cb).not.toHaveBeenCalled()
})
})