Skip to content

Commit c81d596

Browse files
committed
test: update matcher tests
1 parent 8a643ac commit c81d596

File tree

1 file changed

+109
-49
lines changed

1 file changed

+109
-49
lines changed

tests/matcher.test.ts

Lines changed: 109 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
11
import { describe, it, expect } from "vitest";
22
import { createRouter, formatTree } from "./_utils";
3-
import { matchAllRoutes } from "../src";
3+
import { matchAllRoutes, type RouterContext } from "../src";
44

5-
describe("readme example", () => {
6-
const router = createRouter({
7-
"/foo": { m: "foo" },
8-
"/foo/**": { m: "foo/**", order: "2" },
9-
"/foo/bar": { m: "foo/bar" },
10-
"/foo/bar/baz": { m: "foo/bar/baz", order: "4" },
11-
"/foo/*/baz": { m: "foo/*/baz", order: "3" },
12-
"/**": { m: "/**", order: "1" },
13-
});
5+
// Helper to make snapsots more readable
6+
const _matchAllRoutes = (
7+
ctx: RouterContext,
8+
method: string = "",
9+
path: string,
10+
) => matchAllRoutes(ctx, method, path).map((m: any) => m.path);
11+
12+
describe("matcher: basic", () => {
13+
const router = createRouter([
14+
"/foo",
15+
"/foo/**",
16+
"/foo/bar",
17+
"/foo/bar/baz",
18+
"/foo/*/baz",
19+
"/**",
20+
]);
1421

1522
it("snapshot", () => {
1623
expect(formatTree(router.root)).toMatchInlineSnapshot(`
1724
"<root>
18-
├── /foo ┈> [GET] {"m":"foo"}
19-
│ ├── /bar ┈> [GET] {"m":"foo/bar"}
20-
│ │ ├── /baz ┈> [GET] {"m":"foo/bar/baz","order":"4"}
25+
├── /foo ┈> [GET] /foo
26+
│ ├── /bar ┈> [GET] /foo/bar
27+
│ │ ├── /baz ┈> [GET] /foo/bar/baz
2128
│ ├── /*
22-
│ │ ├── /baz ┈> [GET] {"m":"foo/*/baz","order":"3"}
23-
│ ├── /** ┈> [GET] {"m":"foo/**","order":"2"}
24-
├── /** ┈> [GET] {"m":"/**","order":"1"}"
29+
│ │ ├── /baz ┈> [GET] /foo/*/baz
30+
│ ├── /** ┈> [GET] /foo/**
31+
├── /** ┈> [GET] /**"
2532
`);
2633
});
2734

2835
it("matches /foo/bar/baz pattern", () => {
29-
const matches = matchAllRoutes(router, "", "/foo/bar/baz");
30-
expect(matches).to.toMatchInlineSnapshot(`[]`);
36+
const matches = _matchAllRoutes(router, "GET", "/foo/bar/baz");
37+
expect(matches).to.toMatchInlineSnapshot(`
38+
[
39+
"/**",
40+
"/foo/**",
41+
"/foo/*/baz",
42+
"/foo/bar/baz",
43+
]
44+
`);
3145
});
3246
});
3347

34-
describe("route matcher", () => {
48+
describe("matcher: complex", () => {
3549
const router = createRouter([
3650
"/",
3751
"/foo",
@@ -66,49 +80,95 @@ describe("route matcher", () => {
6680
});
6781

6882
it("can match routes", () => {
69-
expect(matchAllRoutes(router, "", "/")).to.toMatchInlineSnapshot(`[]`);
70-
expect(matchAllRoutes(router, "", "/foo")).to.toMatchInlineSnapshot(`[]`);
71-
expect(matchAllRoutes(router, "", "/foo/bar")).to.toMatchInlineSnapshot(
72-
`[]`,
73-
);
74-
expect(matchAllRoutes(router, "", "/foo/baz")).to.toMatchInlineSnapshot(
75-
`[]`,
76-
);
77-
expect(matchAllRoutes(router, "", "/foo/123/sub")).to.toMatchInlineSnapshot(
78-
`[]`,
79-
);
80-
expect(matchAllRoutes(router, "", "/foo/123")).to.toMatchInlineSnapshot(
81-
`[]`,
82-
);
83+
expect(_matchAllRoutes(router, "GET", "/")).to.toMatchInlineSnapshot(`
84+
[
85+
"/",
86+
]
87+
`);
88+
expect(_matchAllRoutes(router, "GET", "/foo")).to.toMatchInlineSnapshot(`
89+
[
90+
"/foo/**",
91+
"/foo",
92+
]
93+
`);
94+
expect(_matchAllRoutes(router, "GET", "/foo/bar")).to
95+
.toMatchInlineSnapshot(`
96+
[
97+
"/foo/**",
98+
"/foo/*",
99+
"/foo/bar",
100+
]
101+
`);
102+
expect(_matchAllRoutes(router, "GET", "/foo/baz")).to
103+
.toMatchInlineSnapshot(`
104+
[
105+
"/foo/**",
106+
"/foo/*",
107+
"/foo/baz/**",
108+
"/foo/baz",
109+
]
110+
`);
111+
expect(_matchAllRoutes(router, "GET", "/foo/123/sub")).to
112+
.toMatchInlineSnapshot(`
113+
[
114+
"/foo/**",
115+
"/foo/*/sub",
116+
]
117+
`);
118+
expect(_matchAllRoutes(router, "GET", "/foo/123")).to
119+
.toMatchInlineSnapshot(`
120+
[
121+
"/foo/**",
122+
"/foo/*",
123+
]
124+
`);
83125
});
84126

85127
it("trailing slash", () => {
86128
// Defined with trailing slash
87-
expect(
88-
matchAllRoutes(router, "", "/with-trailing"),
89-
).to.toMatchInlineSnapshot(`[]`);
90-
expect(matchAllRoutes(router, "", "/with-trailing")).toMatchObject(
91-
matchAllRoutes(router, "", "/with-trailing/"),
129+
expect(_matchAllRoutes(router, "GET", "/with-trailing")).to
130+
.toMatchInlineSnapshot(`
131+
[
132+
"/with-trailing/",
133+
]
134+
`);
135+
expect(_matchAllRoutes(router, "GET", "/with-trailing")).toMatchObject(
136+
_matchAllRoutes(router, "GET", "/with-trailing/"),
92137
);
93138

94139
// Defined without trailing slash
95-
expect(
96-
matchAllRoutes(router, "", "/without-trailing"),
97-
).to.toMatchInlineSnapshot(`[]`);
98-
expect(matchAllRoutes(router, "", "/without-trailing")).toMatchObject(
99-
matchAllRoutes(router, "", "/without-trailing/"),
140+
expect(_matchAllRoutes(router, "GET", "/without-trailing")).to
141+
.toMatchInlineSnapshot(`
142+
[
143+
"/without-trailing",
144+
]
145+
`);
146+
expect(_matchAllRoutes(router, "GET", "/without-trailing")).toMatchObject(
147+
_matchAllRoutes(router, "GET", "/without-trailing/"),
100148
);
101149
});
102150

103151
it("prefix overlap", () => {
104-
expect(matchAllRoutes(router, "", "/c/123")).to.toMatchInlineSnapshot(`[]`);
105-
expect(matchAllRoutes(router, "", "/c/123")).toMatchObject(
106-
matchAllRoutes(router, "", "/c/123/"),
152+
expect(_matchAllRoutes(router, "GET", "/c/123")).to.toMatchInlineSnapshot(
153+
`
154+
[
155+
"/c/**",
156+
]
157+
`,
107158
);
108-
expect(matchAllRoutes(router, "", "/c/123")).toMatchObject(
109-
matchAllRoutes(router, "", "/c"),
159+
expect(_matchAllRoutes(router, "GET", "/c/123")).toMatchObject(
160+
_matchAllRoutes(router, "GET", "/c/123/"),
161+
);
162+
expect(_matchAllRoutes(router, "GET", "/c/123")).toMatchObject(
163+
_matchAllRoutes(router, "GET", "/c"),
110164
);
111165

112-
expect(matchAllRoutes(router, "", "/cart")).to.toMatchInlineSnapshot(`[]`);
166+
expect(_matchAllRoutes(router, "GET", "/cart")).to.toMatchInlineSnapshot(
167+
`
168+
[
169+
"/cart",
170+
]
171+
`,
172+
);
113173
});
114174
});

0 commit comments

Comments
 (0)