Skip to content

Using initialEntries with MemoryRouter for testing #12287

Closed Answered by maksnester
maksnester asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, I think I figured it out, so for initialEntries, just use paths only starting with a slash. For useParams to work your test should include some routing definition. Example

// TestableComponentWithUseParamsHookInside.spec.tsx

render(
     <MemoryRouter initialEntries={['/1111']}>
        <Routes>
          <Route
            element={
              <TestableComponentWithUseParamsHookInside />
            }
            path=":paramName" // should specify the params your component wants from the URL
          />
        </Routes>
   </MemoryRouter>
);

// TestableComponentWithUseParamsHookInside.tsx

export function TestableComponentWithUseParamsHookInside() {
  const {paramName} = useP…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by maksnester
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant