Skip to content
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]: window.location is not updated after a call to router.navigate() #11487

Closed
scallaway-uipath opened this issue Apr 23, 2024 · 1 comment
Labels

Comments

@scallaway-uipath
Copy link

What version of React Router are you using?

^6.23.0

Steps to Reproduce

When using React Router in a test suite, I'm observing that window.location does not get updated when a call to router.navigate() is made.


I'm trying to test a custom hook who's return value is dependent on the current state of the URL. For performance purposes, we're getting the current location of the app from window.location, rather than through useLocation() (we have a separate, but similar, hook that calls this).

The setup in the tests is the following:

  • Latest version of React Router
  • Testing with React Testing Library, powered by vitest
  • router is the result of calling createMemoryRouter() with an array of RouteObject's, which is then given to the <RouterProvider /> in the wrapper key of rendeHook() accordingly.
  • I've specified initialProps for the hook that I'm testing
  • I'm updating the URL through the use of router.navigate(), called inside an act() statement

What I'm seeing, at the point when I call rerender() after router.navigate() has updated the URL, is that the value of useLocation().pathname is not equal to the value of window.location.pathname. useLocation() is set to the value that router.navigate() just defined, but window.location is being left behind.


For what it's worth, this seems to be the opposite of #11473

Expected Behavior

window.location.pathname === useLocation().pathname after router.navigate() is called

Actual Behavior

window.location is still set to the previous location whereas useLocation is updated correctly

@scallaway-uipath scallaway-uipath changed the title [Bug]: window.location and router.state.location don't match after router.navigate [Bug]: window.location is not updated after a call to router.navigate() Apr 23, 2024
@brophdawg11
Copy link
Contributor

You're using createMemoryRouter which doesn't interact with window at all. If you need to test window you will need to use createBrowserRouter in your tests - and generally want to pass it the window instance you are using in your test setup.

However, if you're not testing specific DOM stuff I would recommend using createMemoryRouter and asserting against router.state.location in your tests for simplicity.

@brophdawg11 brophdawg11 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants