// @vitest-environment jsdom import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { useState } from "react"; import { describe, expect, it } from "vitest"; import { z } from "zod"; import { Button, Drawer, Pagination, Tabs, } from "../../src/presentation/design-system/index.ts"; import { LocaleProvider, useLocale, type SupportedLocale, } from "../../src/presentation/i18n/index.ts"; import { ErrorSummary, Form, FormField, useAppForm, } from "../../src/presentation/forms/index.ts"; import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx"; import { PreferenceProviders } from "../../src/presentation/providers/preference-providers.tsx"; import { AppRouter } from "../../src/presentation/routes/app-router.tsx"; import { createTestApplication } from "../helpers/create-test-application.ts"; function LocaleHarness() { const { direction, locale, message, setLocale } = useLocale(); const [drawerOpen, setDrawerOpen] = useState(false); return ( <> {`${locale}:${direction}`} setDrawerOpen(false)} open={drawerOpen} title={message("shell.menu")} > content ); } function LocaleSelect() { const { locale, message, setLocale } = useLocale(); return ( <> ); } function ValidatedForm() { const form = useAppForm({ schema: z.object({ name: z.string().min(2) }).strict(), defaultValues: { name: "" }, allowedServerFields: ["name"], mapToCommand: (values) => values, submit: async () => ({ ok: true as const, value: null }), }); return (
void form.submitForm(event)}> ); } describe("locale provider runtime", () => { // 08-10. A shown error is kept as a message key, not a translated string. it("re-translates displayed form errors when the locale changes", async () => { const user = userEvent.setup(); render( , ); await user.click(screen.getByRole("button", { name: "Submit" })); expect(screen.getByRole("alert")).toHaveTextContent( "입력 내용을 확인해 주세요.Name: 입력값이 너무 짧습니다.", ); await user.selectOptions(screen.getByLabelText("언어"), "en-US"); expect(screen.getByRole("alert")).toHaveTextContent( "Review the entered information.Name: This value is too short.", ); expect(screen.getByRole("textbox", { name: "Name" })).toHaveAccessibleDescription( "This value is too short.", ); }); it("switches copy and synchronizes the document language and direction", async () => { const user = userEvent.setup(); render( , ); expect(document.documentElement).toHaveAttribute("lang", "ko-KR"); await user.selectOptions(screen.getByLabelText("언어"), "en-US"); expect(screen.getByLabelText("Language")).toHaveValue("en-US"); expect(document.documentElement).toHaveAttribute("lang", "en-US"); expect(document.documentElement).toHaveAttribute("dir", "ltr"); await user.selectOptions(screen.getByLabelText("Language"), "ar-EG"); expect(document.documentElement).toHaveAttribute("lang", "ar-EG"); expect(document.documentElement).toHaveAttribute("dir", "rtl"); await user.click(screen.getByRole("button", { name: "Menu" })); expect(screen.getByRole("dialog", { name: "Menu" })).toHaveAttribute( "open", ); }); it("reverses horizontal tab focus semantics in RTL", async () => { const user = userEvent.setup(); render( , ); const first = screen.getByRole("tab", { name: "One" }); first.focus(); await user.keyboard("{ArrowRight}"); expect(screen.getByRole("tab", { name: "Three" })).toHaveFocus(); await user.keyboard("{ArrowLeft}"); expect(first).toHaveFocus(); }); it("keeps pagination semantics while direction-aware icons remain decorative", () => { render( {}} page={2} pageCount={3} pageLabel={(page) => `Page ${page}`} previousLabel="Previous page" /> , ); expect( screen.getByRole("button", { name: "Previous page" }), ).toBeEnabled(); expect(screen.getByRole("button", { name: "Next page" })).toBeEnabled(); expect(screen.queryByRole("img")).not.toBeInTheDocument(); }); }); describe("persisted locale preference", () => { it("stores the chosen locale and starts the next mount from it", async () => { const user = userEvent.setup(); const stored = new Map(); const application = createTestApplication({ preferences: { read: (name) => ({ ok: true as const, value: stored.get(name) }), write: (name, value) => { stored.set(name, value); return { ok: true as const }; }, remove: (name) => { stored.delete(name); return { ok: true as const }; }, }, }); const mount = () => render( , ); window.history.pushState({}, "", "/missing"); const first = mount(); await user.selectOptions(await screen.findByLabelText("언어"), "en-US"); expect(stored.get("LOCALE")).toBe("en-US"); first.unmount(); mount(); expect(await screen.findByLabelText("Language")).toHaveValue("en-US"); expect(document.documentElement).toHaveAttribute("lang", "en-US"); }); });