|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#36911 — the row menu follows the approval lock. |
| 11 | + * |
| 12 | + * A row held by a locking approval is refused `RECORD_LOCKED` on update. The |
| 13 | + * record page offers no Edit on such a record (objectstack#35593) and keeps |
| 14 | + * Delete, because the server lets a locked record be deleted and cancels its |
| 15 | + * request. The grid's row menu, which the list page and a record page's |
| 16 | + * related tab both render, offered Edit on it. |
| 17 | + * |
| 18 | + * The menu now reads the lock the select-first cells read |
| 19 | + * (`useRowApprovalLocks`, the batch door |
| 20 | + * `POST /api/v1/approvals/records/:object/locks`), asked when a row's actions |
| 21 | + * are reached for, never on first paint (objectstack#35795). Only an ANSWERED |
| 22 | + * lock hides Edit: a 501, a `null` entry, a failed read or a read not yet |
| 23 | + * asked leaves the menu as it was. |
| 24 | + */ |
| 25 | + |
| 26 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 27 | +import { render, screen, waitFor, cleanup, act, fireEvent, within } from '@testing-library/react'; |
| 28 | +import userEvent from '@testing-library/user-event'; |
| 29 | +import '@testing-library/jest-dom'; |
| 30 | +import React from 'react'; |
| 31 | + |
| 32 | +// Stable-identity permission stub, as `rowRecordCrudVerdict.test.tsx` has it: |
| 33 | +// ObjectGrid carries `perms` in memo dependency arrays. |
| 34 | +const { permsStub } = vi.hoisted(() => ({ |
| 35 | + permsStub: { |
| 36 | + isLoaded: false, |
| 37 | + checkField: () => true, |
| 38 | + getObjectApiOperations: () => ['get', 'list', 'create', 'update', 'delete'], |
| 39 | + can: () => true, |
| 40 | + }, |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 44 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 45 | + return { ...actual, usePermissions: () => permsStub }; |
| 46 | +}); |
| 47 | + |
| 48 | +import { ObjectGrid } from '../ObjectGrid'; |
| 49 | +import { __clearRecordCrudVerdictCache } from '../hooks/useRecordCrudVerdicts'; |
| 50 | +import { __resetApprovalsServiceLatch } from '../hooks/useRowApprovalLocks'; |
| 51 | +import { useListPageScope } from '../listPageScope'; |
| 52 | +import { registerAllFields } from '@object-ui/fields'; |
| 53 | +import { ActionProvider, SchemaRendererProvider } from '@object-ui/react'; |
| 54 | + |
| 55 | +registerAllFields(); |
| 56 | + |
| 57 | +const OBJECT = 'showcase_project'; |
| 58 | +const LOCKS_PATH = `/api/v1/approvals/records/${OBJECT}/locks`; |
| 59 | +/** Under an executive review whose node locks the record. */ |
| 60 | +const LOCKED_ROW = { id: 'p_locked', name: 'Data Centre Build' }; |
| 61 | +/** Its sibling on the same page, with no approval running. */ |
| 62 | +const OPEN_ROW = { id: 'p_open', name: 'Office Refresh' }; |
| 63 | + |
| 64 | +/** What the lock door answers, per id; `undefined` is the read's `null` entry. */ |
| 65 | +type LockDoor = |
| 66 | + | { kind: 'answers'; locked: Record<string, boolean | null> } |
| 67 | + | { kind: 'status'; status: number } |
| 68 | + | { kind: 'network' }; |
| 69 | + |
| 70 | +const server = { |
| 71 | + door: { kind: 'answers', locked: {} } as LockDoor, |
| 72 | + lockCalls: [] as Array<{ recordIds: string[] }>, |
| 73 | + responses: [] as Promise<unknown>[], |
| 74 | + /** Set by {@link hold}: lock answers wait here until `release` is called. */ |
| 75 | + gate: null as Promise<void> | null, |
| 76 | + release: () => {}, |
| 77 | + hold() { |
| 78 | + server.gate = new Promise<void>((resolve) => { |
| 79 | + server.release = () => resolve(); |
| 80 | + }); |
| 81 | + }, |
| 82 | + reset() { |
| 83 | + server.gate = null; |
| 84 | + server.release = () => {}; |
| 85 | + server.door = { kind: 'answers', locked: { [LOCKED_ROW.id]: true, [OPEN_ROW.id]: false } }; |
| 86 | + server.lockCalls = []; |
| 87 | + server.responses = []; |
| 88 | + }, |
| 89 | +}; |
| 90 | + |
| 91 | +const apiFetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => { |
| 92 | + const url = String(input); |
| 93 | + const body = JSON.parse(String(init?.body ?? '{}')); |
| 94 | + const respond = (status: number, payload: unknown) => { |
| 95 | + const res = { ok: status >= 200 && status < 300, status, json: async () => payload } as unknown as Response; |
| 96 | + const settled = Promise.resolve(res); |
| 97 | + server.responses.push(settled); |
| 98 | + return settled; |
| 99 | + }; |
| 100 | + if (url === LOCKS_PATH) { |
| 101 | + server.lockCalls.push(body); |
| 102 | + if (server.gate) await server.gate; |
| 103 | + const door = server.door; |
| 104 | + if (door.kind === 'network') { |
| 105 | + const failed = Promise.reject(new TypeError('Failed to fetch')); |
| 106 | + server.responses.push(failed.catch(() => undefined)); |
| 107 | + return failed; |
| 108 | + } |
| 109 | + if (door.kind === 'status') return respond(door.status, { success: false }); |
| 110 | + const ids: string[] = body.recordIds ?? []; |
| 111 | + return respond(200, { |
| 112 | + success: true, |
| 113 | + data: { records: ids.map((recordId) => ({ recordId, locked: door.locked[recordId] ?? null })) }, |
| 114 | + }); |
| 115 | + } |
| 116 | + // `security/explain`: every row may be written, so only the lock can take Edit away. |
| 117 | + const ids: string[] = Array.isArray(body.recordIds) ? body.recordIds : []; |
| 118 | + return respond(200, { |
| 119 | + allowed: true, |
| 120 | + object: body.object, |
| 121 | + operation: body.operation, |
| 122 | + principal: { userId: 'u_member' }, |
| 123 | + layers: [], |
| 124 | + records: ids.map((recordId) => ({ recordId, visible: true })), |
| 125 | + }); |
| 126 | +}; |
| 127 | + |
| 128 | +/** The fixture page bar: it registers its page column as a list page scope. */ |
| 129 | +const PageBar: React.FC = () => { |
| 130 | + const [scope, setScope] = React.useState<HTMLElement | null>(null); |
| 131 | + useListPageScope(scope); |
| 132 | + return <div ref={(el) => setScope(el?.parentElement ?? null)} data-testid="page-bar" />; |
| 133 | +}; |
| 134 | + |
| 135 | +function renderGrid( |
| 136 | + rows: Array<Record<string, unknown>> = [LOCKED_ROW, OPEN_ROW], |
| 137 | + { listPage = false }: { listPage?: boolean } = {}, |
| 138 | +) { |
| 139 | + const dataSource: any = { |
| 140 | + getObjectSchema: async (name: string) => ({ |
| 141 | + name, |
| 142 | + fields: { id: { type: 'text' }, name: { type: 'text', label: 'Name' }, approval_status: { type: 'text' } }, |
| 143 | + }), |
| 144 | + }; |
| 145 | + const schema: any = { |
| 146 | + type: 'object-grid', |
| 147 | + objectName: OBJECT, |
| 148 | + columns: [{ field: 'name', label: 'Name' }], |
| 149 | + data: { provider: 'value', items: rows }, |
| 150 | + pageSize: 500, |
| 151 | + ...(listPage ? { selection: { type: 'multiple' } } : {}), |
| 152 | + }; |
| 153 | + return render( |
| 154 | + <ActionProvider> |
| 155 | + <SchemaRendererProvider dataSource={dataSource} apiFetch={apiFetch}> |
| 156 | + <div data-testid="page-column"> |
| 157 | + {listPage && <PageBar />} |
| 158 | + <ObjectGrid schema={schema} dataSource={dataSource} onEdit={() => {}} onDelete={() => {}} /> |
| 159 | + </div> |
| 160 | + </SchemaRendererProvider> |
| 161 | + </ActionProvider>, |
| 162 | + ); |
| 163 | +} |
| 164 | + |
| 165 | +/** Reach for the first row's actions the way a pointer does: hover its "⋮". */ |
| 166 | +async function reachRowActions() { |
| 167 | + await userEvent.hover(screen.getAllByTestId('row-action-trigger')[0]); |
| 168 | +} |
| 169 | + |
| 170 | +async function settleReads() { |
| 171 | + await act(async () => { |
| 172 | + await Promise.allSettled(server.responses); |
| 173 | + }); |
| 174 | +} |
| 175 | + |
| 176 | +/** What the "⋮" of the row showing `label` offers. */ |
| 177 | +async function rowMenu(label: string): Promise<{ edit: boolean; delete: boolean }> { |
| 178 | + const row = screen.getByText(label).closest('tr'); |
| 179 | + const trigger = row?.querySelector('[data-testid="row-action-trigger"]'); |
| 180 | + if (!trigger) return { edit: false, delete: false }; |
| 181 | + await userEvent.click(trigger); |
| 182 | + const answer = { |
| 183 | + edit: screen.queryAllByTestId('row-action-builtin-edit').length > 0, |
| 184 | + delete: screen.queryAllByTestId('row-action-builtin-delete').length > 0, |
| 185 | + }; |
| 186 | + await userEvent.keyboard('{Escape}'); |
| 187 | + return answer; |
| 188 | +} |
| 189 | + |
| 190 | +beforeEach(() => { |
| 191 | + server.reset(); |
| 192 | + __clearRecordCrudVerdictCache(); |
| 193 | + __resetApprovalsServiceLatch(); |
| 194 | +}); |
| 195 | +afterEach(() => { |
| 196 | + cleanup(); |
| 197 | +}); |
| 198 | + |
| 199 | +describe('objectstack#36911 — the row menu follows the approval lock', () => { |
| 200 | + it('a row held by a locking approval offers no Edit and keeps Delete; its unlocked sibling keeps Edit', async () => { |
| 201 | + renderGrid(); |
| 202 | + await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument()); |
| 203 | + await reachRowActions(); |
| 204 | + await waitFor(() => expect(server.lockCalls.length).toBe(1)); |
| 205 | + await settleReads(); |
| 206 | + await waitFor(async () => expect(await rowMenu(LOCKED_ROW.name)).toEqual({ edit: false, delete: true })); |
| 207 | + expect(await rowMenu(OPEN_ROW.name)).toEqual({ edit: true, delete: true }); |
| 208 | + }); |
| 209 | + |
| 210 | + it('reads no lock before a row\'s actions are reached for, then the page in one read', async () => { |
| 211 | + renderGrid(); |
| 212 | + await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument()); |
| 213 | + await settleReads(); |
| 214 | + expect(server.lockCalls).toHaveLength(0); |
| 215 | + await reachRowActions(); |
| 216 | + await waitFor(() => expect(server.lockCalls).toHaveLength(1)); |
| 217 | + expect([...server.lockCalls[0].recordIds].sort()).toEqual([LOCKED_ROW.id, OPEN_ROW.id].sort()); |
| 218 | + // Reaching again, and opening the menus, asks nothing more. |
| 219 | + await settleReads(); |
| 220 | + await rowMenu(LOCKED_ROW.name); |
| 221 | + await rowMenu(OPEN_ROW.name); |
| 222 | + expect(server.lockCalls).toHaveLength(1); |
| 223 | + }); |
| 224 | + |
| 225 | + it('a 501 (no approvals service) leaves the menu as it was, even over an approval_status column', async () => { |
| 226 | + server.door = { kind: 'status', status: 501 }; |
| 227 | + renderGrid([{ ...LOCKED_ROW, approval_status: 'pending' }, OPEN_ROW]); |
| 228 | + await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument()); |
| 229 | + await reachRowActions(); |
| 230 | + await waitFor(() => expect(server.lockCalls).toHaveLength(1)); |
| 231 | + await settleReads(); |
| 232 | + expect(await rowMenu(LOCKED_ROW.name)).toEqual({ edit: true, delete: true }); |
| 233 | + expect(await rowMenu(OPEN_ROW.name)).toEqual({ edit: true, delete: true }); |
| 234 | + }); |
| 235 | + |
| 236 | + it('a null entry (a row the reader cannot read) leaves Edit where it was', async () => { |
| 237 | + server.door = { kind: 'answers', locked: { [LOCKED_ROW.id]: null, [OPEN_ROW.id]: false } }; |
| 238 | + renderGrid(); |
| 239 | + await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument()); |
| 240 | + await reachRowActions(); |
| 241 | + await waitFor(() => expect(server.lockCalls).toHaveLength(1)); |
| 242 | + await settleReads(); |
| 243 | + expect(await rowMenu(LOCKED_ROW.name)).toEqual({ edit: true, delete: true }); |
| 244 | + }); |
| 245 | + |
| 246 | + it('a read that fails, or that never answered, leaves Edit where it was', async () => { |
| 247 | + server.door = { kind: 'network' }; |
| 248 | + renderGrid(); |
| 249 | + await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument()); |
| 250 | + // Not asked yet: the menu is the one it always was. |
| 251 | + expect(server.lockCalls).toHaveLength(0); |
| 252 | + await reachRowActions(); |
| 253 | + await waitFor(() => expect(server.lockCalls).toHaveLength(1)); |
| 254 | + await settleReads(); |
| 255 | + expect(await rowMenu(LOCKED_ROW.name)).toEqual({ edit: true, delete: true }); |
| 256 | + }); |
| 257 | +}); |
| 258 | + |
| 259 | +/** |
| 260 | + * The list page draws no "⋮" column (objectstack#35554): one selected record's |
| 261 | + * actions are offered by the selection bar, which resolves them from the same |
| 262 | + * row menu props. Edit is a bar button there; Delete is a button or behind |
| 263 | + * 「More」. |
| 264 | + */ |
| 265 | +async function selectionBarOffers(label: string): Promise<{ edit: boolean; delete: boolean }> { |
| 266 | + const row = screen.getByText(label).closest('tr')!; |
| 267 | + fireEvent.click(within(row).getByRole('checkbox')); |
| 268 | + const bar = await screen.findByTestId('bulk-actions-bar'); |
| 269 | + await settleReads(); |
| 270 | + const offers = () => ({ |
| 271 | + edit: within(bar).queryByTestId('selection-action-edit') !== null |
| 272 | + || screen.queryAllByTestId('row-action-builtin-edit').length > 0, |
| 273 | + delete: within(bar).queryByTestId('selection-action-delete') !== null |
| 274 | + || screen.queryAllByTestId('row-action-builtin-delete').length > 0, |
| 275 | + }); |
| 276 | + const more = within(bar).queryByTestId('selection-actions-more'); |
| 277 | + if (more) await userEvent.click(more); |
| 278 | + const answer = offers(); |
| 279 | + if (more) await userEvent.keyboard('{Escape}'); |
| 280 | + fireEvent.click(within(row).getByRole('checkbox')); |
| 281 | + await waitFor(() => expect(screen.queryByTestId('bulk-actions-bar')).toBeNull()); |
| 282 | + return answer; |
| 283 | +} |
| 284 | + |
| 285 | +describe('objectstack#36911 — the list page selection bar follows the approval lock', () => { |
| 286 | + it('a selected locked row offers no Edit and keeps Delete; a selected unlocked sibling keeps Edit', async () => { |
| 287 | + const { container } = renderGrid([LOCKED_ROW, OPEN_ROW], { listPage: true }); |
| 288 | + await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument()); |
| 289 | + // The list page scope: no Actions column, so the bar is the row's actions. |
| 290 | + expect(container.querySelector('th[data-column-key="_actions"]')).toBeNull(); |
| 291 | + expect(server.lockCalls).toHaveLength(0); |
| 292 | + // CONTROL first: the unlocked row offers Edit through the bar, so the pin |
| 293 | + // below observes the lock, not a bar that never shows Edit. |
| 294 | + expect(await selectionBarOffers(OPEN_ROW.name)).toEqual({ edit: true, delete: true }); |
| 295 | + expect(server.lockCalls).toHaveLength(1); |
| 296 | + expect(await selectionBarOffers(LOCKED_ROW.name)).toEqual({ edit: false, delete: true }); |
| 297 | + expect(server.lockCalls).toHaveLength(1); |
| 298 | + }); |
| 299 | +}); |
| 300 | + |
| 301 | +describe('objectstack#36911 — the read in flight', () => { |
| 302 | + it('Edit shows while the lock read is on the wire, and leaves the open menu when it answers locked', async () => { |
| 303 | + server.hold(); |
| 304 | + renderGrid(); |
| 305 | + await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument()); |
| 306 | + const row = screen.getByText(LOCKED_ROW.name).closest('tr')!; |
| 307 | + await userEvent.click(row.querySelector('[data-testid="row-action-trigger"]')!); |
| 308 | + await waitFor(() => expect(server.lockCalls).toHaveLength(1)); |
| 309 | + // Unanswered: the menu is the one it always was. |
| 310 | + expect(screen.getAllByTestId('row-action-builtin-edit').length).toBeGreaterThan(0); |
| 311 | + expect(screen.getAllByTestId('row-action-builtin-delete').length).toBeGreaterThan(0); |
| 312 | + await act(async () => { |
| 313 | + server.release(); |
| 314 | + await Promise.allSettled(server.responses); |
| 315 | + }); |
| 316 | + await waitFor(() => expect(screen.queryAllByTestId('row-action-builtin-edit')).toHaveLength(0)); |
| 317 | + expect(screen.getAllByTestId('row-action-builtin-delete').length).toBeGreaterThan(0); |
| 318 | + }); |
| 319 | +}); |
0 commit comments