|
| 1 | +import { render, waitFor } from "@testing-library/react"; |
| 2 | +import { describe, it, expect, beforeEach } from "vitest"; |
| 3 | +import { MemoryRouter } from "react-router"; |
| 4 | +import { |
| 5 | + AppShellConfigContext, |
| 6 | + type RootConfiguration, |
| 7 | +} from "@/contexts/appshell-context"; |
| 8 | +import { BreadcrumbOverrideProvider } from "@/contexts/breadcrumb-context"; |
| 9 | +import { useOverrideBreadcrumb } from "@/hooks/use-override-breadcrumb"; |
| 10 | +import { DEFAULT_LOCALE } from "@/lib/i18n"; |
| 11 | +import { DocumentTitle } from "./document-title"; |
| 12 | + |
| 13 | +const configurations: RootConfiguration = { |
| 14 | + modules: [], |
| 15 | + settingsResources: [], |
| 16 | + locale: DEFAULT_LOCALE, |
| 17 | + errorBoundary: null!, |
| 18 | +}; |
| 19 | + |
| 20 | +// Registers an override for the current route the same way a detail page would. |
| 21 | +const Override = ({ title }: { title: string }) => { |
| 22 | + useOverrideBreadcrumb(title); |
| 23 | + return null; |
| 24 | +}; |
| 25 | + |
| 26 | +const renderAt = (path: string, title: string | undefined, override?: string) => |
| 27 | + render( |
| 28 | + <MemoryRouter initialEntries={[path]}> |
| 29 | + <AppShellConfigContext.Provider value={{ title, configurations }}> |
| 30 | + <BreadcrumbOverrideProvider> |
| 31 | + {override ? <Override title={override} /> : null} |
| 32 | + <DocumentTitle /> |
| 33 | + </BreadcrumbOverrideProvider> |
| 34 | + </AppShellConfigContext.Provider> |
| 35 | + </MemoryRouter>, |
| 36 | + ); |
| 37 | + |
| 38 | +describe("DocumentTitle", () => { |
| 39 | + beforeEach(() => { |
| 40 | + document.title = "initial"; |
| 41 | + }); |
| 42 | + |
| 43 | + it("sets '<page> · <app>' from the leaf segment and app title", async () => { |
| 44 | + renderAt("/orders/123", "My App"); |
| 45 | + // No module mapping, so the leaf title falls back to the decoded segment. |
| 46 | + await waitFor(() => expect(document.title).toBe("123 · My App")); |
| 47 | + }); |
| 48 | + |
| 49 | + it("uses just the page title when no app title is provided", async () => { |
| 50 | + renderAt("/orders/123", undefined); |
| 51 | + await waitFor(() => expect(document.title).toBe("123")); |
| 52 | + }); |
| 53 | + |
| 54 | + it("applies a breadcrumb override to the tab title", async () => { |
| 55 | + renderAt("/orders/123", "My App", "Order #123"); |
| 56 | + await waitFor(() => expect(document.title).toBe("Order #123 · My App")); |
| 57 | + }); |
| 58 | + |
| 59 | + it("leaves the static title untouched when nothing resolves", async () => { |
| 60 | + renderAt("/", undefined); |
| 61 | + await waitFor(() => expect(document.title).toBe("initial")); |
| 62 | + }); |
| 63 | +}); |
0 commit comments