fix(i18n): refresh omitted dialog labels in the active language
This commit is contained in:
@@ -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 <button onClick={() => void confirm({ message: 'Fortsett?' })}>Open localized confirm</button>;
|
||||
}
|
||||
|
||||
function LocaleDemo({ result, copy }: { result: (value: boolean) => void; copy?: { title: string; confirmLabel: string; cancelLabel: string } }) {
|
||||
const { confirm } = useConfirm();
|
||||
return <button onClick={() => void confirm({ message: 'Literal spørsmål 東京?', ...copy }).then(result)}>Open locale confirm</button>;
|
||||
}
|
||||
|
||||
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(<I18nProvider><ConfirmProvider><LocaleDemo result={result} /></ConfirmProvider></I18nProvider>);
|
||||
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(<I18nProvider><ConfirmProvider><LocaleDemo result={result} copy={copy} /></ConfirmProvider></I18nProvider>);
|
||||
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(
|
||||
<ConfirmProvider>
|
||||
|
||||
@@ -29,9 +29,6 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
||||
const [state, setState] = useState<ConfirmState>({
|
||||
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();
|
||||
} },
|
||||
}}>
|
||||
<DialogTitle id={titleId} sx={{ pb: 1, fontWeight: 800 }}>{state.title}</DialogTitle>
|
||||
<DialogTitle id={titleId} sx={{ pb: 1, fontWeight: 800 }}>{state.title ?? i18n?.t("confirmAction") ?? "Confirm action"}</DialogTitle>
|
||||
<DialogContent role="region" aria-labelledby={titleId} tabIndex={0}>
|
||||
<Alert severity={state.destructive ? "warning" : "info"} variant="outlined" sx={{ mb: 2 }}>
|
||||
{state.destructive
|
||||
@@ -79,9 +76,9 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
||||
<Typography id={messageId} sx={{ color: "text.secondary" }}>{state.message}</Typography>
|
||||
</DialogContent>
|
||||
<DialogActions sx={{ px: 3, pb: 2.5, gap: 1 }}>
|
||||
<Button ref={cancelRef} autoFocus onClick={() => closeWith(false)}>{state.cancelLabel}</Button>
|
||||
<Button ref={cancelRef} autoFocus onClick={() => closeWith(false)}>{state.cancelLabel ?? i18n?.t("cancel") ?? "Cancel"}</Button>
|
||||
<Button color={state.destructive ? "error" : "primary"} variant="contained" onClick={() => closeWith(true)}>
|
||||
{state.confirmLabel}
|
||||
{state.confirmLabel ?? i18n?.t("confirm") ?? "Confirm"}
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
|
||||
@@ -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 <button onClick={() => void prompt({ message: 'Literal spørsmål 東京?', defaultValue: 'Original draft', ...copy }).then(result)}>Open locale prompt</button>;
|
||||
}
|
||||
|
||||
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(<I18nProvider><PromptProvider><LocaleDemo result={result} /></PromptProvider></I18nProvider>);
|
||||
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(<I18nProvider><PromptProvider><LocaleDemo result={result} copy={copy} /></PromptProvider></I18nProvider>);
|
||||
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();
|
||||
|
||||
@@ -27,11 +27,8 @@ export function PromptProvider({ children }: { children: React.ReactNode }) {
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [state, setState] = useState<PromptState>({
|
||||
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}
|
||||
<Dialog open={state.open} onClose={() => closeWith(null)} aria-describedby={messageId} fullWidth maxWidth="xs"
|
||||
slotProps={{ paper: { sx: { borderRadius: 3, border: 1, borderColor: "divider", backgroundImage: "none" } } }}>
|
||||
<DialogTitle sx={{ pb: 1, fontWeight: 800 }}>{state.title}</DialogTitle>
|
||||
<DialogTitle sx={{ pb: 1, fontWeight: 800 }}>{state.title ?? i18n?.t("enterValue") ?? "Enter value"}</DialogTitle>
|
||||
<DialogContent>
|
||||
<Typography id={messageId} sx={{ color: "text.secondary", mb: 1.5 }}>{state.message}</Typography>
|
||||
<TextField inputRef={inputRef} autoFocus fullWidth value={value} onChange={(e) => setValue(e.target.value)} inputProps={{ maxLength: 180, "aria-labelledby": messageId }} helperText={`${value.length}/180`} />
|
||||
</DialogContent>
|
||||
<DialogActions sx={{ px: 3, pb: 2.5, gap: 1 }}>
|
||||
<Button onClick={() => closeWith(null)}>{state.cancelLabel}</Button>
|
||||
<Button variant="contained" onClick={() => closeWith(value)}>{state.confirmLabel}</Button>
|
||||
<Button onClick={() => closeWith(null)}>{state.cancelLabel ?? i18n?.t("cancel") ?? "Cancel"}</Button>
|
||||
<Button variant="contained" onClick={() => closeWith(value)}>{state.confirmLabel ?? i18n?.t("save") ?? "Save"}</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
</PromptContext.Provider>
|
||||
|
||||
Reference in New Issue
Block a user