I understand that React Testing Library has an example of testing with react router, but I couldn't get it to work (I think because I am using react router V6).
Basically, I need router testing because I have details component that uses useParams() to get part of the url. I can't render the component without it.
This was my attempt to make it work (yes the page also needs apollo, although it doesn't really need redux).
const AllTheProviders = ({children, initialRoutes}) => {
return (
<ApolloProvider client={client}>
<Provider store={store}>
<MemoryRouter>
{children}
</MemoryRouter>
</Provider>
</ApolloProvider>
);
}
const customRender = (ui, options) => render(ui, {wrapper: AllTheProviders, ...options})
beforeEach(() => {
window.history.pushState({}, 'Test page',"/details/url-param-the-component-needs")
customRender(<App/>);
});
No surprise, but this didn't work. I assume window.history.pushState() doesn't work for react router V6. I tried using useNavigate(), but that doesn't work outside of a component.
If anybody has any ideas on how I could make this work. I would greatly appreciate it.