Make pathToRoute tests more readable

We switch the test for `pathToRoute` from a paramterized test to regular
tests. I had a lot of trouble finding the right properties to change
when a test in this group failed. The line number was not helpful at all
and the test description was burried inside an object.

We also rename `pathToRoute` to `urlToRoute` because the function takes
a `URL` as an argument.

Signed-off-by: Thomas Scholtes <geigerzaehler@axiom.fm>
This commit is contained in:
Thomas Scholtes
2023-07-17 11:44:27 +02:00
parent 283b665b31
commit 50088a5c8a
2 changed files with 72 additions and 82 deletions
+3 -3
View File
@@ -69,7 +69,7 @@ export async function navigateToUrl(
const relativeUrl = pathname + url.search + (hash || "");
url = new URL(relativeUrl, window.origin);
const route = pathToRoute(url);
const route = urlToRoute(url);
if (route) {
await replace(route);
@@ -176,7 +176,7 @@ function extractBaseUrl(hostAndPort: string): BaseUrl {
}
}
function pathToRoute(url: URL): Route | null {
function urlToRoute(url: URL): Route | null {
const segments = url.pathname.substring(1).split("/");
const resource = segments.shift();
@@ -249,4 +249,4 @@ export function routeToPath(route: Route): string {
}
}
export const testExports = { pathToRoute, routeToPath };
export const testExports = { urlToRoute, routeToPath };
+69 -79
View File
@@ -57,7 +57,7 @@ describe("route invariant when parsed", () => {
const path = testExports.routeToPath(route);
route.revision = undefined;
route.route = "REVISION";
expect(testExports.pathToRoute(new URL(path, origin))).toEqual(route);
expect(testExports.urlToRoute(new URL(path, origin))).toEqual(route);
});
test("projects.tree with peer and revision and path", () => {
@@ -74,7 +74,7 @@ describe("route invariant when parsed", () => {
route.revision = undefined;
route.path = undefined;
route.route = "REVISION/PATH";
expect(testExports.pathToRoute(new URL(path, origin))).toEqual(route);
expect(testExports.urlToRoute(new URL(path, origin))).toEqual(route);
});
test("projects.history", () => {
@@ -201,86 +201,76 @@ describe("route invariant when parsed", () => {
function expectParsingInvariant(route: Route) {
const path = testExports.routeToPath(route);
expect(testExports.pathToRoute(new URL(path, origin))).toEqual(route);
expect(testExports.urlToRoute(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: "project.tree",
seed: {
hostname: "willow.radicle.garden",
scheme: "http",
port: 8080,
},
project: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
route: "",
},
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: "project.tree",
seed: {
hostname: "willow.radicle.garden",
scheme: "http",
port: 8080,
},
project: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
route: "",
},
description: "Seed Project Route w/o trailing slash",
},
])("$description", (route: any) => {
expect(testExports.pathToRoute(route.input)).toEqual(route.output);
test("non-existent", () => {
expectPathToRoute("/foo/baz/bar", null);
});
test("home", () => {
expectPathToRoute("", { resource: "home" });
expectPathToRoute("/", { resource: "home" });
});
test("seeds", () => {
expectPathToRoute("/seeds/willow.radicle.garden", {
resource: "seeds",
params: {
baseUrl: {
hostname: "willow.radicle.garden",
scheme: "http",
port: 8080,
},
projectPageIndex: 0,
},
});
});
test("project with trailing slash", () => {
expectPathToRoute(
"/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT/",
{
resource: "project.tree",
seed: {
hostname: "willow.radicle.garden",
scheme: "http",
port: 8080,
},
project: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
route: "",
},
);
});
test("project without trailing slash", () => {
expectPathToRoute(
"/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
{
resource: "project.tree",
seed: {
hostname: "willow.radicle.garden",
scheme: "http",
port: 8080,
},
project: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
route: "",
},
);
});
test("non-existent project route", () => {
expectPathToRoute(
"/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT/nope",
null,
);
});
function expectPathToRoute(relativeUrl: string, route: Route | null) {
expect(
testExports.urlToRoute(new URL(relativeUrl, "http://localhost/")),
).toEqual(route);
}
});