|
1 | 1 | import { assertEquals } from "std/testing/asserts.ts";
|
2 |
| -import { dirname, join } from "std/path/mod.ts"; |
3 |
| -import { matchRoutes, restoreUrl, toLocalPath } from "../lib/helpers.ts"; |
4 |
| -import { initRoutes } from "../server/routing.ts"; |
5 |
| - |
6 |
| -Deno.test("lib/helpers.ts: matchRoutes", async () => { |
7 |
| - const tmpDir = await Deno.makeTempDir(); |
8 |
| - const files = [ |
9 |
| - "./routes/_app.tsx", |
10 |
| - "./routes/_404.tsx", |
11 |
| - "./routes/about.tsx", |
12 |
| - "./routes/index.tsx", |
13 |
| - "./routes/docs.tsx", |
14 |
| - "./routes/docs/index.mdx", |
15 |
| - "./routes/docs/get-started.mdx", |
16 |
| - "./routes/works.tsx", |
17 |
| - "./routes/works/$id.tsx", |
18 |
| - "./routes/p/$path+.tsx", |
19 |
| - ]; |
20 |
| - await Promise.all(files.map((file) => Deno.mkdir(join(tmpDir, dirname(file)), { recursive: true }))); |
21 |
| - await Promise.all(files.map((file) => Deno.writeTextFile(join(tmpDir, file), ""))); |
22 |
| - const routes = await initRoutes("./routes/**/*.{tsx,mdx}", tmpDir); |
23 |
| - assertEquals(routes.length, files.length); |
24 |
| - |
25 |
| - let matches = matchRoutes(new URL("/", "http://localhost:3000"), routes); |
26 |
| - assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/"]); |
27 |
| - assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/index.tsx"]); |
28 |
| - |
29 |
| - matches = matchRoutes(new URL("/about", "http://localhost:3000"), routes); |
30 |
| - assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/about"]); |
31 |
| - assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/about.tsx"]); |
32 |
| - |
33 |
| - matches = matchRoutes(new URL("/foo", "http://localhost:3000"), routes); |
34 |
| - assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/_404"]); |
35 |
| - assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/_404.tsx"]); |
36 |
| - |
37 |
| - matches = matchRoutes(new URL("/docs", "http://localhost:3000"), routes); |
38 |
| - assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/docs", "/docs/index"]); |
39 |
| - assertEquals(matches.map(([_, meta]) => meta.filename), [ |
40 |
| - "./routes/_app.tsx", |
41 |
| - "./routes/docs.tsx", |
42 |
| - "./routes/docs/index.mdx", |
43 |
| - ]); |
44 |
| - |
45 |
| - matches = matchRoutes(new URL("/docs/get-started", "http://localhost:3000"), routes); |
46 |
| - assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/docs", "/docs/get-started"]); |
47 |
| - assertEquals(matches.map(([_, meta]) => meta.filename), [ |
48 |
| - "./routes/_app.tsx", |
49 |
| - "./routes/docs.tsx", |
50 |
| - "./routes/docs/get-started.mdx", |
51 |
| - ]); |
52 |
| - |
53 |
| - matches = matchRoutes(new URL("/works", "http://localhost:3000"), routes); |
54 |
| - assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/_404"]); |
55 |
| - assertEquals(matches.map(([_, meta]) => meta.filename), [ |
56 |
| - "./routes/_app.tsx", |
57 |
| - "./routes/works.tsx", |
58 |
| - "./routes/_404.tsx", |
59 |
| - ]); |
60 |
| - |
61 |
| - matches = matchRoutes(new URL("/works/123", "http://localhost:3000"), routes); |
62 |
| - assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/123"]); |
63 |
| - assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, { id: "123" }]); |
64 |
| - assertEquals(matches.map(([_, meta]) => meta.filename), [ |
65 |
| - "./routes/_app.tsx", |
66 |
| - "./routes/works.tsx", |
67 |
| - "./routes/works/$id.tsx", |
68 |
| - ]); |
69 |
| - |
70 |
| - matches = matchRoutes(new URL("/p/foo/bar/123", "http://localhost:3000"), routes); |
71 |
| - assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/p/foo/bar/123"]); |
72 |
| - assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, { path: "foo/bar/123" }]); |
73 |
| - assertEquals(matches.map(([_, meta]) => meta.filename), [ |
74 |
| - "./routes/_app.tsx", |
75 |
| - "./routes/p/$path+.tsx", |
76 |
| - ]); |
77 |
| -}); |
| 2 | +import { restoreUrl, toLocalPath } from "../lib/helpers.ts"; |
78 | 3 |
|
79 | 4 | Deno.test("lib/helpers.ts: toLocalPath", () => {
|
80 | 5 | assertEquals(toLocalPath("https://foo.com/[email protected]?action"), "/-/foo.com/[email protected]?action");
|
|
0 commit comments