Skip to content

Commit 2ac8bfc

Browse files
committed
Add test to ensure we are calling selector on state change
1 parent 75b2fba commit 2ac8bfc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/hooks/useSelector.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,46 @@ describe('React', () => {
279279
expect(numCalls).toBe(2)
280280
expect(renderedItems.length).toEqual(2)
281281
})
282+
283+
it('calls selector twice once on mount when state changes during render', () => {
284+
store = createStore(({ count } = { count: 0 }) => ({
285+
count: count + 1,
286+
}))
287+
288+
let numCalls = 0
289+
const selector = (s) => {
290+
numCalls += 1
291+
return s.count
292+
}
293+
const renderedItems = []
294+
295+
const Child = () => {
296+
useLayoutEffect(() => {
297+
store.dispatch({ type: '', count: 1 })
298+
}, [])
299+
return <div />
300+
}
301+
302+
const Comp = () => {
303+
const value = useSelector(selector)
304+
renderedItems.push(value)
305+
return (
306+
<div>
307+
<Child />
308+
</div>
309+
)
310+
}
311+
312+
rtl.render(
313+
<ProviderMock store={store}>
314+
<Comp />
315+
</ProviderMock>
316+
)
317+
318+
// Selector first called on Comp mount, and then re-invoked after mount due to useLayoutEffect dispatching event
319+
expect(numCalls).toBe(2)
320+
expect(renderedItems.length).toEqual(2)
321+
})
282322
})
283323

284324
it('uses the latest selector', () => {

0 commit comments

Comments
 (0)