Very Popular
Latest
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152import { assertEquals } from "std/testing/asserts.ts";import { dirname, join } from "std/path/mod.ts";import { matchRoutes } from "../framework/core/router.ts";import { initRouter } from "../server/router.ts";
Deno.test("[unit] server/router.ts: matchRoutes", async () => { const tmpDir = await Deno.makeTempDir(); const files = [ "./routes/_404.tsx", "./routes/_app.tsx", "./routes/blog.tsx", "./routes/docs.tsx", "./routes/docs/get-started.mdx", "./routes/docs/index.mdx", "./routes/index.tsx", "./routes/utils.ts", "./routes/works.tsx", "./routes/works/$id.tsx", "./routes/works/$id/$page+.tsx", "./routes/works/$id/index.tsx", "./routes/works/$id/order.tsx", "./routes/works/index.tsx", "./routes/works/new.tsx", "./routes/users/index.tsx", "./routes/users/$uid.tsx", "./routes/users/$uid/index.tsx", "./routes/users/$uid/settings/$page.tsx", "./routes/post/[date]/[...slug].tsx", ]; await Promise.all(files.map((file) => Deno.mkdir(join(tmpDir, dirname(file)), { recursive: true }))); await Promise.all(files.map((file) => Deno.writeTextFile(join(tmpDir, file), ""))); const routes = await initRouter(tmpDir, { glob: "./routes/**/*.{tsx,mdx}" }); assertEquals(routes.routes.length, files.length - 1); assertEquals(routes.routes.filter(([_, meta]) => meta.nesting).length, 5);
let matches = matchRoutes(new URL("/", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/"]); assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/index.tsx"]);
matches = matchRoutes(new URL("/blog", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/blog"]); assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/blog.tsx"]);
matches = matchRoutes(new URL("/foo", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/_404"]); assertEquals(matches.map(([_, meta]) => meta.filename), ["./routes/_app.tsx", "./routes/_404.tsx"]);
matches = matchRoutes(new URL("/docs", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/docs", "/docs/index"]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/docs.tsx", "./routes/docs/index.mdx", ]);
matches = matchRoutes(new URL("/docs/get-started", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/docs", "/docs/get-started"]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/docs.tsx", "./routes/docs/get-started.mdx", ]);
matches = matchRoutes(new URL("/post/2022-04-18/better-call-saul/wine-and-roses", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), [ "/_app", "/post/2022-04-18/better-call-saul/wine-and-roses", ]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, { date: "2022-04-18", slug: "better-call-saul/wine-and-roses", }]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/post/[date]/[...slug].tsx", ]);
matches = matchRoutes(new URL("/works", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/index"]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, {}]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/works.tsx", "./routes/works/index.tsx", ]);
matches = matchRoutes(new URL("/works/new", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/new"]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, {}]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/works.tsx", "./routes/works/new.tsx", ]);
matches = matchRoutes(new URL("/works/14", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/14", "/works/14/index"]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, { id: "14" }, { id: "14" }]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/works.tsx", "./routes/works/$id.tsx", "./routes/works/$id/index.tsx", ]);
matches = matchRoutes(new URL("/works/14/order", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/14", "/works/14/order"]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, { id: "14" }, { id: "14" }]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/works.tsx", "./routes/works/$id.tsx", "./routes/works/$id/order.tsx", ]);
matches = matchRoutes(new URL("/works/14/admin/edit", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/works", "/works/14", "/works/14/admin/edit"]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}, { id: "14" }, { id: "14", page: "admin/edit" }]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/works.tsx", "./routes/works/$id.tsx", "./routes/works/$id/$page+.tsx", ]);
matches = matchRoutes(new URL("/users", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/index"]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, {}]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/users/index.tsx", ]);
matches = matchRoutes(new URL("/users/ije", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/ije", "/users/ije/index"]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, { uid: "ije" }, { uid: "ije" }]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/users/$uid.tsx", "./routes/users/$uid/index.tsx", ]);
matches = matchRoutes(new URL("/users/ije/settings/profile", "http://localhost:3000"), routes); assertEquals(matches.map(([ret]) => ret.pathname.input), ["/_app", "/users/ije", "/users/ije/settings/profile"]); assertEquals(matches.map(([ret]) => ret.pathname.groups), [{}, { uid: "ije" }, { uid: "ije", page: "profile" }]); assertEquals(matches.map(([_, meta]) => meta.filename), [ "./routes/_app.tsx", "./routes/users/$uid.tsx", "./routes/users/$uid/settings/$page.tsx", ]);});