testRoutesAndLinks

tcmts.react.test.testRoutesAndLinks · #ik6xb2ec · term

Routes match paths, their parameters decoded; a link's address goes under the app's base.

import { href, match } from "#codebase/tcmts/react";
import { withPage } from "#codebase/tcmts/react/test";
import assert from "node:assert/strict";

export const testRoutesAndLinks = () => {
  assert.deepEqual(match("/s/:owner/:key", "/s/alice/k%201"), { owner: "alice", key: "k 1" });
  assert.deepEqual(match("/docs/*", "/docs/a/b"), { "*": "a/b" });
  assert.equal(match("/s/:owner", "/s/alice/more"), undefined);
  assert.equal(match("/s/:owner", "/s/%E0%A4%A"), undefined, "a malformed escape");
  assert.equal(href("/s/1"), "/s/1");
  withPage({ __TCM_BASE__: "/examples/notes" }, () => {
    assert.equal(href("/s/1"), "/examples/notes/s/1");
    assert.equal(href("/examples/notes/s/1"), "/examples/notes/s/1", "under it already");
    assert.equal(href("https://other.example/"), "https://other.example/");
  });
}

uses