diff --git a/job-tracker-ui/src/admin-users-page.test.tsx b/job-tracker-ui/src/admin-users-page.test.tsx index 16111253..4de6546e 100644 --- a/job-tracker-ui/src/admin-users-page.test.tsx +++ b/job-tracker-ui/src/admin-users-page.test.tsx @@ -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 東京 '; + 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() })); diff --git a/job-tracker-ui/src/views/AdminUsersPage.tsx b/job-tracker-ui/src/views/AdminUsersPage.tsx index 591d0878..f07ca80d 100644 --- a/job-tracker-ui/src/views/AdminUsersPage.tsx +++ b/job-tracker-ui/src/views/AdminUsersPage.tsx @@ -127,7 +127,7 @@ export default function AdminUsersPage() { const currentTranslation = useRef(t); useEffect(() => { currentTranslation.current=t; }, [t]); useEffect(() => { const lifetime=owner.current; lifetime.mounted=true; lifetime.generation+=1; return () => { lifetime.mounted=false; lifetime.generation+=1; }; }, []); - const userResource=useViewResource(async () => (await api.get("/users")).data ?? [], { initialData: [], errorMessage: t("adminUsersLoadFailed") }); + const userResource=useViewResource(async () => (await api.get("/users")).data ?? [], { initialData: [], errorMessage: "" }); const users=userResource.data; const loading=userResource.loading || userResource.refreshing; const load=userResource.reload; @@ -179,11 +179,11 @@ export default function AdminUsersPage() { const name = u.userName || u.email || u.id; const confirmed = await confirmAction( u.isCurrentUser - ? t("adminUsersSelfDemoteWarning") - : t("adminUsersDemoteWarning", { name }), + ? { key: "adminUsersSelfDemoteWarning" } + : { key: "adminUsersDemoteWarning", params: { name } }, { - title: u.isCurrentUser ? t("adminUsersSelfDemoteTitle") : t("adminUsersDemoteTitle"), - confirmLabel: t("adminUsersRemoveAdmin"), + title: { key: u.isCurrentUser ? "adminUsersSelfDemoteTitle" : "adminUsersDemoteTitle" }, + confirmLabel: { key: "adminUsersRemoveAdmin" }, destructive: true, }, ); @@ -201,7 +201,7 @@ export default function AdminUsersPage() { if (active()) toast(getApiErrorMessage(e, currentTranslation.current("adminUsersRolesUpdateFailed")), "error"); } finally { finishAction(active); } - }, [beginAction, confirmAction, finishAction, load, t, toast]); + }, [beginAction, confirmAction, finishAction, load, toast]); const sendReset = useCallback(async (u: UserDto) => { const active=beginAction(); if (!active) return; @@ -218,10 +218,10 @@ export default function AdminUsersPage() { const active=beginAction(); if (!active) return; try { const name = u.userName || u.email || u.id; - const message = u.isCurrentUser - ? t("adminUsersDeleteSelfWarning") - : t("adminUsersDeleteConfirmNamed", { name }); - if (!(await confirmAction(message, { title: t("adminUsersDeleteConfirmTitle"), confirmLabel: t("adminUsersDelete"), destructive: true })) || !active()) return; + if (!(await confirmAction(u.isCurrentUser + ? { key: "adminUsersDeleteSelfWarning" } + : { key: "adminUsersDeleteConfirmNamed", params: { name } }, + { title: { key: "adminUsersDeleteConfirmTitle" }, confirmLabel: { key: "adminUsersDelete" }, destructive: true })) || !active()) return; const response=await api.delete(`/users/${u.id}`, { headers: { "X-Confirm-Account-Deletion": u.email || u.userName || "" } }); if (!active()) return; toast(currentTranslation.current(response.status===202 ? "adminUsersDeletionQueued" : "adminUsersDeleted"), "info"); @@ -230,7 +230,7 @@ export default function AdminUsersPage() { if (active()) toast(getApiErrorMessage(e, currentTranslation.current("adminUsersDeleteFailed")), "error"); } finally { finishAction(active); } - }, [beginAction, confirmAction, finishAction, load, t, toast]); + }, [beginAction, confirmAction, finishAction, load, toast]); const columns = useMemo(() => [ { @@ -301,7 +301,7 @@ export default function AdminUsersPage() { - { body.current?.focus(); await load(); }} /> + { body.current?.focus(); await load(); }} /> {!userResource.error ? isMobile ? (