fix(i18n): preserve attachment dialogs across language changes
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import React from 'react';
|
||||
import '@testing-library/jest-dom';
|
||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { act, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
|
||||
import { ConfirmProvider } from './confirm';
|
||||
import { PromptProvider } from './prompt';
|
||||
import Attachments from './components/Attachments';
|
||||
import { ToastProvider } from './toast';
|
||||
import { I18nProvider } from './i18n/I18nProvider';
|
||||
import { api } from './api';
|
||||
import { translations } from './i18n/translations';
|
||||
|
||||
jest.mock('./api', () => ({
|
||||
getApiErrorMessage: (_error: unknown, fallback: string) => fallback,
|
||||
@@ -22,6 +23,70 @@ const mockedApi = api as jest.Mocked<typeof api>;
|
||||
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
|
||||
test.each([['en', 'nb'], ['nb', 'en']] as const)('pending attachment read uses %s to %s failure copy without another read', async (from, to) => {
|
||||
const previous = localStorage.getItem('uiLanguage');
|
||||
let reject!: (error: unknown) => void;
|
||||
mockedApi.get.mockReturnValueOnce(new Promise((_resolve, no) => { reject = no; }));
|
||||
localStorage.setItem('uiLanguage', from);
|
||||
const view = render(<ToastProvider><I18nProvider><ConfirmProvider><PromptProvider><Attachments jobId={42} /></PromptProvider></ConfirmProvider></I18nProvider></ToastProvider>);
|
||||
try {
|
||||
expect(mockedApi.get).toHaveBeenCalledTimes(1);
|
||||
await act(async () => { window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })); });
|
||||
expect(mockedApi.get).toHaveBeenCalledTimes(1);
|
||||
await act(async () => reject(new Error('synthetic unavailable read')));
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent(translations[to].attachmentsLoadFailed);
|
||||
expect(screen.queryByText(translations[from].attachmentsLoadFailed)).not.toBeInTheDocument();
|
||||
} finally {
|
||||
view.unmount();
|
||||
if (previous === null) localStorage.removeItem('uiLanguage'); else localStorage.setItem('uiLanguage', previous);
|
||||
}
|
||||
});
|
||||
|
||||
test.each([
|
||||
['rename', 'en', 'nb'], ['rename', 'nb', 'en'],
|
||||
['delete', 'en', 'nb'], ['delete', 'nb', 'en'],
|
||||
] as const)('pending attachment %s follows %s to %s without changing file data', async (action, from, to) => {
|
||||
const previous = localStorage.getItem('uiLanguage');
|
||||
const fileName = 'Søknad 東京 <draft>.pdf';
|
||||
const draft = 'Renamed 東京.pdf';
|
||||
localStorage.setItem('uiLanguage', from);
|
||||
mockedApi.get.mockResolvedValue({ data: [{ id: 7, fileName, fileSize: 2048, fileType: 'application/pdf', uploadDate: '2026-11-03T10:00:00Z' }] } as any);
|
||||
const view = render(<ToastProvider><I18nProvider><ConfirmProvider><PromptProvider><Attachments jobId={42} /></PromptProvider></ConfirmProvider></I18nProvider></ToastProvider>);
|
||||
try {
|
||||
const titleKey = action === 'rename' ? 'attachmentsRenameTitle' : 'attachmentsDeleteTitle';
|
||||
const labelKey = action === 'rename' ? 'attachmentsRename' : 'attachmentsDelete';
|
||||
fireEvent.click(await screen.findByRole('button', { name: `${translations[from][labelKey]}: ${fileName}` }));
|
||||
const dialog = await screen.findByRole('dialog', { name: translations[from][titleKey] });
|
||||
const input = action === 'rename' ? within(dialog).getByRole('textbox') as HTMLInputElement : null;
|
||||
if (input) {
|
||||
await waitFor(() => expect(input).toHaveFocus());
|
||||
expect(input).toHaveValue(fileName);
|
||||
fireEvent.change(input, { target: { value: draft } });
|
||||
input.setSelectionRange(2, 5);
|
||||
}
|
||||
const reads = mockedApi.get.mock.calls.length;
|
||||
await act(async () => { window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })); });
|
||||
expect(screen.getByRole('dialog', { name: translations[to][titleKey] })).toBe(dialog);
|
||||
expect(within(dialog).getByRole('button', { name: translations[to][labelKey] })).toBeVisible();
|
||||
expect(dialog).toHaveAccessibleDescription(action === 'rename' ? translations[to].attachmentsRenamePrompt : translations[to].attachmentsDeleteConfirm.replace('{name}', fileName));
|
||||
if (input) {
|
||||
expect(input).toHaveValue(draft);
|
||||
expect(input).toHaveFocus();
|
||||
expect([input.selectionStart, input.selectionEnd]).toEqual([2, 5]);
|
||||
}
|
||||
expect(dialog.querySelector('draft')).toBeNull();
|
||||
expect(mockedApi.get).toHaveBeenCalledTimes(reads);
|
||||
fireEvent.click(within(dialog).getByRole('button', { name: translations[to].cancel }));
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
|
||||
expect(mockedApi.patch).not.toHaveBeenCalled();
|
||||
expect(mockedApi.delete).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
view.unmount();
|
||||
mockedApi.get.mockReset();
|
||||
if (previous === null) localStorage.removeItem('uiLanguage'); else localStorage.setItem('uiLanguage', previous);
|
||||
}
|
||||
});
|
||||
|
||||
test.each([['en', '1.5 MB'], ['nb', '1,5 MB']])('attachment size follows the UI locale (%s)', async (language, size) => {
|
||||
const previous = localStorage.getItem('uiLanguage');
|
||||
localStorage.setItem('uiLanguage', language);
|
||||
|
||||
@@ -88,6 +88,8 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
const labelId = useId();
|
||||
const { toast } = useToast();
|
||||
const { language, t } = useI18n();
|
||||
const currentT = useRef(t);
|
||||
currentT.current = t;
|
||||
const { confirmAction, promptForValue } = useDialogActions();
|
||||
const [items, setItems] = useState<AttachmentItem[]>([]);
|
||||
const [loadError, setLoadError] = useState<string | null>(null);
|
||||
@@ -105,11 +107,11 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
setItems(res.data);
|
||||
setLoadError(null);
|
||||
} catch (error) {
|
||||
setLoadError(getApiErrorMessage(error, t("attachmentsLoadFailed")));
|
||||
setLoadError(getApiErrorMessage(error, currentT.current("attachmentsLoadFailed")));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [jobId, t]);
|
||||
}, [jobId]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
@@ -146,7 +148,7 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
);
|
||||
|
||||
const rename = async (a: AttachmentItem) => {
|
||||
const next = await promptForValue(t("attachmentsRenamePrompt"), a.fileName, { title: t("attachmentsRenameTitle"), confirmLabel: t("attachmentsRename") });
|
||||
const next = await promptForValue({ key: "attachmentsRenamePrompt" }, a.fileName, { title: { key: "attachmentsRenameTitle" }, confirmLabel: { key: "attachmentsRename" } });
|
||||
if (!next || next.trim() === a.fileName) return;
|
||||
try {
|
||||
await api.patch(`/attachments/${a.id}`, { fileName: next.trim() });
|
||||
@@ -168,7 +170,7 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
};
|
||||
|
||||
const remove = async (a: AttachmentItem) => {
|
||||
if (!(await confirmAction(t("attachmentsDeleteConfirm", { name: a.fileName }), { title: t("attachmentsDeleteTitle"), confirmLabel: t("attachmentsDelete"), destructive: true }))) return;
|
||||
if (!(await confirmAction({ key: "attachmentsDeleteConfirm", params: { name: a.fileName } }, { title: { key: "attachmentsDeleteTitle" }, confirmLabel: { key: "attachmentsDelete" }, destructive: true }))) return;
|
||||
try {
|
||||
await api.delete(`/attachments/${a.id}`);
|
||||
toast(t("attachmentsDeleted"), "success");
|
||||
|
||||
Reference in New Issue
Block a user