New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Navigate redirects seemingly aren't updating the location state in unit tests #9508
Comments
I'm not able to reproduce the issue, could you provide a reproduction? The following test passes for me on 6.4.2: it("navigates", () => {
let renderer: TestRenderer.ReactTestRenderer;
let router = createMemoryRouter(
[
{
path: "pathway/:id",
element: <h1>Pathway</h1>,
},
{
path: "*",
element: <Navigate to="/pathway/123" replace />,
},
],
{
initialEntries: ["/"],
}
);
expect(router.state.location.pathname).toBe("/");
TestRenderer.act(() => {
renderer = TestRenderer.create(<RouterProvider router={router} />);
});
expect(router.state.location.pathname).toBe("/pathway/123");
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<h1>
Pathway
</h1>
`);
}); |
Thanks, I'll have a look into this. It might be monday before I can confirm now. |
If I cut and paste the above test into my project and run it I get an error:
am I missing something? |
Ah, yeah you will need to polyfill the Here's how we do it for our internal tests: https://github.com/remix-run/react-router/blob/main/packages/react-router-dom/__tests__/setup.ts |
Just FYI I'm using the latest LTS of node, so 18.12.0 |
@sonic1981 Does the above polyfill approach work for you? If so can this issue be closed out? |
This issue has been automatically closed because we haven't received a response from the original author 🙈. This automation helps keep the issue tracker clean from issues that aren't actionable. Please reach out if you have more information for us! 🙂 |
What version of React Router are you using?
6.3.0
Steps to Reproduce
Given a react component:
and routes:
When running using the
createBrowserRouter
hook, everything works as expect. Navigating to/
redirects me to/pathway/123
. I then tried the same thing in a unit test usingcreateMemoryRouter
but the path never updates:Expected Behavior
expect(router.state.location.pathname).toEqual('/pathway/123');
should return true in the unit testActual Behavior
router.state.location.pathname
always returns\
and the test times out.The text was updated successfully, but these errors were encountered: