-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also: - Add (optional) `<Route index>` prop for index routes - Return original route objects from `matchRoutes` - Remove `PartialRouteObject` interface, use `RouteObject` instead - Remove `createRoutesFromArray` Fixes #7335
- Loading branch information
Showing
11 changed files
with
463 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/react-router/__tests__/absolute-path-matching-test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { RouteObject } from "react-router"; | ||
import { matchRoutes } from "react-router"; | ||
|
||
describe("absolute path matching", () => { | ||
function pickPaths(routes: RouteObject[], pathname: string) { | ||
let matches = matchRoutes(routes, { pathname }); | ||
return matches ? matches.map(match => match.route.path || "") : []; | ||
} | ||
|
||
it("matches a nested route with an absolute path", () => { | ||
let routes = [ | ||
{ | ||
path: "/users", | ||
children: [ | ||
{ index: true }, | ||
{ path: "add" }, | ||
{ path: "remove" }, | ||
{ path: "/users/:id" } | ||
] | ||
} | ||
]; | ||
|
||
expect(pickPaths(routes, "/users")).toEqual(["/users", ""]); | ||
expect(pickPaths(routes, "/users/add")).toEqual(["/users", "add"]); | ||
expect(pickPaths(routes, "/users/remove")).toEqual(["/users", "remove"]); | ||
expect(pickPaths(routes, "/users/123")).toEqual(["/users", "/users/:id"]); | ||
}); | ||
|
||
it("throws when the nested path does not begin with its parent path", () => { | ||
expect(() => { | ||
matchRoutes( | ||
[ | ||
{ | ||
path: "/users", | ||
children: [ | ||
{ path: ":id" }, | ||
// This one should throw because it doesn't begin with /users | ||
{ path: "/not/users" } | ||
] | ||
} | ||
], | ||
"/users/123" | ||
); | ||
}).toThrow("absolute child route path must start"); | ||
}); | ||
}); |
148 changes: 148 additions & 0 deletions
148
packages/react-router/__tests__/createRoutesFromChildren-test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import * as React from "react"; | ||
import { Route, createRoutesFromChildren } from "react-router"; | ||
|
||
describe("creating routes from JSX", () => { | ||
it("creates a route config of nested JavaScript objects", () => { | ||
expect( | ||
createRoutesFromChildren( | ||
<Route path="/"> | ||
<Route path="home" element={<h1>home</h1>} /> | ||
<Route path="about" element={<h1>about</h1>} /> | ||
<Route path="users"> | ||
<Route index element={<h1>users index</h1>} /> | ||
<Route path=":id" element={<h1>user profile</h1>} /> | ||
</Route> | ||
</Route> | ||
) | ||
).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"caseSensitive": undefined, | ||
"children": Array [ | ||
Object { | ||
"caseSensitive": undefined, | ||
"element": <Route | ||
element={ | ||
<h1> | ||
home | ||
</h1> | ||
} | ||
path="home" | ||
/>, | ||
"index": undefined, | ||
"path": "home", | ||
}, | ||
Object { | ||
"caseSensitive": undefined, | ||
"element": <Route | ||
element={ | ||
<h1> | ||
about | ||
</h1> | ||
} | ||
path="about" | ||
/>, | ||
"index": undefined, | ||
"path": "about", | ||
}, | ||
Object { | ||
"caseSensitive": undefined, | ||
"children": Array [ | ||
Object { | ||
"caseSensitive": undefined, | ||
"element": <Route | ||
element={ | ||
<h1> | ||
users index | ||
</h1> | ||
} | ||
index={true} | ||
/>, | ||
"index": true, | ||
"path": undefined, | ||
}, | ||
Object { | ||
"caseSensitive": undefined, | ||
"element": <Route | ||
element={ | ||
<h1> | ||
user profile | ||
</h1> | ||
} | ||
path=":id" | ||
/>, | ||
"index": undefined, | ||
"path": ":id", | ||
}, | ||
], | ||
"element": <Route | ||
path="users" | ||
> | ||
<Route | ||
element={ | ||
<h1> | ||
users index | ||
</h1> | ||
} | ||
index={true} | ||
/> | ||
<Route | ||
element={ | ||
<h1> | ||
user profile | ||
</h1> | ||
} | ||
path=":id" | ||
/> | ||
</Route>, | ||
"index": undefined, | ||
"path": "users", | ||
}, | ||
], | ||
"element": <Route | ||
path="/" | ||
> | ||
<Route | ||
element={ | ||
<h1> | ||
home | ||
</h1> | ||
} | ||
path="home" | ||
/> | ||
<Route | ||
element={ | ||
<h1> | ||
about | ||
</h1> | ||
} | ||
path="about" | ||
/> | ||
<Route | ||
path="users" | ||
> | ||
<Route | ||
element={ | ||
<h1> | ||
users index | ||
</h1> | ||
} | ||
index={true} | ||
/> | ||
<Route | ||
element={ | ||
<h1> | ||
user profile | ||
</h1> | ||
} | ||
path=":id" | ||
/> | ||
</Route> | ||
</Route>, | ||
"index": undefined, | ||
"path": "/", | ||
}, | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { matchRoutes } from "react-router"; | ||
|
||
describe("index route matching", () => { | ||
it("throws when the index route has children", () => { | ||
expect(() => { | ||
matchRoutes( | ||
[ | ||
{ | ||
path: "/users", | ||
children: [ | ||
{ | ||
index: true, | ||
// This config is not valid because index routes cannot have children | ||
children: [{ path: "not-valid" }] | ||
}, | ||
{ path: ":id" } | ||
] | ||
} | ||
], | ||
"/users/mj" | ||
); | ||
}).toThrow("must not have child routes"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.