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

feat: allow path on index routes #8148

Merged
merged 2 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 37 additions & 1 deletion packages/react-router/__tests__/index-routes-test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { matchRoutes } from "react-router";
import * as React from "react";
import { create as createTestRenderer } from "react-test-renderer";
import { matchRoutes, MemoryRouter, Routes, Route, Outlet } from "react-router";

describe("index route matching", () => {
it("throws when the index route has children", () => {
Expand All @@ -21,4 +23,38 @@ describe("index route matching", () => {
);
}).toThrow("must not have child routes");
});

it("matches path on index route", () => {
let renderer = createTestRenderer(
<MemoryRouter initialEntries={["/users"]}>
<Routes>
<Route index path="users" element={<h1>Users</h1>} />
</Routes>
</MemoryRouter>
);

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<h1>
Users
</h1>
`);
});

it("throws when the index route with path has children", () => {
expect(() => {
matchRoutes(
[
{
path: "/users",
index: true,
children: [
// This config is not valid because index routes cannot have children
{ path: ":id" }
]
}
],
"/users/mj"
);
}).toThrow("must not have child routes");
});
});
2 changes: 2 additions & 0 deletions packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ export interface LayoutRouteProps {
}

export interface IndexRouteProps {
caseSensitive?: boolean;
element?: React.ReactElement | null;
index: true;
path?: string;
}

/**
Expand Down