Files
co-lite/solvway-admin-portal/app/components/app/LocaleSwitcher.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

118 lines
4.7 KiB
TypeScript

import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { LocalesProvider } from "@/app/lib/i18n/LocalesProvider";
const nav = vi.hoisted(() => ({ refresh: vi.fn() }));
vi.mock("next/navigation", () => ({ useRouter: () => ({ refresh: nav.refresh }) }));
import LocaleSwitcher from "./LocaleSwitcher";
function clearCookies() {
// jsdom does not expose a bulk-clear API; expire every cookie the test set.
document.cookie.split(";").forEach((c) => {
const name = c.split("=")[0]?.trim();
if (name) document.cookie = `${name}=; path=/; max-age=0`;
});
}
beforeEach(() => {
nav.refresh.mockReset();
clearCookies();
});
describe("LocaleSwitcher", () => {
it("lists the two fallback locales (no provider)", async () => {
const user = userEvent.setup();
render(<LocaleSwitcher />);
await user.click(screen.getByRole("button", { name: /change language/i }));
expect(screen.getByText("English")).toBeInTheDocument();
expect(screen.getByText("简体中文")).toBeInTheDocument();
expect(screen.queryByText("繁體中文")).not.toBeInTheDocument();
});
// Spec: "Switching locale persists and re-renders" -- the bb_locale cookie
// must be set to the chosen locale, and the portal must re-render.
it("selecting a locale sets the bb_locale cookie and refreshes", async () => {
const user = userEvent.setup();
render(<LocaleSwitcher />);
await user.click(screen.getByRole("button", { name: /change language/i }));
await user.click(screen.getByText("简体中文"));
expect(document.cookie).toContain("bb_locale=zh_CN");
expect(nav.refresh).toHaveBeenCalledOnce();
});
it("does nothing when re-selecting the already-active locale", async () => {
const user = userEvent.setup();
// No MessagesProvider wraps this render, so useLocale() resolves to the
// built-in DEFAULT_LOCALE, en_US -- "English" is therefore already active.
render(<LocaleSwitcher />);
await user.click(screen.getByRole("button", { name: /change language/i }));
await user.click(screen.getByText("English"));
expect(nav.refresh).not.toHaveBeenCalled();
});
// add-admin-locale-management: the switcher renders the RUNTIME locale list provided by
// RootLayout's LocalesProvider, not a hardcoded array. The default (no provider) is the
// two-locale FALLBACK_LOCALES, covered by the first test above.
it("lists a registry-driven locale provided through LocalesProvider", async () => {
const user = userEvent.setup();
render(
<LocalesProvider
locales={[
{ code: "en_US", label: "English" },
{ code: "th_TH", label: "ภาษาไทย" },
]}
>
<LocaleSwitcher />
</LocalesProvider>,
);
await user.click(screen.getByRole("button", { name: /change language/i }));
expect(screen.getByText("ภาษาไทย")).toBeInTheDocument();
expect(screen.queryByText("简体中文")).not.toBeInTheDocument();
});
// The switcher must offer only ENABLED locales -- a disabled/staged locale
// (translations entered but not yet exposed) must never be selectable, even
// though the same LocalesProvider list carries it for the /messages editor.
it("hides a disabled locale from the switcher", async () => {
const user = userEvent.setup();
render(
<LocalesProvider
locales={[
{ code: "en_US", label: "English", enabled: true },
{ code: "th_TH", label: "ภาษาไทย", enabled: false },
]}
>
<LocaleSwitcher />
</LocalesProvider>,
);
await user.click(screen.getByRole("button", { name: /change language/i }));
expect(screen.getByText("English")).toBeInTheDocument();
expect(screen.queryByText("ภาษาไทย")).not.toBeInTheDocument();
});
it("marks only the current locale with a check icon and aria-current", async () => {
const user = userEvent.setup();
render(<LocaleSwitcher />);
await user.click(screen.getByRole("button", { name: /change language/i }));
const current = screen.getByRole("menuitem", { name: "English" });
expect(current).toHaveAttribute("aria-current", "true");
expect(current.querySelector("svg.lucide-check")).not.toBeNull();
const other = screen.getByRole("menuitem", { name: "简体中文" });
expect(other).not.toHaveAttribute("aria-current");
expect(other.querySelector("svg.lucide-check")).toBeNull();
});
it("shows the current locale's short label on the button", () => {
render(<LocaleSwitcher />);
expect(screen.getByRole("button", { name: /change language/i })).toHaveTextContent("EN");
});
});