Access the location object of a memory router for testing purposes #11200
Replies: 2 comments 4 replies
-
yeah, |
Beta Was this translation helpful? Give feedback.
-
Just sharing another tip here since I was looking into this sort of thing today on react-router v7. You can also stub out desired routes to check, and that way you can use findBy to verify ending up at a particular pathname, without having to extract the location state.
|
Beta Was this translation helpful? Give feedback.
-
I wasn't able to find this in the docs, so I managed to find it in the source code. I was writing unit tests using
createMemoryRouter
and wanted to check whether expected search params have been added. I noticed that I can reach the router's location object through router'sstate
property:Usually you'd want to test the impact of your search params, rather than the params themselves, but in my case I wanted to check one of the search params just to be sure.
Let me know if there is a better way to do this, and whether accessing the
state
property is fine. Until then, this works 💯EDIT: To avoid using the low-level
state
property, a good alternative is to useuseLocation()
in a pathless layout route: #11200 (reply in thread)Beta Was this translation helpful? Give feedback.
All reactions