element-plus/packages/hooks/__tests__/use-timeout.vitest.ts
三咲智子 aaf90d99d0
test: switch to vitest (#5991)
* test: use vitest

* test: add script and ci

* chore: improve tsconfig

* refactor: use-form-item

* fix: remove unused

* chore: improve scripts

* test: improve mock

* refactor: change coverage
2022-02-21 14:28:22 +08:00

54 lines
1.1 KiB
TypeScript

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