Skip to content

Instantly share code, notes, and snippets.

@jordanell
Created May 23, 2018 17:35
Show Gist options
  • Save jordanell/37fb333108d0aa265ed6dba3dbd82dfd to your computer and use it in GitHub Desktop.
Save jordanell/37fb333108d0aa265ed6dba3dbd82dfd to your computer and use it in GitHub Desktop.
import { mount as enzymeMount } from 'enzyme';
import createHistory from 'history/createMemoryHistory';
import React from 'react';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'react-router-redux';
import Immutable from 'seamless-immutable';
import initialize from 'src/store/initialize';
/**
* Given a component, mounts it inside of Enzyme.
*
* @param {Node} component A React node to mount.
* @param {Object} state The default state to use in the Redux store.
*
* @return {Object} The mounted Enzyme object.
*/
export default function mount(component, state = {}) {
let entry = '/';
if (state.location) entry = `${state.location.pathname || '/'}${state.location.search}`;
const history = createHistory({
initialEntries: [entry],
});
const store = initialize(Immutable.from(state), history);
return enzymeMount(
<Provider store={store}>
<ConnectedRouter history={history}>
{component}
</ConnectedRouter>
</Provider>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment