|
22 | 22 |
|
23 | 23 | import '@testing-library/jest-dom/vitest'; |
24 | 24 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
25 | | -import { render, screen, cleanup, waitFor, within, fireEvent } from '@testing-library/react'; |
| 25 | +import { render, screen, cleanup, waitFor, within, fireEvent, act } from '@testing-library/react'; |
26 | 26 | import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
27 | 27 |
|
28 | 28 | const APP = 'com.objectstack.account'; |
@@ -71,7 +71,10 @@ const { approvalsApiStub, ADAPTER, AUTH, I18N } = vi.hoisted(() => { |
71 | 71 | }; |
72 | 72 | const AUTH = { user: { id: 'u_1', email: 'approver@example.com' } }; |
73 | 73 | const I18N = { |
74 | | - t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key), |
| 74 | + // Interpolates like i18next, so a line built from values shows them. |
| 75 | + t: (key: string, options?: Record<string, unknown>) => |
| 76 | + String(options?.defaultValue ?? key).replace(/\{\{(\w+)\}\}/g, (whole, name: string) => |
| 77 | + options && name in options ? String(options[name]) : whole), |
75 | 78 | language: 'en', |
76 | 79 | }; |
77 | 80 |
|
@@ -239,3 +242,160 @@ describe('Approvals Inbox — a marked decide refusal is shown as written (objec |
239 | 242 | expect(await approveQuickFailingWith(refusal())).toBe('You are not allowed to perform this action.'); |
240 | 243 | }); |
241 | 244 | }); |
| 245 | + |
| 246 | +// A bulk approve that partly fails names each failed row WITH its reason — |
| 247 | +// the refusal's own text (`userMessage` first, through the same reader the |
| 248 | +// inline Approve uses above), else the page's generic line. When more rows |
| 249 | +// fail than the toast lists, its "Show all" action opens a dialog listing |
| 250 | +// every one. |
| 251 | +describe('Approvals Inbox — a partly failed bulk approve says why each row failed', () => { |
| 252 | + const MARKED = '此请求由您提交,而其审批步骤不允许提交人批准自己的请求,因此您不能批准它。'; |
| 253 | + const markedRefusal = () => Object.assign(new Error('FORBIDDEN: submitter cannot approve'), { |
| 254 | + code: 'FORBIDDEN', |
| 255 | + status: 403, |
| 256 | + details: { code: 'FORBIDDEN', error: 'refused', userMessage: MARKED }, |
| 257 | + }); |
| 258 | + const decidedRefusal = () => Object.assign(new Error('INVALID_STATE: already decided'), { |
| 259 | + code: 'INVALID_STATE', |
| 260 | + status: 409, |
| 261 | + details: { code: 'INVALID_STATE', error: 'already decided' }, |
| 262 | + }); |
| 263 | + const ALREADY_DECIDED = 'This request was already decided. Refresh the list.'; |
| 264 | + |
| 265 | + const row = (n: number) => ({ |
| 266 | + id: `req_${n}`, |
| 267 | + record_id: `inv_${n}`, |
| 268 | + record_title: `Bulk Invoice ${n}`, |
| 269 | + process_name: 'invoice_approval', |
| 270 | + process_label: 'Invoice Approval', |
| 271 | + object_name: 'showcase_invoice', |
| 272 | + object_label: 'Invoice', |
| 273 | + status: 'pending', |
| 274 | + pending_approvers: ['u_1'], |
| 275 | + submitter_id: 'u_2', |
| 276 | + submitter_name: 'Sam Submitter', |
| 277 | + // Newest first is the inbox's order: row 1 is the newest, so it lists first. |
| 278 | + submitted_at: `2026-08-${String(30 - n).padStart(2, '0')}T00:00:00.000Z`, |
| 279 | + }); |
| 280 | + |
| 281 | + let originalList: ReturnType<typeof approvalsApiStub.listRequests.getMockImplementation>; |
| 282 | + beforeEach(() => { originalList = approvalsApiStub.listRequests.getMockImplementation(); }); |
| 283 | + afterEach(() => { |
| 284 | + approvalsApiStub.listRequests.mockImplementation(originalList!); |
| 285 | + approvalsApiStub.approve.mockReset(); |
| 286 | + approvalsApiStub.approve.mockImplementation(async () => ({ data: row(0), finalized: true }) as any); |
| 287 | + }); |
| 288 | + |
| 289 | + /** Render `count` actionable rows; the returned function runs one bulk approve. */ |
| 290 | + async function renderRows(count: number) { |
| 291 | + const rows = Array.from({ length: count }, (_, i) => row(i + 1)); |
| 292 | + approvalsApiStub.listRequests.mockImplementation(async () => ({ data: rows }) as any); |
| 293 | + render( |
| 294 | + <MemoryRouter initialEntries={[`/apps/${APP}/system/approvals`]}> |
| 295 | + <Routes> |
| 296 | + <Route path="/apps/:appName/system/approvals" element={<ApprovalsInboxPage />} /> |
| 297 | + </Routes> |
| 298 | + </MemoryRouter>, |
| 299 | + ); |
| 300 | + const box = await screen.findByTestId('approvals-inbox-table-scroller'); |
| 301 | + /** Select every row, confirm a bulk approve in which the `fail` rows are refused. */ |
| 302 | + return async (fail: Record<string, () => Error>) => { |
| 303 | + const before = approvalsApiStub.approve.mock.calls.length; |
| 304 | + approvalsApiStub.approve.mockImplementation((async (id: string) => { |
| 305 | + if (fail[id]) throw fail[id](); |
| 306 | + return { data: rows.find(r => r.id === id), finalized: true }; |
| 307 | + }) as any); |
| 308 | + for (const r of rows) { |
| 309 | + const link = await within(box).findByRole('link', { name: r.record_title }); |
| 310 | + const tr = link.closest('tr') as HTMLElement; |
| 311 | + fireEvent.click(within(tr).getByRole('checkbox', { name: 'Select request' })); |
| 312 | + } |
| 313 | + fireEvent.click(await screen.findByRole('button', { name: `Approve ${count}` })); |
| 314 | + const dialog = await screen.findByRole('alertdialog'); |
| 315 | + fireEvent.click(within(dialog).getByRole('button', { name: `Approve ${count}` })); |
| 316 | + await waitFor(() => expect(approvalsApiStub.approve).toHaveBeenCalledTimes(before + count)); |
| 317 | + await waitFor(() => expect(screen.queryByRole('alertdialog')).toBeNull()); |
| 318 | + // The run is over once the selection it cleared is gone. |
| 319 | + await waitFor(() => expect(screen.queryByRole('button', { name: `Approve ${count}` })).toBeNull()); |
| 320 | + }; |
| 321 | + } |
| 322 | + |
| 323 | + async function bulkApprove(count: number, fail: Record<string, () => Error>) { |
| 324 | + await (await renderRows(count))(fail); |
| 325 | + } |
| 326 | + |
| 327 | + it('one refused row: the toast names that row with its marked reason', async () => { |
| 328 | + await bulkApprove(2, { req_2: markedRefusal }); |
| 329 | + await waitFor(() => expect(toasts.error).toHaveBeenCalledTimes(1)); |
| 330 | + const [title, options] = toasts.error.mock.calls[0] as [string, Record<string, any>]; |
| 331 | + expect(title).toBe('1 succeeded, 1 failed'); |
| 332 | + // The failed row, and why — the server's marked text, not a bare title. |
| 333 | + expect(options.description).toBe(`Bulk Invoice 2: ${MARKED}`); |
| 334 | + expect(toasts.success).not.toHaveBeenCalled(); |
| 335 | + // Everything fits in the toast: no overflow action; it still stays until |
| 336 | + // dismissed, so the reasons can be read. |
| 337 | + expect(options.action).toBeUndefined(); |
| 338 | + expect(options.duration).toBe(Infinity); |
| 339 | + }); |
| 340 | + |
| 341 | + it('CONTROL: every row succeeds — a success toast and no failure feedback', async () => { |
| 342 | + await bulkApprove(3, {}); |
| 343 | + await waitFor(() => expect(toasts.success).toHaveBeenCalledTimes(1)); |
| 344 | + expect(toasts.error).not.toHaveBeenCalled(); |
| 345 | + expect(screen.queryByTestId('approvals-bulk-failures')).toBeNull(); |
| 346 | + }); |
| 347 | + |
| 348 | + it('more failures than the toast lists: "Show all" reaches every row and its reason', async () => { |
| 349 | + await bulkApprove(6, { |
| 350 | + req_1: markedRefusal, req_2: decidedRefusal, req_3: decidedRefusal, |
| 351 | + req_4: markedRefusal, req_5: decidedRefusal, |
| 352 | + }); |
| 353 | + await waitFor(() => expect(toasts.error).toHaveBeenCalledTimes(1)); |
| 354 | + const [title, options] = toasts.error.mock.calls[0] as [string, Record<string, any>]; |
| 355 | + expect(title).toBe('1 succeeded, 5 failed'); |
| 356 | + // The toast lists the first three, each with its reason … |
| 357 | + expect(options.description).toBe([ |
| 358 | + `Bulk Invoice 1: ${MARKED}`, |
| 359 | + `Bulk Invoice 2: ${ALREADY_DECIDED}`, |
| 360 | + `Bulk Invoice 3: ${ALREADY_DECIDED}`, |
| 361 | + ].join('\n')); |
| 362 | + expect(options.action.label).toBe('Show all (5)'); |
| 363 | + // The toast stays until dismissed, so its action cannot time out. |
| 364 | + expect(options.duration).toBe(Infinity); |
| 365 | + act(() => options.action.onClick()); |
| 366 | + const list = await screen.findByTestId('approvals-bulk-failures'); |
| 367 | + const items = within(list).getAllByRole('listitem'); |
| 368 | + expect(items).toHaveLength(5); |
| 369 | + const expected: Array<[string, string]> = [ |
| 370 | + ['Bulk Invoice 1', MARKED], ['Bulk Invoice 2', ALREADY_DECIDED], ['Bulk Invoice 3', ALREADY_DECIDED], |
| 371 | + ['Bulk Invoice 4', MARKED], ['Bulk Invoice 5', ALREADY_DECIDED], |
| 372 | + ]; |
| 373 | + for (const [title, reason] of expected) { |
| 374 | + const item = items.find(li => li.textContent?.startsWith(title)); |
| 375 | + expect(item, title).toBeDefined(); |
| 376 | + expect(item).toHaveTextContent(reason); |
| 377 | + } |
| 378 | + expect(within(list).queryByText('Bulk Invoice 6')).toBeNull(); |
| 379 | + }); |
| 380 | + |
| 381 | + it("a toast's \"Show all\" opens its own run's rows, not a later run's", async () => { |
| 382 | + const runBulk = await renderRows(6); |
| 383 | + await runBulk({ |
| 384 | + req_1: markedRefusal, req_2: decidedRefusal, req_3: decidedRefusal, |
| 385 | + req_4: markedRefusal, req_5: decidedRefusal, |
| 386 | + }); |
| 387 | + await waitFor(() => expect(toasts.error).toHaveBeenCalledTimes(1)); |
| 388 | + const first = toasts.error.mock.calls[0][1] as Record<string, any>; |
| 389 | + expect(first.action.label).toBe('Show all (5)'); |
| 390 | + // A second, clean run while the first toast is still up. |
| 391 | + await runBulk({}); |
| 392 | + await waitFor(() => expect(toasts.success).toHaveBeenCalledTimes(1)); |
| 393 | + act(() => first.action.onClick()); |
| 394 | + const dialog = await screen.findByRole('alertdialog'); |
| 395 | + expect(within(dialog).getByText('Not decided: 5')).toBeInTheDocument(); |
| 396 | + const items = within(within(dialog).getByTestId('approvals-bulk-failures')).getAllByRole('listitem'); |
| 397 | + expect(items.map(li => li.firstElementChild?.textContent)).toEqual([ |
| 398 | + 'Bulk Invoice 1', 'Bulk Invoice 2', 'Bulk Invoice 3', 'Bulk Invoice 4', 'Bulk Invoice 5', |
| 399 | + ]); |
| 400 | + }); |
| 401 | +}); |
0 commit comments