Skip to content

Commit

Permalink
chore: update descendant routes warning (#8202)
Browse files Browse the repository at this point in the history
  • Loading branch information
msutkowski committed Dec 9, 2021
1 parent 9412869 commit 38933fc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
44 changes: 41 additions & 3 deletions packages/react-router/__tests__/descendant-routes-warning-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,47 @@ describe("Descendant <Routes>", () => {
});

expect(consoleWarn).toHaveBeenCalledTimes(1);
expect(consoleWarn).toHaveBeenCalledWith(
expect.stringContaining("child routes will never render")
);

expect(consoleWarn.mock.calls[0][0])
.toMatch(`You rendered descendant <Routes> (or called \`useRoutes()\`) at "/courses/react" (under <Route path="react">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.
Please change the parent <Route path="react"> to <Route path="react/*">.`);
});
});

describe("when the parent route path does not have a trailing * and is the root", () => {
it("warns once when you visit the parent route and only shows a hint like *", () => {
function ReactCourses() {
return (
<div>
<h1>React</h1>

<Routes>
<Route
path="/react-fundamentals"
element={<h1>React Fundamentals</h1>}
/>
</Routes>
</div>
);
}

TestRenderer.act(() => {
TestRenderer.create(
<MemoryRouter initialEntries={["/"]}>
<Routes>
<Route path="/" element={<ReactCourses />} />
</Routes>
</MemoryRouter>
);
});

expect(consoleWarn).toHaveBeenCalledTimes(1);

expect(consoleWarn.mock.calls[0][0])
.toMatch(`You rendered descendant <Routes> (or called \`useRoutes()\`) at "/" (under <Route path="/">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.
Please change the parent <Route path="/"> to <Route path="*">.`);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ export function useRoutes(
`deeper, the parent won't match anymore and therefore the child ` +
`routes will never render.\n\n` +
`Please change the parent <Route path="${parentPath}"> to <Route ` +
`path="${parentPath}/*">.`
`path="${parentPath === "/" ? "*" : `${parentPath}/*`}">.`
);
}

Expand Down

0 comments on commit 38933fc

Please sign in to comment.