Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 4de4599

Browse files
committed
Fix testing
1 parent a419eeb commit 4de4599

File tree

2 files changed

+78
-76
lines changed

2 files changed

+78
-76
lines changed

tests/lib_helper_test.ts

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,5 @@
11
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";
783

794
Deno.test("lib/helpers.ts: toLocalPath", () => {
805
assertEquals(toLocalPath("https://foo.com/[email protected]?action"), "/-/foo.com/[email protected]?action");

tests/lib_route_test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { assertEquals } from "std/testing/asserts.ts";
2+
import { dirname, join } from "std/path/mod.ts";
3+
import { matchRoutes } from "../lib/route.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+
});

0 commit comments

Comments
 (0)