From 121627ff3181bd0b988aa989eaa95f56815020ff Mon Sep 17 00:00:00 2001 From: cesnimda Date: Mon, 16 Nov 2026 23:40:15 +0100 Subject: [PATCH] fix(i18n): refresh omitted dialog labels in the active language --- job-tracker-ui/src/confirm.test.tsx | 59 +++++++++++++++++++++++++- job-tracker-ui/src/confirm.tsx | 17 ++++---- job-tracker-ui/src/prompt.test.tsx | 65 ++++++++++++++++++++++++++++- job-tracker-ui/src/prompt.tsx | 17 ++++---- 4 files changed, 136 insertions(+), 22 deletions(-) diff --git a/job-tracker-ui/src/confirm.test.tsx b/job-tracker-ui/src/confirm.test.tsx index b3a4fd8f..d858742a 100644 --- a/job-tracker-ui/src/confirm.test.tsx +++ b/job-tracker-ui/src/confirm.test.tsx @@ -1,8 +1,11 @@ import React from 'react'; import '@testing-library/jest-dom'; -import { render, screen, fireEvent, act } from '@testing-library/react'; +import { render, screen, fireEvent, act, waitFor, within } from '@testing-library/react'; import { ConfirmProvider, useConfirm } from './confirm'; import { I18nProvider } from './i18n/I18nProvider'; +import { translations } from './i18n/translations'; + +afterEach(() => window.localStorage.removeItem('uiLanguage')); function Demo() { const { confirm } = useConfirm(); @@ -18,6 +21,60 @@ function DefaultDemo() { return ; } +function LocaleDemo({ result, copy }: { result: (value: boolean) => void; copy?: { title: string; confirmLabel: string; cancelLabel: string } }) { + const { confirm } = useConfirm(); + return ; +} + +test.each([ + ['en', 'nb', true], ['nb', 'en', false], +] as const)('open confirm defaults follow %s to %s without replacing focus or resolver', async (from, to, accepted) => { + localStorage.setItem('uiLanguage', from); + const result = jest.fn(); + render(); + const trigger = screen.getByRole('button', { name: 'Open locale confirm' }); + act(() => trigger.focus()); + fireEvent.click(trigger); + const dialog = await screen.findByRole('dialog', { name: translations[from].confirmAction }); + expect(screen.getByRole('button', { name: translations[from].cancel })).toHaveFocus(); + const message = screen.getByRole('region', { name: translations[from].confirmAction }); + act(() => message.focus()); + await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to }))); + expect(screen.getByRole('dialog', { name: translations[to].confirmAction })).toBe(dialog); + expect(screen.getByRole('button', { name: translations[to].confirm })).toBeVisible(); + expect(screen.getByRole('button', { name: translations[to].cancel })).toBeVisible(); + expect(dialog).toHaveAccessibleDescription('Literal spørsmål 東京?'); + expect(message).toHaveFocus(); + expect(result).not.toHaveBeenCalled(); + const action = screen.getByRole('button', { name: translations[to][accepted ? 'confirm' : 'cancel'] }); + fireEvent.click(action); fireEvent.click(action); + await waitFor(() => expect(result).toHaveBeenCalledTimes(1)); + expect(result).toHaveBeenCalledWith(accepted); + await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); + expect(trigger).toHaveFocus(); +}); + +test.each([ + { title: 'Literal title', confirmLabel: 'Literal accept', cancelLabel: 'Literal cancel' }, + { title: '', confirmLabel: '', cancelLabel: '' }, +])('locale changes preserve explicit confirm copy %j verbatim', async copy => { + localStorage.setItem('uiLanguage', 'en'); + const result = jest.fn(); + render(); + fireEvent.click(screen.getByRole('button', { name: 'Open locale confirm' })); + const dialog = await screen.findByRole('dialog'); + await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: 'nb' }))); + expect(within(dialog).getByRole('heading').textContent).toBe(copy.title); + const buttons = within(dialog).getAllByRole('button'); + expect(buttons.map(button => button.textContent)).toEqual([copy.cancelLabel, copy.confirmLabel]); + expect(within(dialog).getByText(translations.nb.confirmStandardHint)).toBeVisible(); + expect(dialog).toHaveAccessibleDescription('Literal spørsmål 東京?'); + expect(result).not.toHaveBeenCalled(); + fireEvent.click(buttons[1]); + await waitFor(() => expect(result).toHaveBeenCalledTimes(1)); + expect(result).toHaveBeenCalledWith(true); +}); + test('renders app-owned confirmation dialog', async () => { render( diff --git a/job-tracker-ui/src/confirm.tsx b/job-tracker-ui/src/confirm.tsx index 2b48aecd..652fb4e1 100644 --- a/job-tracker-ui/src/confirm.tsx +++ b/job-tracker-ui/src/confirm.tsx @@ -29,9 +29,6 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) { const [state, setState] = useState({ open: false, message: "", - title: "Confirm action", - confirmLabel: "Confirm", - cancelLabel: "Cancel", destructive: false, }); @@ -47,14 +44,14 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) { resolverRef.current = resolve; setState({ open: true, - title: options.title ?? i18n?.t("confirmAction") ?? "Confirm action", + title: options.title, message: options.message, - confirmLabel: options.confirmLabel ?? i18n?.t("confirm") ?? "Confirm", - cancelLabel: options.cancelLabel ?? i18n?.t("cancel") ?? "Cancel", + confirmLabel: options.confirmLabel, + cancelLabel: options.cancelLabel, destructive: options.destructive ?? false, }); }); - }, [i18n]); + }, []); const value = useMemo(() => ({ confirm }), [confirm]); @@ -69,7 +66,7 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) { if (node.ownerDocument.activeElement === node) cancelRef.current?.focus(); } }, }}> - {state.title} + {state.title ?? i18n?.t("confirmAction") ?? "Confirm action"} {state.destructive @@ -79,9 +76,9 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) { {state.message} - + diff --git a/job-tracker-ui/src/prompt.test.tsx b/job-tracker-ui/src/prompt.test.tsx index c09cf478..88edcb5b 100644 --- a/job-tracker-ui/src/prompt.test.tsx +++ b/job-tracker-ui/src/prompt.test.tsx @@ -1,7 +1,70 @@ import React from 'react'; import '@testing-library/jest-dom'; -import { render, screen, fireEvent } from '@testing-library/react'; +import { render, screen, fireEvent, act, waitFor, within } from '@testing-library/react'; import { PromptProvider, usePrompt } from './prompt'; +import { I18nProvider } from './i18n/I18nProvider'; +import { translations } from './i18n/translations'; + +afterEach(() => window.localStorage.removeItem('uiLanguage')); + +function LocaleDemo({ result, copy }: { result: (value: string | null) => void; copy?: { title: string; confirmLabel: string; cancelLabel: string } }) { + const { prompt } = usePrompt(); + return ; +} + +test.each([ + ['en', 'nb', true], ['nb', 'en', false], +] as const)('open prompt defaults follow %s to %s without replacing its Unicode draft, focus or resolver', async (from, to, accepted) => { + localStorage.setItem('uiLanguage', from); + const result = jest.fn(); + render(); + const trigger = screen.getByRole('button', { name: 'Open locale prompt' }); + act(() => trigger.focus()); + fireEvent.click(trigger); + const dialog = await screen.findByRole('dialog', { name: translations[from].enterValue }); + const input = screen.getByRole('textbox', { name: 'Literal spørsmål 東京?' }) as HTMLInputElement; + await waitFor(() => expect(input).toHaveFocus()); + const draft = 'Søknad 東京 🧑‍💻'; + fireEvent.change(input, { target: { value: draft } }); + input.setSelectionRange(2, 7); + await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to }))); + expect(screen.getByRole('dialog', { name: translations[to].enterValue })).toBe(dialog); + expect(screen.getByRole('button', { name: translations[to].save })).toBeVisible(); + expect(screen.getByRole('button', { name: translations[to].cancel })).toBeVisible(); + expect(input).toHaveValue(draft); expect(input).toHaveFocus(); + expect([input.selectionStart, input.selectionEnd]).toEqual([2, 7]); + expect(dialog).toHaveAccessibleDescription('Literal spørsmål 東京?'); + expect(result).not.toHaveBeenCalled(); + const action = screen.getByRole('button', { name: translations[to][accepted ? 'save' : 'cancel'] }); + fireEvent.click(action); fireEvent.click(action); + await waitFor(() => expect(result).toHaveBeenCalledTimes(1)); + expect(result).toHaveBeenCalledWith(accepted ? draft : null); + await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); + expect(trigger).toHaveFocus(); +}); + +test.each([ + { title: 'Literal title', confirmLabel: 'Literal save', cancelLabel: 'Literal cancel' }, + { title: '', confirmLabel: '', cancelLabel: '' }, +])('locale changes preserve explicit prompt copy %j verbatim', async copy => { + localStorage.setItem('uiLanguage', 'en'); + const result = jest.fn(); + render(); + fireEvent.click(screen.getByRole('button', { name: 'Open locale prompt' })); + const dialog = await screen.findByRole('dialog'); + const input = screen.getByRole('textbox', { name: 'Literal spørsmål 東京?' }); + fireEvent.change(input, { target: { value: 'Typed æøå 東京' } }); + await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: 'nb' }))); + expect(document.documentElement.lang).toBe('nb-NO'); + expect(within(dialog).getByRole('heading').textContent).toBe(copy.title); + const buttons = within(dialog).getAllByRole('button'); + expect(buttons.map(button => button.textContent)).toEqual([copy.cancelLabel, copy.confirmLabel]); + expect(input).toHaveValue('Typed æøå 東京'); + expect(result).not.toHaveBeenCalled(); + fireEvent.click(buttons[1]); + await waitFor(() => expect(result).toHaveBeenCalledTimes(1)); + expect(result).toHaveBeenCalledWith('Typed æøå 東京'); +}); function Demo() { const { prompt } = usePrompt(); diff --git a/job-tracker-ui/src/prompt.tsx b/job-tracker-ui/src/prompt.tsx index 7b43d544..389b9a8a 100644 --- a/job-tracker-ui/src/prompt.tsx +++ b/job-tracker-ui/src/prompt.tsx @@ -27,11 +27,8 @@ export function PromptProvider({ children }: { children: React.ReactNode }) { const inputRef = useRef(null); const [state, setState] = useState({ open: false, - title: "Enter value", message: "", defaultValue: "", - confirmLabel: "Save", - cancelLabel: "Cancel", }); const [value, setValue] = useState(""); @@ -53,15 +50,15 @@ export function PromptProvider({ children }: { children: React.ReactNode }) { setValue(options.defaultValue ?? ""); setState({ open: true, - title: options.title ?? i18n?.t("enterValue") ?? "Enter value", + title: options.title, message: options.message, defaultValue: options.defaultValue ?? "", - confirmLabel: options.confirmLabel ?? i18n?.t("save") ?? "Save", - cancelLabel: options.cancelLabel ?? i18n?.t("cancel") ?? "Cancel", + confirmLabel: options.confirmLabel, + cancelLabel: options.cancelLabel, resolver: resolve, }); }); - }, [i18n]); + }, []); const contextValue = useMemo(() => ({ prompt }), [prompt]); @@ -70,14 +67,14 @@ export function PromptProvider({ children }: { children: React.ReactNode }) { {children} closeWith(null)} aria-describedby={messageId} fullWidth maxWidth="xs" slotProps={{ paper: { sx: { borderRadius: 3, border: 1, borderColor: "divider", backgroundImage: "none" } } }}> - {state.title} + {state.title ?? i18n?.t("enterValue") ?? "Enter value"} {state.message} setValue(e.target.value)} inputProps={{ maxLength: 180, "aria-labelledby": messageId }} helperText={`${value.length}/180`} /> - - + +