-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathreact-hooks.js
119 lines (98 loc) · 2.82 KB
/
react-hooks.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* This is the recommended way to test reusable custom react hooks.
* It is not however recommended to use the testHook utility to test
* single-use custom hooks. Typically those are better tested by testing
* the component that is using it.
*/
import {testHook, act, cleanup} from 'react-testing-library'
import {useCounter, useDocumentTitle, useCall} from '../react-hooks'
afterEach(cleanup)
describe('useCounter', () => {
test('accepts default initial values', () => {
let count
testHook(() => ({count} = useCounter()))
expect(count).toBe(0)
})
test('accepts a default initial value for `count`', () => {
let count
testHook(() => ({count} = useCounter({})))
expect(count).toBe(0)
})
test('provides an `increment` function', () => {
let count, increment
testHook(() => ({count, increment} = useCounter({step: 2})))
expect(count).toBe(0)
act(() => {
increment()
})
expect(count).toBe(2)
})
test('provides an `decrement` function', () => {
let count, decrement
testHook(() => ({count, decrement} = useCounter({step: 2})))
expect(count).toBe(0)
act(() => {
decrement()
})
expect(count).toBe(-2)
})
test('accepts a default initial value for `step`', () => {
let count, increment
testHook(() => ({count, increment} = useCounter({})))
expect(count).toBe(0)
act(() => {
increment()
})
expect(count).toBe(1)
})
})
// using unmount function to check useEffect behavior when unmounting
describe('useDocumentTitle', () => {
test('sets a title', () => {
document.title = 'original title'
testHook(() => {
useDocumentTitle('modified title')
})
expect(document.title).toBe('modified title')
})
test('returns to original title when component is unmounted', () => {
document.title = 'original title'
const {unmount} = testHook(() => {
useDocumentTitle('modified title')
})
unmount()
expect(document.title).toBe('original title')
})
})
// using rerender function to test calling useEffect multiple times
describe('useCall', () => {
test('calls once on render', () => {
const spy = jest.fn()
testHook(() => {
useCall(spy, [])
})
expect(spy).toHaveBeenCalledTimes(1)
})
test('calls again if deps change', () => {
let deps = [false]
const spy = jest.fn()
const {rerender} = testHook(() => {
useCall(spy, deps)
})
expect(spy).toHaveBeenCalledTimes(1)
deps = [true]
rerender()
expect(spy).toHaveBeenCalledTimes(2)
})
test('does not call again if deps are the same', () => {
let deps = [false]
const spy = jest.fn()
const {rerender} = testHook(() => {
useCall(spy, deps)
})
expect(spy).toHaveBeenCalledTimes(1)
deps = [false]
rerender()
expect(spy).toHaveBeenCalledTimes(1)
})
})