import { describe, expect, test } from "bun:test"; import { parseHash, TAB_LABELS, TABS } from "./routes"; describe("parseHash", () => { test.each(["#home"])("reads %s", (hash) => { expect(parseHash(hash).tab).toBe(hash.slice(1) as never); }); // The hash a browser produces varies: some paths write `#/gallery`, and copied links arrive // capitalised. All of them mean the same tab. test.each(["#/home", "#Home", "home"])("normalises %s", (hash) => { expect(parseHash(hash).tab).toBe("home"); }); // A refresh on the bare URL, and any hash left over from elsewhere, land somewhere valid rather // than rendering nothing. test.each(["", "#", "#nope", "#/../etc"])("falls back to home for %p", (hash) => { expect(parseHash(hash).tab).toBe("home"); }); }); describe("nested routes", () => { test("a collection id rides along in param", () => { const route = parseHash("#home/0004853c-e097-4af9-b40b-0666594703cf"); expect(route.tab).toBe("home"); expect(route.param).toBe("0004853c-e097-4af9-b40b-0666594703cf"); }); test("a bare tab has no param", () => expect(parseHash("#home").param).toBeUndefined()); // A trailing slash is what you get from hand-editing the address bar. test("a trailing slash is not a param", () => expect(parseHash("#home/").param).toBeUndefined()); test("an unknown tab with a param still falls back", () => { expect(parseHash("#nope/123").tab).toBe("home"); }); }); describe("tabs", () => { test("every tab has a label", () => { for (const tab of TABS) expect(TAB_LABELS[tab]).toBeTruthy(); }); test("the hash written for a tab parses back to it", () => { for (const tab of TABS) expect(parseHash(`#${tab}`).tab).toBe(tab); }); });