-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathserver_routing_test.ts
151 lines (136 loc) · 6.76 KB
/
server_routing_test.ts
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
import { assertEquals } from "std/testing/asserts.ts";
import { dirname, join } from "std/path/mod.ts";
import { matchRoutes } from "../framework/core/router.ts";
import { initRouter } from "../server/router.ts";
Deno.test("[unit] server/router.ts: matchRoutes", async () => {
const tmpDir = await Deno.makeTempDir();
const files = [
"./routes/_404.tsx",
"./routes/_app.tsx",
"./routes/blog.tsx",
"./routes/docs.tsx",
"./routes/docs/get-started.mdx",
"./routes/docs/index.mdx",
"./routes/index.tsx",
"./routes/utils.ts",
"./routes/works.tsx",
"./routes/works/$id.tsx",
"./routes/works/$id/$page+.tsx",
"./routes/works/$id/index.tsx",
"./routes/works/$id/order.tsx",
"./routes/works/index.tsx",
"./routes/works/new.tsx",
"./routes/users/index.tsx",
"./routes/users/$uid.tsx",
"./routes/users/$uid/index.tsx",
"./routes/users/$uid/settings/$page.tsx",
"./routes/post/[date]/[...slug].tsx",
];
await Promise.all(files.map((file) => Deno.mkdir(join(tmpDir, dirname(file)), { recursive: true })));
await Promise.all(files.map((file) => Deno.writeTextFile(join(tmpDir, file), "")));
const routes = await initRouter(tmpDir, { glob: "./routes/**/*.{tsx,mdx}" });
assertEquals(routes.routes.length, files.length - 1);
assertEquals(routes.routes.filter(([_, meta]) => meta.nesting).length, 5);
let matches = matchRoutes(new URL("/", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/"]);
assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/index.tsx"]);
matches = matchRoutes(new URL("/blog", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/blog"]);
assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/blog.tsx"]);
matches = matchRoutes(new URL("/foo", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/_404"]);
assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/_404.tsx"]);
matches = matchRoutes(new URL("/docs", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/docs", "/docs/index"]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/docs.tsx",
"./routes/docs/index.mdx",
]);
matches = matchRoutes(new URL("/docs/get-started", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/docs", "/docs/get-started"]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/docs.tsx",
"./routes/docs/get-started.mdx",
]);
matches = matchRoutes(new URL("/post/2022-04-18/better-call-saul/wine-and-roses", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), [
"/_app",
"/post/2022-04-18/better-call-saul/wine-and-roses",
]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {
date: "2022-04-18",
slug: "better-call-saul/wine-and-roses",
}]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/post/[date]/[...slug].tsx",
]);
matches = matchRoutes(new URL("/works", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/index"]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, {}]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/works.tsx",
"./routes/works/index.tsx",
]);
matches = matchRoutes(new URL("/works/new", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/new"]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, {}]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/works.tsx",
"./routes/works/new.tsx",
]);
matches = matchRoutes(new URL("/works/14", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/14", "/works/14/index"]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, { id: "14" }, { id: "14" }]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/works.tsx",
"./routes/works/$id.tsx",
"./routes/works/$id/index.tsx",
]);
matches = matchRoutes(new URL("/works/14/order", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/14", "/works/14/order"]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, { id: "14" }, { id: "14" }]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/works.tsx",
"./routes/works/$id.tsx",
"./routes/works/$id/order.tsx",
]);
matches = matchRoutes(new URL("/works/14/admin/edit", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/14", "/works/14/admin/edit"]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, { id: "14" }, { id: "14", page: "admin/edit" }]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/works.tsx",
"./routes/works/$id.tsx",
"./routes/works/$id/$page+.tsx",
]);
matches = matchRoutes(new URL("/users", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/index"]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/users/index.tsx",
]);
matches = matchRoutes(new URL("/users/ije", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/ije", "/users/ije/index"]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, { uid: "ije" }, { uid: "ije" }]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/users/$uid.tsx",
"./routes/users/$uid/index.tsx",
]);
matches = matchRoutes(new URL("/users/ije/settings/profile", "http://localhost:3000"), routes);
assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/ije", "/users/ije/settings/profile"]);
assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, { uid: "ije" }, { uid: "ije", page: "profile" }]);
assertEquals(matches.map(([_, meta]) => meta.filename), [
"./routes/_app.tsx",
"./routes/users/$uid.tsx",
"./routes/users/$uid/settings/$page.tsx",
]);
});