/
useSelector.spec.js
258 lines (199 loc) · 6.92 KB
/
useSelector.spec.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*eslint-disable react/prop-types*/
import React from 'react'
import { createStore } from 'redux'
import { renderHook, act } from 'react-hooks-testing-library'
import * as rtl from 'react-testing-library'
import {
Provider as ProviderMock,
useSelector,
shallowEqual
} from '../../src/index.js'
import { useReduxContext } from '../../src/hooks/useReduxContext'
describe('React', () => {
describe('hooks', () => {
describe('useSelector', () => {
let store
let renderedItems = []
beforeEach(() => {
store = createStore(({ count } = { count: -1 }) => ({
count: count + 1
}))
renderedItems = []
})
afterEach(() => rtl.cleanup())
describe('core subscription behavior', () => {
it('selects the state on initial render', () => {
const { result } = renderHook(() => useSelector(s => s.count), {
wrapper: props => <ProviderMock {...props} store={store} />
})
expect(result.current).toEqual(0)
})
it('selects the state and renders the component when the store updates', () => {
const { result } = renderHook(() => useSelector(s => s.count), {
wrapper: props => <ProviderMock {...props} store={store} />
})
expect(result.current).toEqual(0)
act(() => {
store.dispatch({ type: '' })
})
expect(result.current).toEqual(1)
})
})
describe('lifeycle interactions', () => {
it('subscribes to the store synchronously', () => {
let rootSubscription
const Parent = () => {
const { subscription } = useReduxContext()
rootSubscription = subscription
const count = useSelector(s => s.count)
return count === 1 ? <Child /> : null
}
const Child = () => {
const count = useSelector(s => s.count)
return <div>{count}</div>
}
rtl.render(
<ProviderMock store={store}>
<Parent />
</ProviderMock>
)
expect(rootSubscription.listeners.get().length).toBe(1)
store.dispatch({ type: '' })
expect(rootSubscription.listeners.get().length).toBe(2)
})
it('unsubscribes when the component is unmounted', () => {
let rootSubscription
const Parent = () => {
const { subscription } = useReduxContext()
rootSubscription = subscription
const count = useSelector(s => s.count)
return count === 0 ? <Child /> : null
}
const Child = () => {
const count = useSelector(s => s.count)
return <div>{count}</div>
}
rtl.render(
<ProviderMock store={store}>
<Parent />
</ProviderMock>
)
expect(rootSubscription.listeners.get().length).toBe(2)
store.dispatch({ type: '' })
expect(rootSubscription.listeners.get().length).toBe(1)
})
it('notices store updates between render and store subscription effect', () => {
const Comp = () => {
const count = useSelector(s => s.count)
renderedItems.push(count)
// I don't know a better way to trigger a store update before the
// store subscription effect happens
if (count === 0) {
store.dispatch({ type: '' })
}
return <div>{count}</div>
}
rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)
expect(renderedItems).toEqual([0, 1])
})
})
describe('performance optimizations and bail-outs', () => {
it('defaults to ref-equality to prevent unnecessary updates', () => {
const state = {}
store = createStore(() => state)
const Comp = () => {
const value = useSelector(s => s)
renderedItems.push(value)
return <div />
}
rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)
expect(renderedItems.length).toBe(1)
store.dispatch({ type: '' })
expect(renderedItems.length).toBe(1)
})
it('allows other equality functions to prevent unnecessary updates', () => {
store = createStore(
({ count, stable } = { count: -1, stable: {} }) => ({
count: count + 1,
stable
})
)
const Comp = () => {
const value = useSelector(s => Object.keys(s), shallowEqual)
renderedItems.push(value)
return <div />
}
rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)
expect(renderedItems.length).toBe(1)
store.dispatch({ type: '' })
expect(renderedItems.length).toBe(1)
})
})
describe('edge cases', () => {
it('ignores transient errors in selector (e.g. due to stale props)', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
const Parent = () => {
const count = useSelector(s => s.count)
return <Child parentCount={count} />
}
const Child = ({ parentCount }) => {
const result = useSelector(({ count }) => {
if (count !== parentCount) {
throw new Error()
}
return count + parentCount
})
return <div>{result}</div>
}
rtl.render(
<ProviderMock store={store}>
<Parent />
</ProviderMock>
)
expect(() => store.dispatch({ type: '' })).not.toThrowError()
spy.mockRestore()
})
it('correlates the subscription callback error with a following error during rendering', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
const Comp = () => {
const result = useSelector(count => {
if (count > 0) {
throw new Error('foo')
}
return count
})
return <div>{result}</div>
}
const store = createStore((count = -1) => count + 1)
const App = () => (
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)
rtl.render(<App />)
expect(() => store.dispatch({ type: '' })).toThrow(
/The error may be correlated/
)
spy.mockRestore()
})
})
describe('error handling for invalid arguments', () => {
it('throws if no selector is passed', () => {
expect(() => useSelector()).toThrow()
})
})
})
})
})