Initial commit
This commit is contained in:
commit
afed023152
8152 files changed
+2086461
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { act, renderHook } from '@testing-library/react'
|
||||
import { vi } from 'vitest'
|
||||
import { useTimeout } from '../src/utils/useTimeout'
|
||||
|
||||
describe('useTimeout', () => {
|
||||
it('fires the callback after the delay', () => {
|
||||
vi.useFakeTimers()
|
||||
const { result } = renderHook(() => useTimeout())
|
||||
const fn = vi.fn()
|
||||
act(() => {
|
||||
result.current.schedule(fn, 1000)
|
||||
})
|
||||
expect(fn).not.toHaveBeenCalled()
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1000)
|
||||
})
|
||||
expect(fn).toHaveBeenCalledTimes(1)
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('clear() cancels pending timers', () => {
|
||||
vi.useFakeTimers()
|
||||
const { result } = renderHook(() => useTimeout())
|
||||
const fn = vi.fn()
|
||||
act(() => {
|
||||
result.current.schedule(fn, 1000)
|
||||
result.current.clear()
|
||||
})
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(5000)
|
||||
})
|
||||
expect(fn).not.toHaveBeenCalled()
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('clears pending timers on unmount (no setState-after-unmount leaks)', () => {
|
||||
vi.useFakeTimers()
|
||||
const { result, unmount } = renderHook(() => useTimeout())
|
||||
const fn = vi.fn()
|
||||
act(() => {
|
||||
result.current.schedule(fn, 1000)
|
||||
})
|
||||
unmount()
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(5000)
|
||||
})
|
||||
expect(fn).not.toHaveBeenCalled()
|
||||
vi.useRealTimers()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user