Skip to content

Commit

Permalink
Add test to ensure we are calling selector on state change
Browse files Browse the repository at this point in the history
  • Loading branch information
sufian-slack committed Aug 25, 2021
1 parent 75b2fba commit 2ac8bfc
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/hooks/useSelector.spec.js
Expand Up @@ -279,6 +279,46 @@ describe('React', () => {
expect(numCalls).toBe(2)
expect(renderedItems.length).toEqual(2)
})

it('calls selector twice once on mount when state changes during render', () => {
store = createStore(({ count } = { count: 0 }) => ({
count: count + 1,
}))

let numCalls = 0
const selector = (s) => {
numCalls += 1
return s.count
}
const renderedItems = []

const Child = () => {
useLayoutEffect(() => {
store.dispatch({ type: '', count: 1 })
}, [])
return <div />
}

const Comp = () => {
const value = useSelector(selector)
renderedItems.push(value)
return (
<div>
<Child />
</div>
)
}

rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)

// Selector first called on Comp mount, and then re-invoked after mount due to useLayoutEffect dispatching event
expect(numCalls).toBe(2)
expect(renderedItems.length).toEqual(2)
})
})

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

0 comments on commit 2ac8bfc

Please sign in to comment.