-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathrenderHook.ts
84 lines (72 loc) · 1.9 KB
/
renderHook.ts
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
import { useState, useCallback, useEffect } from 'react'
import { renderHook } from 'react-hooks-testing-library'
const useCounter = (initialCount: number = 0) => {
const [count, setCount] = useState(initialCount)
const incrementBy = useCallback(
(n: number) => {
setCount(count + n)
},
[count]
)
const decrementBy = useCallback(
(n: number) => {
setCount(count - n)
},
[count]
)
return {
count,
incrementBy,
decrementBy
}
}
function checkTypesWithNoInitialProps() {
const { result, unmount, rerender } = renderHook(() => useCounter())
// check types
const _result: {
current: {
count: number
incrementBy: (_: number) => void
decrementBy: (_: number) => void
}
} = result
const _unmount: () => boolean = unmount
const _rerender: () => void = rerender
}
function checkTypesWithInitialProps() {
const { result, unmount, rerender } = renderHook(({ count }) => useCounter(count), {
initialProps: { count: 10 }
})
// check types
const _result: {
current: {
count: number
incrementBy: (_: number) => void
decrementBy: (_: number) => void
}
} = result
const _unmount: () => boolean = unmount
const _rerender: (_?: { count: number }) => void = rerender
}
function checkTypesWhenHookReturnsVoid() {
const { result, unmount, rerender } = renderHook(() => useEffect(() => {}))
// check types
const _result: {
current: void
} = result
const _unmount: () => boolean = unmount
const _rerender: () => void = rerender
}
function checkTypesWithError() {
const { result } = renderHook(() => useCounter())
// check types
const _result: {
error: Error
} = result
}
async function checkTypesForWaitForNextUpdate() {
const { waitForNextUpdate } = renderHook(() => {})
await waitForNextUpdate()
// check type
const _waitForNextUpdate: () => Promise<void> = waitForNextUpdate
}