mirror of
https://github.com/element-plus/element-plus.git
synced 2024-12-27 03:01:14 +08:00
aaf90d99d0
* 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
54 lines
1.1 KiB
TypeScript
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()
|
|
})
|
|
})
|