Last active
January 9, 2018 21:39
-
-
Save sorenlouv/3f3119b7678c1dbcf1c4612bf255906d to your computer and use it in GitHub Desktop.
Approaches to mocking React-router and Redux store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { mount } from 'enzyme'; | |
import React from 'react'; | |
import { createMockStore } from 'redux-test-utils'; | |
import { Provider } from 'react-redux'; | |
import { Router } from 'react-router-dom'; | |
import createHistory from 'history/createHashHistory'; | |
import PropTypes from 'prop-types'; | |
export const mountWithRouterAndStore = (Component, storeState = {}) => { | |
const store = createMockStore(storeState); | |
const history = createHistory(); | |
return mount( | |
<Provider store={store}> | |
<Router history={history}>{Component}</Router> | |
</Provider> | |
); | |
}; | |
export function mountWithRouterAndStore2(Component, storeState = {}) { | |
const store = createMockStore(storeState); | |
const history = createHistory(); | |
const options = { | |
context: { store, router: { history, route: { location: {} } } }, | |
childContextTypes: { | |
store: PropTypes.object.isRequired, | |
router: PropTypes.object.isRequired | |
} | |
}; | |
return mount(Component, options); | |
} | |
const reduxState = {foo: 'bar'}; | |
const wrapper = mountWithRouterAndStore(<MyComponent />, reduxState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment