Skip to content

Instantly share code, notes, and snippets.

@oieduardorabelo
Created December 19, 2018 13:07
Show Gist options
  • Save oieduardorabelo/f110776baa693c7dd899ed2b68d48035 to your computer and use it in GitHub Desktop.
Save oieduardorabelo/f110776baa693c7dd899ed2b68d48035 to your computer and use it in GitHub Desktop.
Module mock in Jest using `react-redux/connect` as example
const connect = jest.fn((mapStateToProps) => {
const React = require.requireActual('react');
const { rootReducer } = require.requireActual('../domains/root.reducer');
const state = rootReducer({}, { type: '__jest-mock-react-redux__' });
const fromConnectProps = mapStateToProps(state);
return jest.fn(Component => {
function Wrapper(props: any) {
const finalProps = { ...props, ...fromConnectProps };
return React.createElement(Component, finalProps);
}
return Wrapper;
});
});
export {
connect
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment