Skip to content

Commit 2e7b6a1

Browse files
committed
add test to verify that mapStateToProps is always called with latest store state
1 parent 79982c9 commit 2e7b6a1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/components/connect.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3110,6 +3110,46 @@ describe('React', () => {
31103110
expect(rendered.getByTestId('child').dataset.count).toEqual('3')
31113111
expect(rendered.getByTestId('child').dataset.prop).toEqual('b')
31123112
})
3113+
3114+
it('should invoke mapState always with latest store state', () => {
3115+
const store = createStore((state = 0) => state + 1)
3116+
3117+
let reduxCountPassedToMapState
3118+
3119+
@connect(reduxCount => {
3120+
reduxCountPassedToMapState = reduxCount
3121+
return reduxCount < 2 ? { a: 'a' } : { a: 'b' }
3122+
})
3123+
class InnerComponent extends Component {
3124+
render() {
3125+
return <Passthrough {...this.props} />
3126+
}
3127+
}
3128+
3129+
class OuterComponent extends Component {
3130+
constructor() {
3131+
super()
3132+
this.state = { count: 0 }
3133+
}
3134+
3135+
render() {
3136+
return <InnerComponent {...this.state} />
3137+
}
3138+
}
3139+
3140+
let outerComponent
3141+
rtl.render(
3142+
<ProviderMock store={store}>
3143+
<OuterComponent ref={c => (outerComponent = c)} />
3144+
</ProviderMock>
3145+
)
3146+
3147+
store.dispatch({ type: '' })
3148+
store.dispatch({ type: '' })
3149+
outerComponent.setState(({ count }) => ({ count: count + 1 }))
3150+
3151+
expect(reduxCountPassedToMapState).toEqual(3)
3152+
})
31133153
})
31143154

31153155
it("should enforce top-down updates to ensure a deleted child's mapState doesn't throw errors", () => {

0 commit comments

Comments
 (0)