// @vitest-environment node import { describe, expect, it } from "vitest"; import type { MetaDescriptor, MetaFunction } from "react-router"; import { createHiveGridMetaFunction, formatHiveGridDocumentTitle } from "./HiveGridMeta"; describe("HiveGrid document title contract", () => { it.each([ ["Dashboard", "Dashboard | HiveGrid"], ["Dashboard | HiveGrid", "Dashboard | HiveGrid"], ["Dashboard | HiveGrid | HiveGrid", "Dashboard | HiveGrid"], ["Dashboard | Application Starter", "Dashboard | HiveGrid"], ["Aufgabe · Heizungswartung | Wissensbasis | HiveGrid", "Aufgabe · Heizungswartung | Wissensbasis | HiveGrid"], ["Küche | Bad", "Küche | Bad | HiveGrid"], ["HiveGrid", "HiveGrid"], [" ", "HiveGrid"], [undefined, "HiveGrid"], ])("normalizes %j to %j", (input, expected) => { expect(formatHiveGridDocumentTitle(input)).toBe(expected); }); it("normalizes an existing route meta title and keeps its other descriptors", () => { const meta = createHiveGridMetaFunction(() => [ { title: "Passwort vergessen | Anwendungsstarter" }, { name: "description", content: "Passwort zurücksetzen" }, ]); expect(meta(metaArgs({ title: "ignored" }))).toEqual([ { title: "Passwort vergessen | HiveGrid" }, { name: "description", content: "Passwort zurücksetzen" }, ]); }); it("uses a loader title when a leaf route has no meta export", () => { const meta = createHiveGridMetaFunction(); expect(meta(metaArgs({ title: "Benachrichtigungen" }, [{ title: "Dashboard | HiveGrid" }, { name: "robots", content: "noindex" }]))).toEqual([ { title: "Benachrichtigungen | HiveGrid" }, { name: "robots", content: "noindex" }, ]); }); it("replaces the configured app name without truncating pipes inside the page title", () => { const meta = createHiveGridMetaFunction(); const rootData = { metatags: [{ title: "Lokale App" }] }; expect(meta(metaArgs({ title: "Küche | Bad | Lokale App" }, [], rootData))).toEqual([{ title: "Küche | Bad | HiveGrid" }]); expect(meta(metaArgs({ title: "Küche | Bad" }, [], rootData))).toEqual([{ title: "Küche | Bad | HiveGrid" }]); }); it("supports the shell's nested meta.title fallback", () => { const meta = createHiveGridMetaFunction(); expect(meta(metaArgs({ meta: { title: "Profil" } }))).toEqual([{ title: "Profil | HiveGrid" }]); }); it("uses loader metatags for routes without a meta export", () => { const meta = createHiveGridMetaFunction(); expect( meta( metaArgs({ metatags: [{ title: "Kundenportal" }, { tagName: "link", rel: "canonical", href: "https://portal.hivegrid.de" }], }) ) ).toEqual([{ title: "Kundenportal | HiveGrid" }, { tagName: "link", rel: "canonical", href: "https://portal.hivegrid.de" }]); }); it("inherits the parent metadata and de-duplicates title descriptors", () => { const meta = createHiveGridMetaFunction(); expect(meta(metaArgs(undefined, [{ title: "Projekte | HiveGrid" }, { title: "stale" }, { property: "og:type", content: "website" }]))).toEqual([ { title: "stale | HiveGrid" }, { property: "og:type", content: "website" }, ]); }); it("falls back to the loader or parent title when an explicit meta function returns no title", () => { const meta = createHiveGridMetaFunction(() => [{ name: "description", content: "Details" }]); expect(meta(metaArgs({ title: "Aufgabendetail" }, [{ title: "Aufgaben | HiveGrid" }]))).toEqual([ { title: "Aufgabendetail | HiveGrid" }, { name: "description", content: "Details" }, ]); }); }); function metaArgs(data: unknown, parentMeta: MetaDescriptor[] = [], rootData?: unknown): Parameters[0] { return { data, loaderData: data, params: {}, location: { pathname: "/acme/example", search: "", hash: "", state: null, key: "test", }, matches: [ { id: "root", pathname: "/", data: rootData, loaderData: rootData, params: {}, meta: parentMeta, }, { id: "routes/example", pathname: "/acme/example", data, loaderData: data, params: {}, meta: [], }, ], } as Parameters[0]; }