Skip to content

Instantly share code, notes, and snippets.

@rgabs
Last active May 10, 2021 06:31
No need to export mapStateToProps and mapDispatchToProps for testing connected components!!
// This mock will make sure that we are able to access mapStateToProps, mapDispatchToProps and reactComponent in the test file.
// To use this, just do `jest.mock('react-redux');` in your page.test.js file.
const mockDispatch = jest.fn((action) => action);
module.exports = {
connect: (mapStateToProps, mapDispatchToProps) => (reactComponent) => ({
mapStateToProps,
mapDispatchToProps: (dispatch = mockDispatch, ownProps) => mapDispatchToProps(dispatch, ownProps),
reactComponent,
mockDispatch
}),
Provider: ({children}) => children
};
@a7ul
Copy link

a7ul commented Mar 28, 2018

@rgabs Thanks for this snippet !
This was exactly what I was looking for to test react-redux based components.
Thanks a ton!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment