fix(i18n): keep dashboard and saved-search decisions translated

This commit is contained in:
cesnimda
2026-11-17 15:17:45 +01:00
parent 24e98cbe75
commit b652b5c55d
4 changed files with 108 additions and 6 deletions
@@ -21,9 +21,9 @@ export function DashboardNavigationGuard({ dirty, saving }: { dirty: boolean; sa
if (saving) { setWaiting(true); blocker.reset(); return; }
if (pending.current) return;
pending.current = true;
void confirm({ title: t("dashboardDiscardTitle"), message: t("dashboardDiscardBody"), confirmLabel: t("dashboardDiscard"), cancelLabel: t("workspaceKeepEditing"), destructive: true })
void confirm({ title: { key: "dashboardDiscardTitle" }, message: { key: "dashboardDiscardBody" }, confirmLabel: { key: "dashboardDiscard" }, cancelLabel: { key: "workspaceKeepEditing" }, destructive: true })
.then(approved => { if (approved) blocker.proceed(); else blocker.reset(); });
}, [blocker, confirm, saving, t]);
}, [blocker, confirm, saving]);
return waiting && saving ? <Typography role="status">{t("dashboardWaitForSave")}</Typography> : null;
}
@@ -48,13 +48,13 @@ export default function DashboardLayoutEditor({ draft, dirty, saving, saveError,
panel.current?.scrollIntoView?.({ block: "start" });
}, []);
const cancel = async () => {
if (!dirty || await confirm({ title: t("dashboardDiscardTitle"), message: t("dashboardDiscardBody"), confirmLabel: t("dashboardDiscard"), destructive: true })) onCancel();
if (!dirty || await confirm({ title: { key: "dashboardDiscardTitle" }, message: { key: "dashboardDiscardBody" }, confirmLabel: { key: "dashboardDiscard" }, destructive: true })) onCancel();
};
const reset = async () => {
if (await confirm({ title: t("dashboardReset"), message: t("dashboardResetBody"), confirmLabel: t("dashboardReset") })) onChange({ ...defaultDashboardLayout(), revision: draft.revision });
if (await confirm({ title: { key: "dashboardReset" }, message: { key: "dashboardResetBody" }, confirmLabel: { key: "dashboardReset" } })) onChange({ ...defaultDashboardLayout(), revision: draft.revision });
};
const reload = async () => {
if (await confirm({ title: t("dashboardReload"), message: t("dashboardReloadBody"), confirmLabel: t("dashboardReload"), destructive: true })) onReload();
if (await confirm({ title: { key: "dashboardReload" }, message: { key: "dashboardReloadBody" }, confirmLabel: { key: "dashboardReload" }, destructive: true })) onReload();
};
return <Box ref={panel} tabIndex={-1} role="group" aria-label={t("dashboardEditing")} sx={{ scrollMarginTop: "calc(var(--app-shell-header-height, 124px) + 16px)" }} onKeyDown={event => {
if (event.key === "Escape" && !saving && !event.defaultPrevented) { event.preventDefault(); void cancel(); }
@@ -0,0 +1,66 @@
import React from 'react';
import '@testing-library/jest-dom';
import { act, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { createMemoryRouter, Link, RouterProvider } from 'react-router-dom';
import DashboardLayoutEditor, { DashboardNavigationGuard } from './components/DashboardLayoutEditor';
import { defaultDashboardLayout } from './dashboardLayout';
import { I18nProvider } from './i18n/I18nProvider';
import { ConfirmProvider } from './confirm';
import { translations } from './i18n/translations';
beforeEach(() => localStorage.clear());
test.each((['cancel', 'reset', 'reload'] as const).flatMap(action => (['en', 'nb'] as const).map(from => [action, from, from === 'en' ? 'nb' : 'en'] as const)))('dashboard %s confirmation follows %s to %s without changing the draft', async (action, from, to) => {
localStorage.setItem('uiLanguage', from);
const initial = defaultDashboardLayout();
const draft = { ...initial, revision: 7, widgets: initial.widgets.map((widget, i) => i === 0 ? { ...widget, visible: false } : widget) };
const snapshot = JSON.stringify(draft);
const onChange = jest.fn(), onCancel = jest.fn(), onReload = jest.fn(), onSave = jest.fn();
render(<I18nProvider><ConfirmProvider><DashboardLayoutEditor draft={draft} dirty saving={false} saveError={action === 'reload' ? 'conflict' : null} conflict={action === 'reload'} onChange={onChange} onCancel={onCancel} onReload={onReload} onSave={onSave} /></ConfirmProvider></I18nProvider>);
const titleKey = action === 'cancel' ? 'dashboardDiscardTitle' : action === 'reset' ? 'dashboardReset' : 'dashboardReload';
const bodyKey = action === 'cancel' ? 'dashboardDiscardBody' : action === 'reset' ? 'dashboardResetBody' : 'dashboardReloadBody';
const actionKey = action === 'cancel' ? 'dashboardDiscard' : titleKey;
const opener = screen.getByRole('button', { name: translations[from][action === 'cancel' ? 'cancel' : titleKey] });
act(() => opener.focus()); fireEvent.click(opener);
const dialog = await screen.findByRole('dialog', { name: translations[from][titleKey] });
await act(async () => { window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })); });
expect(screen.getByRole('dialog', { name: translations[to][titleKey] })).toBe(dialog);
expect(dialog).toHaveAccessibleDescription(translations[to][bodyKey]);
expect(within(dialog).getByRole('button', { name: translations[to][actionKey] })).toBeVisible();
expect(JSON.stringify(draft)).toBe(snapshot);
for (const callback of [onChange, onCancel, onReload, onSave]) expect(callback).not.toHaveBeenCalled();
fireEvent.click(within(dialog).getByRole('button', { name: translations[to].cancel }));
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
expect(JSON.stringify(draft)).toBe(snapshot);
for (const callback of [onChange, onCancel, onReload, onSave]) expect(callback).not.toHaveBeenCalled();
fireEvent.click(opener);
fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: translations[to][actionKey] }));
const expected = action === 'cancel' ? onCancel : action === 'reset' ? onChange : onReload;
await waitFor(() => expect(expected).toHaveBeenCalledTimes(1));
if (action === 'reset') {
expect(onChange.mock.calls[0][0].revision).toBe(7);
expect(onChange.mock.calls[0][0].widgets.every((widget: { visible: boolean }) => widget.visible)).toBe(true);
}
expect(onSave).not.toHaveBeenCalled();
});
test.each(['en', 'nb'] as const)('dashboard dirty-navigation confirmation follows %s without leaving or saving', async from => {
localStorage.setItem('uiLanguage', from);
const to = from === 'en' ? 'nb' : 'en';
const router = createMemoryRouter([
{ path: '/dashboard', element: <><DashboardNavigationGuard dirty saving={false} /><Link to='/jobs'>Leave dashboard</Link></> },
{ path: '/jobs', element: <div>Jobs destination</div> },
], { initialEntries: ['/dashboard'] });
render(<I18nProvider><ConfirmProvider><RouterProvider router={router} /></ConfirmProvider></I18nProvider>);
fireEvent.click(screen.getByRole('link', { name: 'Leave dashboard' }));
const dialog = await screen.findByRole('dialog', { name: translations[from].dashboardDiscardTitle });
await act(async () => { window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })); });
expect(screen.getByRole('dialog', { name: translations[to].dashboardDiscardTitle })).toBe(dialog);
expect(dialog).toHaveAccessibleDescription(translations[to].dashboardDiscardBody);
expect(within(dialog).getByRole('button', { name: translations[to].dashboardDiscard })).toBeVisible();
expect(router.state.location.pathname).toBe('/dashboard');
fireEvent.click(within(dialog).getByRole('button', { name: translations[to].workspaceKeepEditing }));
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
expect(router.state.location.pathname).toBe('/dashboard');
expect(screen.queryByText('Jobs destination')).not.toBeInTheDocument();
});
+36
View File
@@ -305,6 +305,42 @@ function deferred<T>() {
return { promise, resolve, reject };
}
test.each([false, true].flatMap(narrow => (['en', 'nb'] as const).map(from => [narrow, from] as const)))('saved-search deletion keeps its decision and literal name across a language switch (mobile=%s, %s)', async (narrow, from) => {
mockNarrow = narrow;
localStorage.setItem('uiLanguage', from);
const to = from === 'en' ? 'nb' : 'en';
const saved = { ...savedSearch, name: '<nurse> Øresund & 東京' };
savedFixture([saved]);
mockedApi.delete.mockResolvedValue({ data: null } as any);
renderPage();
fireEvent.change(screen.getByLabelText(translations[from].jobDiscoveryRoleCompany), { target: { value: 'my draft query' } });
if (narrow) fireEvent.click(screen.getByRole('button', { name: translations[from].jobDiscoveryFilters }));
const opener = await screen.findByRole('button', { name: translations[from].adminUsersDelete });
act(() => opener.focus());
fireEvent.click(opener);
const dialog = await screen.findByRole('dialog', { name: translations[from].jobDiscoveryDeleteSavedTitle });
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].jobDiscoveryDeleteSavedTitle })).toBe(dialog);
expect(dialog).toHaveAccessibleDescription(translations[to].jobDiscoveryDeleteSavedConfirm.replace('{name}', saved.name));
expect(dialog.querySelector('nurse')).toBeNull();
expect(mockedApi.get).toHaveBeenCalledTimes(reads);
expect(mockedApi.delete).not.toHaveBeenCalled();
expect(mockedApi.post).not.toHaveBeenCalled();
expect(mockedApi.patch).not.toHaveBeenCalled();
fireEvent.click(within(dialog).getByRole('button', { name: translations[to].cancel }));
await waitFor(() => expect(screen.queryByRole('dialog', { name: translations[to].jobDiscoveryDeleteSavedTitle })).not.toBeInTheDocument());
await waitFor(() => expect(opener).toHaveFocus());
expect(screen.getByLabelText(translations[to].jobDiscoveryRoleCompany)).toHaveValue('my draft query');
expect(screen.getByText(saved.name)).toBeVisible();
fireEvent.click(opener);
fireEvent.click(within(await screen.findByRole('dialog', { name: translations[to].jobDiscoveryDeleteSavedTitle })).getByRole('button', { name: translations[to].adminUsersDelete }));
await waitFor(() => expect(mockedApi.delete).toHaveBeenCalledTimes(1));
expect(mockedApi.delete).toHaveBeenCalledWith('/job-discovery/saved-searches/7');
await waitFor(() => expect(screen.queryByText(saved.name)).not.toBeInTheDocument());
expect(mockedApi.get).toHaveBeenCalledTimes(reads);
});
test("a manual search after saved-search results has no dismiss action belonging to the saved search", async () => {
savedFixture([savedSearch], [{ ...discoveredJob, id: "manual-1", title: "Manual vacancy" }]);
mockedApi.post.mockResolvedValueOnce(savedRun() as any);
@@ -162,7 +162,7 @@ export default function JobDiscoveryPage() {
pending.current = true;
const control = document.activeElement;
try {
if (!(await confirmAction(t("jobDiscoveryDeleteSavedConfirm", { name: saved.name }), { title: t("jobDiscoveryDeleteSavedTitle"), confirmLabel: t("adminUsersDelete"), destructive: true }))) return;
if (!(await confirmAction({ key: "jobDiscoveryDeleteSavedConfirm", params: { name: saved.name } }, { title: { key: "jobDiscoveryDeleteSavedTitle" }, confirmLabel: { key: "adminUsersDelete" }, destructive: true }))) return;
setMutating(true);
setError(null);
await api.delete(`/job-discovery/saved-searches/${saved.id}`);