Skip to content

add test to verify that mapStateToProps is always called with latest store state #1215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3110,6 +3110,46 @@ describe('React', () => {
expect(rendered.getByTestId('child').dataset.count).toEqual('3')
expect(rendered.getByTestId('child').dataset.prop).toEqual('b')
})

it('should invoke mapState always with latest store state', () => {
const store = createStore((state = 0) => state + 1)

let reduxCountPassedToMapState

@connect(reduxCount => {
reduxCountPassedToMapState = reduxCount
return reduxCount < 2 ? { a: 'a' } : { a: 'b' }
})
class InnerComponent extends Component {
render() {
return <Passthrough {...this.props} />
}
}

class OuterComponent extends Component {
constructor() {
super()
this.state = { count: 0 }
}

render() {
return <InnerComponent {...this.state} />
}
}

let outerComponent
rtl.render(
<ProviderMock store={store}>
<OuterComponent ref={c => (outerComponent = c)} />
</ProviderMock>
)

store.dispatch({ type: '' })
store.dispatch({ type: '' })
outerComponent.setState(({ count }) => ({ count: count + 1 }))

expect(reduxCountPassedToMapState).toEqual(3)
})
})

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