mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 10:10:30 +08:00
Test that route building and parsing leave routes invariant
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>
This commit is contained in:
+4
-2
@@ -12,6 +12,8 @@ import {
|
||||
} from "@app/views/projects/router";
|
||||
import { loadRoute } from "@app/lib/router/definitions";
|
||||
|
||||
export { type Route };
|
||||
|
||||
// Only used by Safari.
|
||||
const DOCUMENT_TITLE = "Radicle Interface";
|
||||
|
||||
@@ -227,7 +229,7 @@ function seedPath(baseUrl: BaseUrl) {
|
||||
}
|
||||
}
|
||||
|
||||
export function routeToPath(route: Route) {
|
||||
export function routeToPath(route: Route): string {
|
||||
if (route.resource === "home") {
|
||||
return "/";
|
||||
} else if (route.resource === "session") {
|
||||
@@ -302,7 +304,7 @@ export function routeToPath(route: Route) {
|
||||
} else if (route.resource === "notFound") {
|
||||
return route.params.url;
|
||||
} else {
|
||||
utils.unreachable(route);
|
||||
return utils.unreachable(route);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+37
-26
@@ -1,35 +1,46 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { testExports } from "@app/lib/router";
|
||||
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("routeToPath", () => {
|
||||
test.each([
|
||||
{ input: { resource: "home" }, output: "/", description: "Home Route" },
|
||||
{
|
||||
input: {
|
||||
resource: "seeds",
|
||||
params: { baseUrl: { hostname: "willow.radicle.garden" } },
|
||||
},
|
||||
output: "/seeds/willow.radicle.garden",
|
||||
description: "Seed View Route",
|
||||
},
|
||||
{
|
||||
input: {
|
||||
resource: "projects",
|
||||
params: {
|
||||
view: { resource: "tree" },
|
||||
baseUrl: { hostname: "willow.radicle.garden" },
|
||||
id: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
|
||||
},
|
||||
},
|
||||
output: "/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
|
||||
description: "Seed Project Route",
|
||||
},
|
||||
])("$description", (route: any) => {
|
||||
expect(testExports.routeToPath(route.input)).toEqual(route.output);
|
||||
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", () => {
|
||||
|
||||
Reference in New Issue
Block a user