Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Make pathToRoute tests more readable
Thomas Scholtes committed 2 years ago
commit 50088a5c8a2aeca45ed43af3d1e4607bef70be1a
parent 283b665b3147b695f1cfa619b780ca4a5e40f841
2 files changed +52 -62
modified src/lib/router.ts
@@ -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 };
modified tests/unit/router.test.ts
@@ -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,49 +201,38 @@ 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,
+
  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,
      },
-
      description: "Seed View Route",
-
    },
-
    {
-
      input: new URL(
-
        "/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT/",
-
        dummyUrl,
-
      ),
-
      output: {
+
    });
+
  });
+

+
  test("project with trailing slash", () => {
+
    expectPathToRoute(
+
      "/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT/",
+
      {
        resource: "project.tree",
        seed: {
          hostname: "willow.radicle.garden",
@@ -253,22 +242,13 @@ describe("pathToRoute", () => {
        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: {
+
    );
+
  });
+

+
  test("project without trailing slash", () => {
+
    expectPathToRoute(
+
      "/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
+
      {
        resource: "project.tree",
        seed: {
          hostname: "willow.radicle.garden",
@@ -278,9 +258,19 @@ describe("pathToRoute", () => {
        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 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);
+
  }
});