Skip to content

Commit 7626018

Browse files
committed
add test to verify that docs suggestion of putting a connected component above the hooks component allows dealing with stale props
1 parent 9c1b6a9 commit 7626018

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

test/hooks/useSelector.spec.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import * as rtl from 'react-testing-library'
77
import {
88
Provider as ProviderMock,
99
useSelector,
10-
shallowEqual
10+
shallowEqual,
11+
connect
1112
} from '../../src/index.js'
1213
import { useReduxContext } from '../../src/hooks/useReduxContext'
1314

@@ -333,6 +334,47 @@ describe('React', () => {
333334

334335
spy.mockRestore()
335336
})
337+
338+
it('allows dealing with stale props by putting a specific connected component above the hooks component', () => {
339+
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
340+
341+
const Parent = () => {
342+
const count = useSelector(s => s.count)
343+
return <ConnectedWrapper parentCount={count} />
344+
}
345+
346+
const ConnectedWrapper = connect(({ count }) => ({ count }))(
347+
({ parentCount }) => {
348+
return <Child parentCount={parentCount} />
349+
}
350+
)
351+
352+
let sawInconsistentState = false
353+
354+
const Child = ({ parentCount }) => {
355+
const result = useSelector(({ count }) => {
356+
if (count !== parentCount) {
357+
sawInconsistentState = true
358+
}
359+
360+
return count + parentCount
361+
})
362+
363+
return <div>{result}</div>
364+
}
365+
366+
rtl.render(
367+
<ProviderMock store={store}>
368+
<Parent />
369+
</ProviderMock>
370+
)
371+
372+
store.dispatch({ type: '' })
373+
374+
expect(sawInconsistentState).toBe(false)
375+
376+
spy.mockRestore()
377+
})
336378
})
337379

338380
describe('error handling for invalid arguments', () => {

0 commit comments

Comments
 (0)