fix(i18n): preserve pending admin confirmations and users reads

This commit is contained in:
cesnimda
2026-11-17 08:45:11 +01:00
parent 07fbd5f72c
commit 4e224151db
2 changed files with 60 additions and 12 deletions
@@ -10,6 +10,7 @@ import { PromptProvider } from "./prompt";
import { getTheme } from "./theme";
import { ToastProvider } from "./toast";
import AdminUsersPage from "./views/AdminUsersPage";
import { translations } from "./i18n/translations";
jest.mock("./api", () => ({
api: { get: jest.fn(), put: jest.fn(), post: jest.fn(), delete: jest.fn() },
@@ -60,6 +61,53 @@ async function openUserActions() {
return screen.findByRole("menu");
}
test.each((['en', 'nb'] as const).flatMap(from => (['pending', 'visible'] as const).map(state => [from, from === 'en' ? 'nb' : 'en', state] as const)))('users %s to %s keeps %s read failure and draft without another request', async (from, to, state) => {
localStorage.setItem('uiLanguage', from);
let reject!: (error: unknown) => void;
const pending = new Promise((_resolve, no) => { reject = no; });
if (state === 'pending') mockedApi.get.mockReturnValueOnce(pending as any);
else mockedApi.get.mockRejectedValueOnce(new Error('synthetic unavailable users'));
renderPage([]);
fireEvent.change(screen.getByLabelText(translations[from].profileEmail), { target: { value: 'draft@example.test' } });
if (state === 'visible') await screen.findByRole('button', { name: translations[from].retry });
await act(async () => { window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })); });
if (state === 'pending') await act(async () => reject(new Error('synthetic unavailable users')));
expect(mockedApi.get).toHaveBeenCalledTimes(1);
expect(await screen.findByRole('button', { name: translations[to].retry })).toBeVisible();
expect(screen.getAllByText(translations[to].adminUsersLoadFailed).length).toBeGreaterThan(0);
expect(screen.queryByText(translations[from].adminUsersLoadFailed)).not.toBeInTheDocument();
expect(screen.getByLabelText(translations[to].profileEmail)).toHaveValue('draft@example.test');
expect(mockedApi.post).not.toHaveBeenCalled(); expect(mockedApi.put).not.toHaveBeenCalled(); expect(mockedApi.delete).not.toHaveBeenCalled();
});
test.each(([true, false] as const).flatMap(mobile => (['demote self', 'demote other', 'delete self', 'delete other'] as const).flatMap(action => (['en', 'nb'] as const).map(from => [mobile, action, from, from === 'en' ? 'nb' : 'en'] as const))))('pending admin confirmation mobile=%s %s follows %s to %s without applying it', async (mobile, action, from, to) => {
localStorage.setItem('uiLanguage', from);
(window.matchMedia as jest.Mock).mockImplementation(() => ({ matches: mobile, addEventListener: jest.fn(), removeEventListener: jest.fn(), addListener: jest.fn(), removeListener: jest.fn() }));
const isCurrentUser = action.endsWith('self');
const demote = action.startsWith('demote');
const name = 'Søker 東京 <review>';
renderPage([{ id: 'selected-user', email: 'selected@example.test', userName: name, roles: demote ? ['Admin'] : [], emailConfirmed: true, isCurrentUser, canRemoveAdmin: true }]);
fireEvent.click(await screen.findByRole('button', { name: translations[from].adminUsersActionsFor.replace('{name}', 'selected@example.test') }));
fireEvent.click(within(await screen.findByRole('menu')).getByRole('menuitem', { name: translations[from][demote ? 'adminUsersRemoveAdmin' : 'adminUsersDelete'] }));
const titleKey = demote ? isCurrentUser ? 'adminUsersSelfDemoteTitle' : 'adminUsersDemoteTitle' : 'adminUsersDeleteConfirmTitle';
const messageKey = demote ? isCurrentUser ? 'adminUsersSelfDemoteWarning' : 'adminUsersDemoteWarning' : isCurrentUser ? 'adminUsersDeleteSelfWarning' : 'adminUsersDeleteConfirmNamed';
const opened = await screen.findByRole('dialog', { name: translations[from][titleKey] });
const cancel = within(opened).getByRole('button', { name: translations[from].cancel });
act(() => cancel.focus());
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(opened);
expect(opened).toHaveAccessibleDescription(translations[to][messageKey].replace('{name}', name));
expect(opened.querySelector('review')).toBeNull();
expect(within(opened).getByRole('button', { name: translations[to][demote ? 'adminUsersRemoveAdmin' : 'adminUsersDelete'] })).toBeVisible();
expect(cancel).toHaveFocus();
expect(mockedApi.get).toHaveBeenCalledTimes(reads);
expect(mockedApi.put).not.toHaveBeenCalled(); expect(mockedApi.delete).not.toHaveBeenCalled();
fireEvent.click(within(opened).getByRole('button', { name: translations[to].cancel }));
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
expect(mockedApi.put).not.toHaveBeenCalled(); expect(mockedApi.delete).not.toHaveBeenCalled();
});
test.each([['en', 'Rows per page:'], ['nb', 'Rader per side:']])('desktop user-grid pagination follows %s UI language', async (language, label) => {
window.localStorage.setItem('uiLanguage', language);
(window.matchMedia as jest.Mock).mockImplementation(() => ({ matches: false, addEventListener: jest.fn(), removeEventListener: jest.fn(), addListener: jest.fn(), removeListener: jest.fn() }));