-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
matchRoutes-test.tsx
397 lines (373 loc) · 9.62 KB
/
matchRoutes-test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import * as React from "react";
import type { RouteObject } from "react-router";
import { matchRoutes } from "react-router";
function pickPaths(
routes: RouteObject[],
pathname: string,
basename?: string
): string[] | null {
let matches = matchRoutes(routes, pathname, basename);
return matches && matches.map((match) => match.route.path || "");
}
describe("matchRoutes", () => {
let userEditRoute: RouteObject = {
path: "edit",
element: <h1>User Edit</h1>,
};
let userProfileRoute: RouteObject = {
path: ":id",
element: <h1>User Profile</h1>,
children: [userEditRoute],
};
let usersRoute: RouteObject = {
path: "/users",
element: <h1>Users</h1>,
children: [{ index: true, element: <h1>Index</h1> }, userProfileRoute],
};
let indexWithPathRoute: RouteObject = {
path: "/withpath",
index: true,
};
let layoutRouteIndex: RouteObject = {
path: "/layout",
index: true,
element: <h1>Layout</h1>,
};
let layoutRoute: RouteObject = {
path: "/layout",
children: [
{ path: "item", element: <h1>Item</h1> },
{ path: ":id", element: <h1>ID</h1> },
{ path: "*", element: <h1>Not Found</h1> },
],
};
let routes: RouteObject[] = [
{ path: "/", element: <h1>Root Layout</h1> },
{
path: "/home",
element: <h1>Home</h1>,
children: [
{ index: true, element: <h1>Index</h1> },
{ path: "*", element: <h1>Not Found</h1> },
],
},
indexWithPathRoute,
layoutRoute,
layoutRouteIndex,
usersRoute,
{ path: "*", element: <h1>Not Found</h1> },
];
it("matches root * routes correctly", () => {
expect(pickPaths(routes, "/not-found")).toEqual(["*"]);
expect(pickPaths(routes, "/hometypo")).toEqual(["*"]);
});
it("matches index routes with path correctly", () => {
expect(pickPaths(routes, "/withpath")).toEqual(["/withpath"]);
});
it("matches index routes with path over layout", () => {
expect(matchRoutes(routes, "/layout")?.[0].route.index).toBe(true);
expect(pickPaths(routes, "/layout")).toEqual(["/layout"]);
});
it("matches static path over index", () => {
expect(pickPaths(routes, "/layout/item")).toEqual(["/layout", "item"]);
});
it("matches dynamic layout path with param over index", () => {
expect(pickPaths(routes, "/layout/id")).toEqual(["/layout", ":id"]);
});
it("matches dynamic layout path with splat over index", () => {
expect(pickPaths(routes, "/layout/id/more")).toEqual(["/layout", "*"]);
});
it("matches nested index routes correctly", () => {
expect(pickPaths(routes, "/users")).toEqual(["/users", ""]);
});
it("matches nested dynamic routes correctly", () => {
expect(pickPaths(routes, "/users/mj")).toEqual(["/users", ":id"]);
expect(pickPaths(routes, "/users/mj/edit")).toEqual([
"/users",
":id",
"edit",
]);
});
it("matches nested dynamic routes with params ending in = (e.x. base64 encoded Id)", () => {
expect(pickPaths(routes, "/users/VXNlcnM6MQ==")).toEqual(["/users", ":id"]);
expect(pickPaths(routes, "/users/VXNlcnM6MQ==/edit")).toEqual([
"/users",
":id",
"edit",
]);
});
it("matches nested * routes correctly", () => {
expect(pickPaths(routes, "/home/typo")).toEqual(["/home", "*"]);
});
it("returns the same route object on match.route as the one that was passed in", () => {
let matches = matchRoutes(routes, "/users/mj")!;
expect(matches[0].route).toBe(usersRoute);
expect(matches[1].route).toBe(userProfileRoute);
});
it("matches routes with special characters", () => {
// Ensure we can use special characters in any number of ways:
// As a static route: /✅
// As a static child of root layout route: /✅
// As a static child of a static layout route: /static/✅
// As a static child of a param layout route: /:param/✅
// As a root splat route: /*
// As a root param route: /:param
// As a child splat route: /splat/*
// As a child param route: /dynamic/:param/
let getStaticRoutes = (char) => [{ path: char }];
let getNestedStaticRoutes = (char) => [
{
path: "/",
children: [{ path: char }],
},
{
path: "/static",
children: [{ path: char }],
},
{
path: "/:param",
children: [{ path: char }],
},
];
let rootSplatRoutes = [
{
index: true,
},
{
path: "*",
},
];
let rootParamRoute = [
{
index: true,
},
{
path: ":param",
},
];
let nestedRoutes = [
{
index: true,
},
{
path: "dynamic",
children: [
{
path: ":param",
},
],
},
{
path: "splat",
children: [
{
path: "*",
},
],
},
];
let specialChars = [
// Include non-special characters here to ensure the behavior is the same
"x",
"X",
"!",
"@",
"$",
"^",
"&",
"*",
"(",
")",
"_",
"+",
"-",
"=",
"~",
"{",
"}",
"[",
"]",
"|",
"<",
">",
".",
",",
":",
";",
"🤯",
"✅",
"🔥",
"ä",
"Ä",
"ø",
"山",
"人",
"口",
"刀",
"木",
];
specialChars.forEach((char) => {
expect(matchRoutes(rootSplatRoutes, `/${char}`)).toEqual([
{
params: {
"*": char,
},
pathname: `/${char}`,
pathnameBase: "/",
route: expect.objectContaining({
path: "*",
}),
},
]);
expect(matchRoutes(rootParamRoute, `/${char}`)).toEqual([
{
params: {
param: char,
},
pathname: `/${char}`,
pathnameBase: `/${char}`,
route: expect.objectContaining({
path: ":param",
}),
},
]);
expect(matchRoutes(nestedRoutes, `/dynamic/${char}`)).toEqual([
{
params: {
param: char,
},
pathname: "/dynamic",
pathnameBase: "/dynamic",
route: expect.objectContaining({
path: "dynamic",
}),
},
{
params: {
param: char,
},
pathname: `/dynamic/${char}`,
pathnameBase: `/dynamic/${char}`,
route: {
path: ":param",
},
},
]);
expect(matchRoutes(nestedRoutes, `/splat/${char}`)).toEqual([
{
params: {
"*": char,
},
pathname: "/splat",
pathnameBase: "/splat",
route: expect.objectContaining({
path: "splat",
}),
},
{
params: {
"*": char,
},
pathname: `/splat/${char}`,
pathnameBase: `/splat`,
route: {
path: "*",
},
},
]);
// This just becomes a splat/param route - skip it for static routes :)
if (char === "*" || char === ":") {
return;
}
expect(matchRoutes(getStaticRoutes(char), `/${char}`)).toEqual([
{
params: {},
pathname: `/${char}`,
pathnameBase: `/${char}`,
route: expect.objectContaining({
path: char,
}),
},
]);
expect(matchRoutes(getNestedStaticRoutes(char), `/${char}`)).toEqual([
{
params: {},
pathname: `/`,
pathnameBase: `/`,
route: expect.objectContaining({
path: "/",
}),
},
{
params: {},
pathname: `/${char}`,
pathnameBase: `/${char}`,
route: expect.objectContaining({
path: char,
}),
},
]);
expect(
matchRoutes(getNestedStaticRoutes(char), `/static/${char}`)
).toEqual([
{
params: {},
pathname: `/static`,
pathnameBase: `/static`,
route: expect.objectContaining({
path: "/static",
}),
},
{
params: {},
pathname: `/static/${char}`,
pathnameBase: `/static/${char}`,
route: expect.objectContaining({
path: char,
}),
},
]);
expect(matchRoutes(getNestedStaticRoutes(char), `/foo/${char}`)).toEqual([
{
params: {
param: "foo",
},
pathname: `/foo`,
pathnameBase: `/foo`,
route: expect.objectContaining({
path: "/:param",
}),
},
{
params: {
param: "foo",
},
pathname: `/foo/${char}`,
pathnameBase: `/foo/${char}`,
route: expect.objectContaining({
path: char,
}),
},
]);
});
});
describe("with a basename", () => {
it("matches a pathname that starts with the basename", () => {
expect(pickPaths(routes, "/app/users/mj", "/app")).toEqual([
"/users",
":id",
]);
// basename should not be case-sensitive
expect(pickPaths(routes, "/APP/users/mj", "/app")).toEqual([
"/users",
":id",
]);
});
it("does not match a pathname that does not start with the basename", () => {
expect(pickPaths(routes, "/home", "/app")).toBeNull();
});
it("does not match a pathname that does not start with basename/", () => {
expect(pickPaths(routes, "/appextra/home", "/app")).toBeNull();
});
});
});