mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-08 13:10:44 +08:00
We replace the tests for `routeToPath` with a test that calls `pathToRoute(routeToPath(route))`. These test do not only cover the assertions of the previous tests but also check an important invariant for route parsing and building. I’m not using `test.each` here because this makes the tests less readable. Signed-off-by: Thomas Scholtes <geigerzaehler@axiom.fm>
131 lines
3.3 KiB
TypeScript
131 lines
3.3 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import { testExports, type Route } from "@app/lib/router";
|
|
|
|
// Defining the window.origin value, since vitest doesn't provide one.
|
|
window.origin = "http://localhost:3000";
|
|
|
|
describe("route invariant when parsed", () => {
|
|
const baseUrl = {
|
|
hostname: "willow.radicle.garden",
|
|
port: 8000,
|
|
scheme: "http",
|
|
};
|
|
|
|
test("home", () => {
|
|
return expectParsingInvariant({ resource: "home" });
|
|
});
|
|
test("seeds", () => {
|
|
expectParsingInvariant({
|
|
resource: "seeds",
|
|
params: {
|
|
// TODO: This only works with the value 0. The value is not actually
|
|
// extract.
|
|
projectPageIndex: 0,
|
|
baseUrl,
|
|
},
|
|
});
|
|
});
|
|
test("projects.tree", () => {
|
|
return expectParsingInvariant({
|
|
resource: "projects",
|
|
params: {
|
|
view: { resource: "tree" },
|
|
baseUrl,
|
|
id: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
|
|
},
|
|
});
|
|
});
|
|
|
|
function expectParsingInvariant(route: Route) {
|
|
const origin = "http://localhost:3000";
|
|
const path = testExports.routeToPath(route);
|
|
expect(testExports.pathToRoute(new URL(path, origin))).toEqual(route);
|
|
}
|
|
});
|
|
|
|
describe("pathToRoute", () => {
|
|
const dummyUrl = "https://localhost";
|
|
test.each([
|
|
{
|
|
input: new URL("/foo/baz/bar", dummyUrl),
|
|
output: null,
|
|
description: "Non existant not found route",
|
|
},
|
|
{
|
|
input: new URL("", dummyUrl),
|
|
output: { resource: "home" },
|
|
description: "Home Route",
|
|
},
|
|
{
|
|
input: new URL("/", dummyUrl),
|
|
output: { resource: "home" },
|
|
description: "Home Route",
|
|
},
|
|
{
|
|
input: new URL("/seeds/willow.radicle.garden", dummyUrl),
|
|
output: {
|
|
resource: "seeds",
|
|
params: {
|
|
baseUrl: {
|
|
hostname: "willow.radicle.garden",
|
|
scheme: "http",
|
|
port: 8080,
|
|
},
|
|
projectPageIndex: 0,
|
|
},
|
|
},
|
|
description: "Seed View Route",
|
|
},
|
|
{
|
|
input: new URL(
|
|
"/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT/",
|
|
dummyUrl,
|
|
),
|
|
output: {
|
|
resource: "projects",
|
|
params: {
|
|
view: { resource: "tree" },
|
|
baseUrl: {
|
|
hostname: "willow.radicle.garden",
|
|
scheme: "http",
|
|
port: 8080,
|
|
},
|
|
peer: undefined,
|
|
id: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
|
|
},
|
|
},
|
|
description: "Seed Project Route w trailing slash",
|
|
},
|
|
{
|
|
input: new URL(
|
|
"/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT/nope",
|
|
dummyUrl,
|
|
),
|
|
output: null,
|
|
description: "Seed Project Route w undefined suffix",
|
|
},
|
|
{
|
|
input: new URL(
|
|
"/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
|
|
dummyUrl,
|
|
),
|
|
output: {
|
|
resource: "projects",
|
|
params: {
|
|
view: { resource: "tree" },
|
|
baseUrl: {
|
|
hostname: "willow.radicle.garden",
|
|
scheme: "http",
|
|
port: 8080,
|
|
},
|
|
peer: undefined,
|
|
id: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
|
|
},
|
|
},
|
|
description: "Seed Project Route w/o trailing slash",
|
|
},
|
|
])("$description", (route: any) => {
|
|
expect(testExports.pathToRoute(route.input)).toEqual(route.output);
|
|
});
|
|
});
|