React js testing library: How to set state for MemoryRouter initialEntries to get it from useLocation inside testing component?

Viewed 47

I'm testing const deleteUserAction = () => deleteUserDispatch(user.id, navigate). It gets user.id from const { user } = useLocation().state. In original code I pass it like this without a problem:

navigate(
      {
        pathname: '/user',
        search: `?id=${id}`
      },
      { state: { user } }
)

In my test I'm trying to do

<MemoryRouter
  initialEntries={[
    { pathname: '/user', search: '?id=1', state: { user: { id: 1 } } } ]} >
    <Detail />
</MemoryRouter>

But inside Detail I get Cannot read properties of undefined (reading 'id') instead of 1. How can I pass the state to the function I'm testing to have it inside useLocation()?

0 Answers
Related