import { describe, expect, test } from "vitest"; import type { MobileRegistryItem, PortalRegistryItem } from "@hivegrid/view-models"; import { AppShell, EmptyPage, createBreadcrumbRegistry, createMobileRegistry, createPortalRegistry, createShellRegistries, createSidebarRegistry, } from "./index.js"; describe("EmptyPage", () => { test("renders an empty-state section with the default i18n label key", () => { const element = EmptyPage({}); expect(element.type).toBe("section"); expect(element.props["data-page-state"]).toBe("empty"); expect(element.props["data-label-key"]).toBe("foundation.pages.empty"); }); test("passes a caller-supplied label key through to the empty-state markup", () => { const element = EmptyPage({ labelKey: "tasks.list.empty.headline" }); expect(element.props["data-label-key"]).toBe("tasks.list.empty.headline"); }); }); describe("createMobileRegistry", () => { test("items without an explicit priority default to 100 and sort last", () => { const result = createMobileRegistry([ { id: "no-priority", href: "/x", labelKey: "nav.x" }, { id: "high", href: "/h", labelKey: "nav.h", priority: 5 }, ]); expect(result.map((item) => item.id)).toEqual(["high", "no-priority"]); }); test("orders a prioritised item ahead of an unprioritised one regardless of input order", () => { const result = createMobileRegistry([ { id: "high", href: "/h", labelKey: "nav.h", priority: 5 }, { id: "no-priority", href: "/x", labelKey: "nav.x" }, ]); expect(result.map((item) => item.id)).toEqual(["high", "no-priority"]); }); test("keeps input order for items sharing the same priority (stable sort)", () => { const result = createMobileRegistry([ { id: "first", href: "/1", labelKey: "nav.1", priority: 10 }, { id: "second", href: "/2", labelKey: "nav.2", priority: 10 }, ]); expect(result.map((item) => item.id)).toEqual(["first", "second"]); }); test("returns a sorted copy without mutating the source array", () => { const source: MobileRegistryItem[] = [ { id: "b", href: "/b", labelKey: "nav.b", priority: 2 }, { id: "a", href: "/a", labelKey: "nav.a", priority: 1 }, ]; const result = createMobileRegistry(source); expect(result).not.toBe(source); expect(result.map((item) => item.id)).toEqual(["a", "b"]); // The source must stay in its original (unsorted) order. expect(source.map((item) => item.id)).toEqual(["b", "a"]); }); }); describe("createPortalRegistry", () => { test("items without an explicit order default to 100 and sort after ordered slots", () => { const result = createPortalRegistry([ { id: "tail", slot: "main", labelKey: "portal.tail" }, { id: "lead", slot: "main", labelKey: "portal.lead", order: 1 }, ]); expect(result.map((item) => item.id)).toEqual(["lead", "tail"]); }); test("orders an ordered slot ahead of an unordered one regardless of input order", () => { const result = createPortalRegistry([ { id: "lead", slot: "main", labelKey: "portal.lead", order: 1 }, { id: "tail", slot: "main", labelKey: "portal.tail" }, ]); expect(result.map((item) => item.id)).toEqual(["lead", "tail"]); }); test("returns a sorted copy without mutating the source array", () => { const source: PortalRegistryItem[] = [ { id: "late", slot: "main", labelKey: "portal.late", order: 9 }, { id: "early", slot: "main", labelKey: "portal.early", order: 1 }, ]; const result = createPortalRegistry(source); expect(result).not.toBe(source); expect(source.map((item) => item.id)).toEqual(["late", "early"]); }); }); describe("registry copy semantics", () => { test("createSidebarRegistry returns a defensive copy", () => { const source = [{ id: "home", href: "/", labelKey: "nav.home" }]; const copy = createSidebarRegistry(source); expect(copy).not.toBe(source); expect(copy).toEqual(source); }); test("createBreadcrumbRegistry returns a defensive copy", () => { const source = [{ id: "root", labelKey: "crumb.root" }]; const copy = createBreadcrumbRegistry(source); expect(copy).not.toBe(source); expect(copy).toEqual(source); }); }); describe("createShellRegistries", () => { test("keeps sidebar/breadcrumb order verbatim while sorting mobile and portals", () => { const registries = createShellRegistries({ sidebar: [ { id: "s2", href: "/s2", labelKey: "nav.s2" }, { id: "s1", href: "/s1", labelKey: "nav.s1" }, ], breadcrumbs: [ { id: "b1", labelKey: "crumb.b1" }, { id: "b2", labelKey: "crumb.b2" }, ], mobile: [ { id: "m-late", href: "/ml", labelKey: "nav.ml", priority: 9 }, { id: "m-early", href: "/me", labelKey: "nav.me", priority: 1 }, ], portals: [ { id: "p-late", slot: "main", labelKey: "portal.pl", order: 9 }, { id: "p-early", slot: "main", labelKey: "portal.pe", order: 1 }, ], }); expect(registries.sidebar.map((item) => item.id)).toEqual(["s2", "s1"]); expect(registries.breadcrumbs.map((item) => item.id)).toEqual(["b1", "b2"]); expect(registries.mobile.map((item) => item.id)).toEqual(["m-early", "m-late"]); expect(registries.portals.map((item) => item.id)).toEqual(["p-early", "p-late"]); }); }); describe("AppShell", () => { test("exposes each registry size as a data attribute for the layout shell", () => { const shell = AppShell({ registries: { sidebar: [{ id: "s", href: "/s", labelKey: "nav.s" }], breadcrumbs: [{ id: "b", labelKey: "crumb.b" }], mobile: [ { id: "m1", href: "/m1", labelKey: "nav.m1", priority: 1 }, { id: "m2", href: "/m2", labelKey: "nav.m2", priority: 2 }, ], portals: [], }, children: "page", }); expect(shell.props["data-ui-shell"]).toBe("app"); expect(shell.props["data-sidebar-count"]).toBe(1); expect(shell.props["data-breadcrumb-count"]).toBe(1); expect(shell.props["data-mobile-count"]).toBe(2); expect(shell.props["data-portal-count"]).toBe(0); expect((shell.props as { children?: unknown }).children).toBe("page"); }); test("defaults every registry to empty when no registries prop is supplied", () => { const shell = AppShell({}); expect(shell.props["data-sidebar-count"]).toBe(0); expect(shell.props["data-breadcrumb-count"]).toBe(0); expect(shell.props["data-mobile-count"]).toBe(0); expect(shell.props["data-portal-count"]).toBe(0); }); });