Skip to content

Instantly share code, notes, and snippets.

@biern
Created May 28, 2017 17:30
Show Gist options
  • Save biern/610de894eb5dd66eeec9d2b421f060b6 to your computer and use it in GitHub Desktop.
Save biern/610de894eb5dd66eeec9d2b421f060b6 to your computer and use it in GitHub Desktop.
Render Route with enzyme shallow() proof of concept
import React from 'react';
import { Route } from 'react-router-dom';
export default class RouteTest extends React.PureComponent {
render() {
return (
<div>
<Route path="/hello" render={() => (<div>Hello world!</div>)} />
</div>
);
}
}
import React from 'react';
import { Router, Route } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory'
import RouteTest from './RouteTest';
function shallowWithRoutes(component, path) {
const history = createHistory();
const routeContext = {
router: {
history: history,
route: {
location: { pathname: path, }
},
},
}
return shallow(component).find(Route).dive({
context: routeContext,
});
};
describe('(Component) RouteTest', () => {
it('should render hello', function () {
const wrapper = shallowWithRoutes(
<RouteTest />, '/hello'
);
expect(wrapper).toMatchSnapshot();
});
it('should not render hello', function () {
const wrapper = shallowWithRoutes(
<RouteTest />, '/goodbye'
);
expect(wrapper).toMatchSnapshot();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment