Skip to content

Commit

Permalink
add test to verify that docs suggestion of putting a connected compon…
Browse files Browse the repository at this point in the history
…ent above the hooks component allows dealing with stale props
  • Loading branch information
MrWolfZ committed May 25, 2019
1 parent 9c1b6a9 commit 7626018
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion test/hooks/useSelector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import * as rtl from 'react-testing-library'
import {
Provider as ProviderMock,
useSelector,
shallowEqual
shallowEqual,
connect
} from '../../src/index.js'
import { useReduxContext } from '../../src/hooks/useReduxContext'

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

spy.mockRestore()
})

it('allows dealing with stale props by putting a specific connected component above the hooks component', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})

const Parent = () => {
const count = useSelector(s => s.count)
return <ConnectedWrapper parentCount={count} />
}

const ConnectedWrapper = connect(({ count }) => ({ count }))(
({ parentCount }) => {
return <Child parentCount={parentCount} />
}
)

let sawInconsistentState = false

const Child = ({ parentCount }) => {
const result = useSelector(({ count }) => {
if (count !== parentCount) {
sawInconsistentState = true
}

return count + parentCount
})

return <div>{result}</div>
}

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

store.dispatch({ type: '' })

expect(sawInconsistentState).toBe(false)

spy.mockRestore()
})
})

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

0 comments on commit 7626018

Please sign in to comment.