|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | 2 | import { createRouter, formatTree } from "./_utils"; |
3 | | -import { matchAllRoutes } from "../src"; |
| 3 | +import { matchAllRoutes, type RouterContext } from "../src"; |
4 | 4 |
|
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 | + ]); |
14 | 21 |
|
15 | 22 | it("snapshot", () => { |
16 | 23 | expect(formatTree(router.root)).toMatchInlineSnapshot(` |
17 | 24 | "<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 |
21 | 28 | │ ├── /* |
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] /**" |
25 | 32 | `); |
26 | 33 | }); |
27 | 34 |
|
28 | 35 | 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 | + `); |
31 | 45 | }); |
32 | 46 | }); |
33 | 47 |
|
34 | | -describe("route matcher", () => { |
| 48 | +describe("matcher: complex", () => { |
35 | 49 | const router = createRouter([ |
36 | 50 | "/", |
37 | 51 | "/foo", |
@@ -66,49 +80,95 @@ describe("route matcher", () => { |
66 | 80 | }); |
67 | 81 |
|
68 | 82 | 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 | + `); |
83 | 125 | }); |
84 | 126 |
|
85 | 127 | it("trailing slash", () => { |
86 | 128 | // 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/"), |
92 | 137 | ); |
93 | 138 |
|
94 | 139 | // 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/"), |
100 | 148 | ); |
101 | 149 | }); |
102 | 150 |
|
103 | 151 | 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 | + `, |
107 | 158 | ); |
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"), |
110 | 164 | ); |
111 | 165 |
|
112 | | - expect(matchAllRoutes(router, "", "/cart")).to.toMatchInlineSnapshot(`[]`); |
| 166 | + expect(_matchAllRoutes(router, "GET", "/cart")).to.toMatchInlineSnapshot( |
| 167 | + ` |
| 168 | + [ |
| 169 | + "/cart", |
| 170 | + ] |
| 171 | + `, |
| 172 | + ); |
113 | 173 | }); |
114 | 174 | }); |
0 commit comments