Skip to content

Instantly share code, notes, and snippets.

@sorenlouv
Last active January 9, 2018 21:39
Show Gist options
  • Save sorenlouv/3f3119b7678c1dbcf1c4612bf255906d to your computer and use it in GitHub Desktop.
Save sorenlouv/3f3119b7678c1dbcf1c4612bf255906d to your computer and use it in GitHub Desktop.
Approaches to mocking React-router and Redux store
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