Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
160 lines
6.8 KiB
TypeScript
160 lines
6.8 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import { useLocales } from "@/app/lib/i18n/LocalesProvider";
|
|
|
|
const cookieValue = vi.hoisted(() => ({ value: undefined as string | undefined }));
|
|
const acceptLanguage = vi.hoisted(() => ({ value: null as string | null }));
|
|
vi.mock("next/headers", () => ({
|
|
cookies: async () => ({
|
|
get: (name: string) =>
|
|
name === "bb_locale" && cookieValue.value ? { value: cookieValue.value } : undefined,
|
|
}),
|
|
headers: async () => ({ get: () => acceptLanguage.value }),
|
|
}));
|
|
|
|
const catalog = vi.hoisted(() => ({ fn: vi.fn() }));
|
|
vi.mock("@/app/lib/server/messages", () => ({ loadMessageCatalog: catalog.fn }));
|
|
|
|
const runtimeLocales = vi.hoisted(() => ({ fn: vi.fn() }));
|
|
vi.mock("@/app/lib/server/locales", () => ({ loadRuntimeLocales: runtimeLocales.fn }));
|
|
|
|
// Isolate RootLayout's own composition (cookie/header read -> runtime locale list ->
|
|
// catalog load -> LocalesProvider/MessagesProvider) from ThemeProvider/AdminAuthProvider/
|
|
// AppShell's own logic, which is exercised by their own test files.
|
|
vi.mock("@/app/lib/theme", () => ({
|
|
ThemeProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
|
}));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
AdminAuthProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
|
}));
|
|
vi.mock("@/app/components/app/AppShell", () => ({
|
|
default: ({ children }: { children: React.ReactNode }) => <div data-testid="shell">{children}</div>,
|
|
}));
|
|
|
|
// RootLayout resolves the locale through the real lib/server/i18n (globally mocked in vitest.setup).
|
|
vi.unmock("@/app/lib/server/i18n");
|
|
|
|
import RootLayout from "./layout";
|
|
|
|
// A five-locale runtime registry; these cases exercise resolution across zh_TW / ms_MY.
|
|
const FIVE_LOCALES = [
|
|
{ code: "en_US", label: "English" },
|
|
{ code: "zh_CN", label: "简体中文" },
|
|
{ code: "zh_TW", label: "繁體中文" },
|
|
{ code: "in_ID", label: "Bahasa Indonesia" },
|
|
{ code: "ms_MY", label: "Bahasa Melayu" },
|
|
];
|
|
|
|
function ChildProbe() {
|
|
const t = useT();
|
|
const locales = useLocales();
|
|
return <div>{t("Logout")}|{locales.map((l) => l.code).join(",")}</div>;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
cookieValue.value = undefined;
|
|
acceptLanguage.value = null;
|
|
catalog.fn.mockReset().mockResolvedValue({});
|
|
runtimeLocales.fn.mockReset().mockResolvedValue(FIVE_LOCALES);
|
|
});
|
|
|
|
describe("RootLayout i18n wiring", () => {
|
|
it("loads the catalog for the bb_locale cookie when present", async () => {
|
|
cookieValue.value = "zh_CN";
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
render(el);
|
|
expect(catalog.fn).toHaveBeenCalledWith("zh_CN");
|
|
});
|
|
|
|
it("falls back to Accept-Language resolution when no cookie is present", async () => {
|
|
acceptLanguage.value = "zh-TW";
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
render(el);
|
|
expect(catalog.fn).toHaveBeenCalledWith("zh_TW");
|
|
});
|
|
|
|
it("defaults to en_US when neither the cookie nor Accept-Language resolve", async () => {
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
render(el);
|
|
expect(catalog.fn).toHaveBeenCalledWith("en_US");
|
|
});
|
|
|
|
// React refuses to mount <html> as a child of the test container (it is
|
|
// only ever valid as the document root), so this asserts the prop on the
|
|
// returned element directly rather than rendering it into the DOM.
|
|
it("sets the html lang attribute from the resolved locale", async () => {
|
|
cookieValue.value = "zh_TW";
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
expect(el.props.lang).toBe("zh-TW");
|
|
});
|
|
|
|
it("puts only the Inter font variable class on html", async () => {
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
expect(el.props.className).toContain("--font-inter");
|
|
expect(el.props.className).not.toContain("--font-dm-sans");
|
|
});
|
|
|
|
// Spec: "The Admin Portal Displays Labels From The Message Catalog" -- the
|
|
// loaded catalog must actually reach components via MessagesProvider, not
|
|
// just be fetched and discarded.
|
|
it("provides the loaded catalog to descendants through MessagesProvider", async () => {
|
|
catalog.fn.mockResolvedValue({ Logout: "Sign Out" });
|
|
const el = await RootLayout({ children: <ChildProbe /> });
|
|
render(el);
|
|
expect(screen.getByText(/Sign Out/)).toBeInTheDocument();
|
|
});
|
|
|
|
// add-admin-locale-management: the runtime locale list is fetched server-side and provided
|
|
// through LocalesProvider so the switcher and the /messages per-locale inputs use it.
|
|
it("provides the runtime locale list to descendants through LocalesProvider", async () => {
|
|
runtimeLocales.fn.mockResolvedValue([
|
|
{ code: "en_US", label: "English" },
|
|
{ code: "th_TH", label: "ภาษาไทย" },
|
|
]);
|
|
const el = await RootLayout({ children: <ChildProbe /> });
|
|
render(el);
|
|
expect(screen.getByText(/en_US,th_TH/)).toBeInTheDocument();
|
|
});
|
|
|
|
it("resolves Accept-Language against the runtime codes only", async () => {
|
|
runtimeLocales.fn.mockResolvedValue([{ code: "en_US", label: "English" }]);
|
|
acceptLanguage.value = "ms-MY"; // ms_MY is not in the runtime list -> DEFAULT_LOCALE
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
render(el);
|
|
expect(catalog.fn).toHaveBeenCalledWith("en_US");
|
|
});
|
|
|
|
// Minor 6: the bb_locale cookie is only honoured while it names a locale still in the
|
|
// runtime set — a disabled/removed locale must not keep being requested.
|
|
it("uses the bb_locale cookie when it is in the runtime list", async () => {
|
|
cookieValue.value = "ms_MY";
|
|
acceptLanguage.value = "zh-TW";
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
render(el);
|
|
expect(catalog.fn).toHaveBeenCalledWith("ms_MY");
|
|
});
|
|
|
|
it("ignores a bb_locale cookie outside the runtime list and falls back to Accept-Language", async () => {
|
|
cookieValue.value = "th_TH"; // not among the five runtime locales
|
|
acceptLanguage.value = "zh-TW";
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
render(el);
|
|
expect(catalog.fn).toHaveBeenCalledWith("zh_TW");
|
|
});
|
|
|
|
// A DISABLED runtime locale is not a resolvable UI locale: a cookie naming it is
|
|
// ignored, and Accept-Language cannot steer onto it — only enabled codes count.
|
|
it("excludes a disabled runtime locale from resolution", async () => {
|
|
runtimeLocales.fn.mockResolvedValue([
|
|
{ code: "en_US", label: "English", enabled: true },
|
|
{ code: "zh_CN", label: "简体中文", enabled: false },
|
|
]);
|
|
cookieValue.value = "zh_CN"; // disabled -> not honoured
|
|
acceptLanguage.value = "zh-CN"; // would resolve to zh_CN if it were enabled
|
|
const el = await RootLayout({ children: <div>page</div> });
|
|
render(el);
|
|
expect(catalog.fn).toHaveBeenCalledWith("en_US");
|
|
});
|
|
});
|