-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseInterval.test.tsx
More file actions
96 lines (71 loc) · 2.18 KB
/
Copy pathuseInterval.test.tsx
File metadata and controls
96 lines (71 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { cleanup, renderHook } from '@testing-library/react'
import { useInterval } from '../useInterval'
afterEach(cleanup)
jest.useFakeTimers()
test('it should execute the callback after every interval', function () {
const handler = jest.fn()
renderHook(() => {
useInterval(handler, 100)
})
expect(handler).toHaveBeenCalledTimes(0)
jest.advanceTimersByTime(500)
expect(handler).toHaveBeenCalledTimes(5)
})
test('if you pass a `delay` of `null`, the timer is "paused"', () => {
const handler = jest.fn()
renderHook(() => {
useInterval(handler, null)
})
jest.advanceTimersByTime(5000)
expect(handler).toHaveBeenCalledTimes(0)
})
test('if you pass a new `handler`, the timer will not restart', () => {
const handler1 = jest.fn()
const handler2 = jest.fn()
let handler = handler1
const { rerender } = renderHook(() => {
useInterval(handler, 1000)
})
jest.advanceTimersByTime(500)
handler = handler2
rerender()
jest.advanceTimersByTime(500)
expect(handler1).toHaveBeenCalledTimes(0)
expect(handler2).toHaveBeenCalledTimes(1)
})
test('if you pass a new `delay`, it will cancel the current timer and start a new one', () => {
const handler = jest.fn()
let delay = 500
const { rerender } = renderHook(() => {
useInterval(handler, delay)
})
jest.advanceTimersByTime(1000)
expect(handler).toHaveBeenCalledTimes(2)
delay = 1000
rerender()
jest.advanceTimersByTime(5000)
expect(handler).toHaveBeenCalledTimes(7)
})
test('if you pass a new `delay` of `null`, it will cancel the current timer and "pause"', () => {
const handler = jest.fn()
let delay: number | null = 500
const { rerender } = renderHook(() => {
useInterval(handler, delay)
})
jest.advanceTimersByTime(1000)
expect(handler).toHaveBeenCalledTimes(2)
delay = null
rerender()
jest.advanceTimersByTime(5000)
expect(handler).toHaveBeenCalledTimes(2)
})
test('passing the same parameters causes no change in the timer', () => {
const handler = jest.fn()
const { rerender } = renderHook(() => {
useInterval(handler, 1000)
})
jest.advanceTimersByTime(500)
rerender()
jest.advanceTimersByTime(500)
expect(handler).toHaveBeenCalledTimes(1)
})