Mocking React Router’s match object in component unit tests

A url parameter in current route
https://gist.github.com/adamjarling/a354755f5597ded173e83b10aaf68301
https://gist.github.com/adamjarling/9ac59f3f8984c4c19d34018ee8a0401c
// Helper function
export function renderWithRouterMatch( ui,
{
path = “/”, // ie. "/project/:id"
route = “/”, // ie. "/project/ABC123"
history = createMemoryHistory({ initialEntries: [route] })
} = {}) {
return {
…render(
<Router history={history}>
<Route path={path} component={ui} />
</Router>
)
};
}
https://gist.github.com/adamjarling/5b5f607e45d9de47f9d18423697b2535

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam J. Arling

Adam J. Arling

72 Followers

Senior Front-End Developer/Manager — Northwestern University Libraries Repository and Digital Curation