-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.ts
208 lines (193 loc) · 5.03 KB
/
utils_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
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
import {
equalsURLPattern,
inspect,
inspectURLPattern,
intersectBy,
nest,
validateURLRoutes,
} from "./utils.ts";
import { describe, expect, Fn, it } from "./dev_deps.ts";
const handler = () => new Response();
Deno.test("inspect should pass", () => {
const value = [new URLPattern({})];
expect(inspect(value)).toBe(Deno.inspect(value));
});
Deno.test("inspectURLPattern should pass", () => {
expect(inspectURLPattern(new URLPattern({}))).toBe(
String.raw`URLPattern {
protocol: "*",
username: "*",
password: "*",
hostname: "*",
port: "*",
pathname: "*",
search: "*",
hash: "*"
}`,
);
});
Deno.test("validateURLRoutes should pass", () => {
const table: Fn<typeof validateURLRoutes>[] = [
[{}, true],
[{ "/": handler }, true],
[[[
{},
handler,
]], true],
[[[
{ pathname: "", hash: "" },
handler,
], [{ pathname: "" }, handler]], true],
[{ "?": handler }, new AggregateError([])],
[[[
{ pathname: "" },
handler,
], [{ pathname: "" }, handler]], new TypeError()],
];
table.forEach(([routes, expected]) => {
expect(validateURLRoutes(routes)).toEqual(expected);
});
});
Deno.test("equalsURLPattern should pass", () => {
const table: Fn<typeof equalsURLPattern>[] = [
[new URLPattern({}), new URLPattern({}), true],
[new URLPattern({ pathname: "*" }), new URLPattern({}), true],
[
new URLPattern({ pathname: "/abc" }),
new URLPattern({ pathname: "/abc" }),
true,
],
[
new URLPattern({
port: "8000",
password: "admin",
hostname: "localhost",
}),
new URLPattern({
port: "8000",
password: "admin",
hostname: "localhost",
}),
true,
],
[
new URLPattern({ pathname: "*" }),
new URLPattern({ pathname: "" }),
false,
],
[
new URLPattern({ hash: "*" }),
new URLPattern({ hash: "" }),
false,
],
[
new URLPattern({ hostname: "*" }),
new URLPattern({ hostname: "" }),
false,
],
[
new URLPattern({ password: "*" }),
new URLPattern({ password: "" }),
false,
],
[
new URLPattern({ port: "*" }),
new URLPattern({ port: "" }),
false,
],
[
new URLPattern({ protocol: "*" }),
new URLPattern({ protocol: "" }),
false,
],
[
new URLPattern({ search: "*" }),
new URLPattern({ search: "" }),
false,
],
[
new URLPattern({ username: "*" }),
new URLPattern({ username: "" }),
false,
],
[new URLPattern({ pathname: "" }), new URLPattern({}), false],
];
table.forEach(([left, right, expected]) => {
expect(equalsURLPattern(left, right)).toBe(expected);
});
});
Deno.test("intersectBy should pass", () => {
const table: Fn<typeof intersectBy>[] = [
[[], () => false, []],
[[1], () => false, []],
[[1, 1], () => false, []],
[[1, 1], () => true, [1]],
[[0, 1, 2], () => true, [1]],
[[1, 2, 3], () => true, [2]],
[[1, 1, 1], Object.is, [1]],
[[1, 2, 1, 2], Object.is, [1, 2]],
[["", 1, 2, 3, "a", "b", "1", 3, 3, 1, 3, "A", "a", false], Object.is, [
3,
1,
"a",
]],
];
table.forEach(([value, selector, expected]) => {
expect(intersectBy(value, selector)).toEqual(expected);
});
});
describe("nest", () => {
it("should pass", () => {
const handler = () => new Response();
const table: Fn<typeof nest>[] = [
["", {}, {}],
["/", {}, {}],
["/", { "/": handler }, { "/": handler }],
["/api", { "hello": handler }, { "/api/hello": handler }],
["/api", { "hello": handler, "": handler }, {
"/api/hello": handler,
"/api": handler,
}],
["/api/", { "/hello/": handler, "//": handler }, {
"/api/hello/": handler,
"/api//": handler,
}],
["", { "/a": handler, "/a//": handler }, {
"/a": handler,
"/a//": handler,
}],
];
table.forEach(([root, routes, expected]) => {
expect(nest(root, routes)).toEqual(expected);
});
});
it("should preference first pattern", () => {
const handler = () => new Response();
const handler2 = () => new Response();
const table: Fn<typeof nest>[] = [
["", { "": handler, "/": handler2 }, { "": handler, "/": handler2 }],
["/", { "": handler, "/": handler2 }, { "/": handler }],
["/", { "/": handler, "//": handler2 }, { "/": handler, "//": handler2 }],
["/", { "/a": handler, "///a": handler2 }, {
"/a": handler,
"///a": handler2,
}],
["/", {
"/a": handler,
"///a": handler2,
"//a": handler2,
"////a": handler2,
}, {
"/a": handler,
"///a": handler2,
"//a": handler2,
"////a": handler2,
}],
["//", { "": handler, "/": handler2 }, { "//": handler }],
["/", { "/a": handler, "a": handler2 }, { "/a": handler }],
];
table.forEach(([root, routes, expected]) => {
expect(nest(root, routes)).toEqual(expected);
});
});
});