|
| 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 | + * `record:path` with `allowStageChange: true` (objectstack#28061, console half |
| 9 | + * objectstack#28199): a stage press asks for confirmation, then writes the |
| 10 | + * status field through the record update with the record's concurrency token; |
| 11 | + * a server refusal is shown in place; a reader who may not edit the status |
| 12 | + * field — and every path that does not opt in — keeps the readout. |
| 13 | + * |
| 14 | + * The subject is imported relatively and `@object-ui/*` resolves to source |
| 15 | + * through the root vitest alias table, so nothing here reads a `dist/`. |
| 16 | + */ |
| 17 | + |
| 18 | +import * as React from 'react'; |
| 19 | +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; |
| 20 | +import { render, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react'; |
| 21 | +import '@testing-library/jest-dom'; |
| 22 | +import { InlineEditProvider, RecordContextProvider, useInlineEdit } from '@object-ui/react'; |
| 23 | + |
| 24 | +const { permsStub, perms } = vi.hoisted(() => { |
| 25 | + const perms: { isLoaded: boolean; writeDenied: string[] } = { isLoaded: true, writeDenied: [] }; |
| 26 | + return { |
| 27 | + perms, |
| 28 | + permsStub: { |
| 29 | + get isLoaded() { return perms.isLoaded; }, |
| 30 | + checkField: (_object: string, field: string, action: string) => |
| 31 | + action === 'read' ? true : !perms.writeDenied.includes(field), |
| 32 | + check: () => ({ allowed: true }), |
| 33 | + getFieldPermissions: () => [], |
| 34 | + getRowFilter: () => undefined, |
| 35 | + getObjectApiOperations: () => undefined, |
| 36 | + roles: [], |
| 37 | + userId: null, |
| 38 | + systemPermissions: undefined, |
| 39 | + hasCapabilities: () => true, |
| 40 | + can: () => true, |
| 41 | + cannot: () => false, |
| 42 | + }, |
| 43 | + }; |
| 44 | +}); |
| 45 | + |
| 46 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 47 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 48 | + return { ...actual, usePermissions: () => permsStub as any }; |
| 49 | +}); |
| 50 | + |
| 51 | +import { RecordPathRenderer } from '../record-path'; |
| 52 | + |
| 53 | +const STAGES = [ |
| 54 | + { value: 'draft', label: 'Draft' }, |
| 55 | + { value: 'in_review', label: 'In review' }, |
| 56 | + { value: 'accepted', label: 'Accepted' }, |
| 57 | + { value: 'declined', label: 'Declined', terminal: 'lost' as const }, |
| 58 | +]; |
| 59 | + |
| 60 | +const OBJECT_SCHEMA = { |
| 61 | + name: 'crm_quote', |
| 62 | + fields: { |
| 63 | + status: { type: 'select', label: 'Status' }, |
| 64 | + amount: { type: 'currency', label: 'Amount' }, |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +const VERSION = '2026-10-30T12:00:00.000Z'; |
| 69 | + |
| 70 | +function makeDataSource(update: (...args: any[]) => Promise<unknown> = async () => ({})) { |
| 71 | + return { update: vi.fn(update) }; |
| 72 | +} |
| 73 | + |
| 74 | +interface MountOptions { |
| 75 | + allowStageChange?: boolean; |
| 76 | + canEdit?: boolean; |
| 77 | + dataSource?: ReturnType<typeof makeDataSource>; |
| 78 | + refresh?: () => Promise<void>; |
| 79 | + objectSchema?: Record<string, unknown>; |
| 80 | + editing?: boolean; |
| 81 | +} |
| 82 | + |
| 83 | +function EnterEditing() { |
| 84 | + // Puts the page's edit session into editing, the way a field pencil does. |
| 85 | + const inline = useInlineEdit(); |
| 86 | + const enter = inline?.enter; |
| 87 | + React.useEffect(() => { enter?.(); }, [enter]); |
| 88 | + return null; |
| 89 | +} |
| 90 | + |
| 91 | +function mount(opts: MountOptions = {}) { |
| 92 | + const dataSource = opts.dataSource ?? makeDataSource(); |
| 93 | + const refresh = opts.refresh ?? vi.fn(async () => {}); |
| 94 | + const schema: Record<string, unknown> = { statusField: 'status', stages: STAGES }; |
| 95 | + if (opts.allowStageChange !== undefined) schema.allowStageChange = opts.allowStageChange; |
| 96 | + const r = render( |
| 97 | + <RecordContextProvider |
| 98 | + objectName="crm_quote" |
| 99 | + recordId="q1" |
| 100 | + data={{ id: 'q1', status: 'draft', updated_at: VERSION }} |
| 101 | + objectSchema={opts.objectSchema ?? OBJECT_SCHEMA} |
| 102 | + dataSource={dataSource as any} |
| 103 | + refresh={refresh} |
| 104 | + > |
| 105 | + <InlineEditProvider canEdit={opts.canEdit ?? true}> |
| 106 | + {opts.editing && <EnterEditing />} |
| 107 | + <RecordPathRenderer schema={schema as never} /> |
| 108 | + </InlineEditProvider> |
| 109 | + </RecordContextProvider>, |
| 110 | + ); |
| 111 | + return { ...r, dataSource, refresh }; |
| 112 | +} |
| 113 | + |
| 114 | +/** Both rows (desktop + phone) are in the DOM; the first is the desktop rail. */ |
| 115 | +const rows = (container: HTMLElement): HTMLElement[] => |
| 116 | + Array.from(container.querySelectorAll('[role="list"]')) as HTMLElement[]; |
| 117 | + |
| 118 | +async function pressAndConfirm(container: HTMLElement, stageLabel: string) { |
| 119 | + fireEvent.click(within(rows(container)[0]).getByRole('button', { name: `Move to ${stageLabel}` })); |
| 120 | + const dialog = await screen.findByRole('alertdialog'); |
| 121 | + expect(within(dialog).getByText(`Move this record to ${stageLabel}?`)).toBeInTheDocument(); |
| 122 | + fireEvent.click(within(dialog).getByRole('button', { name: 'Move' })); |
| 123 | +} |
| 124 | + |
| 125 | +beforeEach(() => { |
| 126 | + perms.isLoaded = true; |
| 127 | + perms.writeDenied = []; |
| 128 | +}); |
| 129 | + |
| 130 | +afterEach(() => cleanup()); |
| 131 | + |
| 132 | +describe('record:path allowStageChange — a press writes the stage', () => { |
| 133 | + it('every stage but the current one is a button, on both rows', () => { |
| 134 | + const { container } = mount({ allowStageChange: true }); |
| 135 | + for (const row of rows(container)) { |
| 136 | + const names = within(row).getAllByRole('button').map((b) => b.getAttribute('aria-label')); |
| 137 | + expect(names).toEqual(['Move to In review', 'Move to Accepted', 'Move to Declined']); |
| 138 | + // The listitem semantics and the state names stay as they were. |
| 139 | + expect(row.querySelector('[aria-current="step"]')).toHaveAttribute('aria-label', 'Draft, current stage'); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + it('confirming writes { statusField: value } through update with the record version, then re-reads', async () => { |
| 144 | + const { container, dataSource, refresh } = mount({ allowStageChange: true }); |
| 145 | + await pressAndConfirm(container, 'In review'); |
| 146 | + await waitFor(() => expect(dataSource.update).toHaveBeenCalledTimes(1)); |
| 147 | + expect(dataSource.update).toHaveBeenCalledWith('crm_quote', 'q1', { status: 'in_review' }, { ifMatch: VERSION }); |
| 148 | + await waitFor(() => expect(refresh).toHaveBeenCalledTimes(1)); |
| 149 | + expect(container.querySelector('[data-stage-change-refusal]')).toBeNull(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('cancelling the confirmation writes nothing', async () => { |
| 153 | + const { container, dataSource } = mount({ allowStageChange: true }); |
| 154 | + fireEvent.click(within(rows(container)[0]).getByRole('button', { name: 'Move to Accepted' })); |
| 155 | + const dialog = await screen.findByRole('alertdialog'); |
| 156 | + fireEvent.click(within(dialog).getByRole('button', { name: 'Cancel' })); |
| 157 | + await waitFor(() => expect(screen.queryByRole('alertdialog')).toBeNull()); |
| 158 | + expect(dataSource.update).not.toHaveBeenCalled(); |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +describe('record:path allowStageChange — a refusal renders in place', () => { |
| 163 | + it('an illegal state_machine transition shows the server\'s sentence under the path', async () => { |
| 164 | + const refusal = Object.assign(new Error('VALIDATION_FAILED'), { |
| 165 | + code: 'VALIDATION_FAILED', |
| 166 | + fields: [{ field: 'status', code: 'state_machine', message: 'A quote cannot go from Draft to Accepted.' }], |
| 167 | + }); |
| 168 | + const { container, refresh } = mount({ |
| 169 | + allowStageChange: true, |
| 170 | + dataSource: makeDataSource(async () => { throw refusal; }), |
| 171 | + }); |
| 172 | + await pressAndConfirm(container, 'Accepted'); |
| 173 | + const alert = await within(container).findByRole('alert'); |
| 174 | + expect(alert).toHaveAttribute('data-stage-change-refusal'); |
| 175 | + expect(within(alert).getAllByRole('listitem').map((li) => li.textContent)).toEqual([ |
| 176 | + 'A quote cannot go from Draft to Accepted.', |
| 177 | + ]); |
| 178 | + expect(refresh).not.toHaveBeenCalled(); |
| 179 | + }); |
| 180 | + |
| 181 | + it('a required field elsewhere on the record is named by its label', async () => { |
| 182 | + const refusal = Object.assign(new Error('VALIDATION_FAILED'), { |
| 183 | + code: 'VALIDATION_FAILED', |
| 184 | + fields: [{ field: 'amount', code: 'required', message: 'is required to accept a quote' }], |
| 185 | + }); |
| 186 | + const { container } = mount({ |
| 187 | + allowStageChange: true, |
| 188 | + dataSource: makeDataSource(async () => { throw refusal; }), |
| 189 | + }); |
| 190 | + await pressAndConfirm(container, 'Accepted'); |
| 191 | + const alert = await within(container).findByRole('alert'); |
| 192 | + expect(alert).toHaveTextContent('Amount: is required to accept a quote'); |
| 193 | + }); |
| 194 | + |
| 195 | + it('a permission refusal reads as a fixed line, not the raw 403 text', async () => { |
| 196 | + const refusal = Object.assign(new Error('[Security] PERMISSION_DENIED: no update on crm_quote'), { |
| 197 | + httpStatus: 403, |
| 198 | + code: 'PERMISSION_DENIED', |
| 199 | + }); |
| 200 | + const { container } = mount({ |
| 201 | + allowStageChange: true, |
| 202 | + dataSource: makeDataSource(async () => { throw refusal; }), |
| 203 | + }); |
| 204 | + await pressAndConfirm(container, 'In review'); |
| 205 | + const alert = await within(container).findByRole('alert'); |
| 206 | + expect(alert).toHaveTextContent("You don't have permission to change this record's stage."); |
| 207 | + }); |
| 208 | + |
| 209 | + it('a stale concurrency token says so and re-reads the record', async () => { |
| 210 | + const refusal = Object.assign(new Error('CONCURRENT_UPDATE'), { code: 'CONCURRENT_UPDATE' }); |
| 211 | + const { container, refresh } = mount({ |
| 212 | + allowStageChange: true, |
| 213 | + dataSource: makeDataSource(async () => { throw refusal; }), |
| 214 | + }); |
| 215 | + await pressAndConfirm(container, 'In review'); |
| 216 | + const alert = await within(container).findByRole('alert'); |
| 217 | + expect(alert).toHaveTextContent('Someone else changed this record'); |
| 218 | + await waitFor(() => expect(refresh).toHaveBeenCalledTimes(1)); |
| 219 | + }); |
| 220 | + |
| 221 | + it('the next press clears the last refusal', async () => { |
| 222 | + const refusal = Object.assign(new Error('nope'), { code: 'VALIDATION_FAILED', fields: [{ field: 'status', message: 'Not from here.' }] }); |
| 223 | + const { container } = mount({ |
| 224 | + allowStageChange: true, |
| 225 | + dataSource: makeDataSource(async () => { throw refusal; }), |
| 226 | + }); |
| 227 | + await pressAndConfirm(container, 'Accepted'); |
| 228 | + await within(container).findByRole('alert'); |
| 229 | + fireEvent.click(within(rows(container)[0]).getByRole('button', { name: 'Move to In review' })); |
| 230 | + expect(within(container).queryByRole('alert')).toBeNull(); |
| 231 | + }); |
| 232 | +}); |
| 233 | + |
| 234 | +describe('record:path allowStageChange — who sees the readout', () => { |
| 235 | + const noButtons = (container: HTMLElement) => { |
| 236 | + for (const row of rows(container)) { |
| 237 | + expect(within(row).queryAllByRole('button')).toHaveLength(0); |
| 238 | + expect(row.querySelectorAll('button,[tabindex]')).toHaveLength(0); |
| 239 | + } |
| 240 | + }; |
| 241 | + |
| 242 | + it('control: the opt-in absent keeps the readout, for a reader who could edit', () => { |
| 243 | + const { container } = mount({}); |
| 244 | + expect(rows(container)).toHaveLength(2); |
| 245 | + noButtons(container); |
| 246 | + }); |
| 247 | + |
| 248 | + it('control: the opt-in false keeps the readout', () => { |
| 249 | + noButtons(mount({ allowStageChange: false }).container); |
| 250 | + }); |
| 251 | + |
| 252 | + it('a reader the page may not let edit this record sees the readout', () => { |
| 253 | + noButtons(mount({ allowStageChange: true, canEdit: false }).container); |
| 254 | + }); |
| 255 | + |
| 256 | + it('a reader without the edit grant on the status field sees the readout', () => { |
| 257 | + perms.writeDenied = ['status']; |
| 258 | + noButtons(mount({ allowStageChange: true }).container); |
| 259 | + }); |
| 260 | + |
| 261 | + it('a read-only status field is never pressable', () => { |
| 262 | + noButtons( |
| 263 | + mount({ |
| 264 | + allowStageChange: true, |
| 265 | + objectSchema: { name: 'crm_quote', fields: { status: { type: 'select', readonly: true } } }, |
| 266 | + }).container, |
| 267 | + ); |
| 268 | + }); |
| 269 | + |
| 270 | + it('while the record\'s inline edit holds a draft, the path is a readout', async () => { |
| 271 | + const { container } = mount({ allowStageChange: true, editing: true }); |
| 272 | + await waitFor(() => noButtons(container)); |
| 273 | + }); |
| 274 | +}); |
0 commit comments