first commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { test, expect } from "bun:test";
|
||||
import { compileRoutePattern, generateRoutesFile, matchRoute, type Route } from "../src/index.ts";
|
||||
|
||||
function route(raw: string): Route {
|
||||
return { raw, file: raw, ...compileRoutePattern(raw) };
|
||||
}
|
||||
|
||||
test("generateRoutesFile emits a Routes map with param types", () => {
|
||||
const code = generateRoutesFile([route("/"), route("/about"), route("/users/[id]")]);
|
||||
expect(code).toContain('"/": Record<string, never>;');
|
||||
expect(code).toContain('"/about": Record<string, never>;');
|
||||
expect(code).toContain('"/users/[id]": { "id": string };');
|
||||
expect(code).toContain("export function href");
|
||||
});
|
||||
|
||||
test("generated href() fills params and leaves static paths intact", async () => {
|
||||
// Compile + import the generated module to exercise href() for real.
|
||||
const code = generateRoutesFile([
|
||||
route("/"),
|
||||
route("/users/[id]"),
|
||||
route("/org/[org]/team/[team]"),
|
||||
]);
|
||||
const { tmpdir } = await import("node:os");
|
||||
const { writeFileSync, mkdirSync } = await import("node:fs");
|
||||
const { join } = await import("node:path");
|
||||
const { pathToFileURL } = await import("node:url");
|
||||
const dir = join(tmpdir(), "wire-routes-test");
|
||||
mkdirSync(dir, { recursive: true });
|
||||
const file = join(dir, `r${Date.now()}.ts`);
|
||||
writeFileSync(file, code);
|
||||
const mod = (await import(pathToFileURL(file).href)) as {
|
||||
href: (p: string, params?: Record<string, string>) => string;
|
||||
};
|
||||
expect(mod.href("/")).toBe("/");
|
||||
expect(mod.href("/users/[id]", { id: "42" })).toBe("/users/42");
|
||||
expect(mod.href("/org/[org]/team/[team]", { org: "acme", team: "core" })).toBe(
|
||||
"/org/acme/team/core",
|
||||
);
|
||||
expect(mod.href("/users/[id]", { id: "a b" })).toBe("/users/a%20b"); // encoded
|
||||
});
|
||||
|
||||
test("malformed encoded route params return no match instead of throwing", () => {
|
||||
expect(matchRoute([route("/users/[id]")], "/users/%E0%A4%A")).toBeNull();
|
||||
});
|
||||
Reference in New Issue
Block a user