|
| 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#27138 — the console's edit dialog reads the approvals record |
| 11 | + * lock of the record it edits, through the read and the rule the record page |
| 12 | + * uses (`useRecordApprovals`, `openApprovalOf`, `resolveApprovalLock`), and |
| 13 | + * hands it to `ModalForm` as `lockedBy: 'approval'`. The record's field |
| 14 | + * projection leaves that lock out, so without it the dialog offered fields |
| 15 | + * a save then refused. |
| 16 | + * |
| 17 | + * Measured from a real `AppContent` mount (the harness of |
| 18 | + * `AppContent.formViewModalSize-12803`), with the approvals routes served by a |
| 19 | + * fetch double: |
| 20 | + * - a reader outside the slate: the list answers empty and the record lock |
| 21 | + * answers `locked: true` → `lockedBy: 'approval'`; |
| 22 | + * - a participant: the pending request's node locks (`lock_record` absent) |
| 23 | + * → `'approval'`; it declares `lockRecord: false` → no lock; |
| 24 | + * - CONTROL no approval running → no lock; |
| 25 | + * - CONTROL a create dialog asks nothing and is not locked. |
| 26 | + */ |
| 27 | + |
| 28 | +import '@testing-library/jest-dom/vitest'; |
| 29 | +import { describe, it, expect, vi, beforeEach, afterEach, afterAll } from 'vitest'; |
| 30 | +import React from 'react'; |
| 31 | +import { render, cleanup, waitFor } from '@testing-library/react'; |
| 32 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 33 | +import { registerAllFields } from '@object-ui/fields'; |
| 34 | + |
| 35 | +registerAllFields(); |
| 36 | + |
| 37 | +const probe = vi.hoisted(() => ({ |
| 38 | + /** Every set of props the global record-form modal was rendered with. */ |
| 39 | + modalProps: [] as any[], |
| 40 | +})); |
| 41 | + |
| 42 | +vi.mock('@object-ui/plugin-form', async (importOriginal) => { |
| 43 | + const actual = await importOriginal<Record<string, any>>(); |
| 44 | + return { |
| 45 | + ...actual, |
| 46 | + ModalForm: (props: any) => { |
| 47 | + probe.modalProps.push(props); |
| 48 | + return <div data-testid="modal-form" />; |
| 49 | + }, |
| 50 | + }; |
| 51 | +}); |
| 52 | + |
| 53 | +vi.mock('@object-ui/plugin-designer', async (importOriginal) => ({ |
| 54 | + ...(await importOriginal<typeof import('@object-ui/plugin-designer')>()), |
| 55 | + CreateAppPage: () => <div data-testid="create-app-page" />, |
| 56 | + EditAppPage: () => <div data-testid="edit-app-page" />, |
| 57 | + DashboardDesignPage: () => <div data-testid="dashboard-design-page" />, |
| 58 | +})); |
| 59 | + |
| 60 | +vi.mock('../../layout/ConsoleLayout', () => ({ |
| 61 | + ConsoleLayout: ({ children }: { children?: React.ReactNode }) => ( |
| 62 | + <div data-testid="console-layout">{children}</div> |
| 63 | + ), |
| 64 | +})); |
| 65 | +vi.mock('../../chrome/CommandPalette', () => ({ CommandPalette: () => null })); |
| 66 | +vi.mock('../../chrome/KeyboardShortcutsDialog', () => ({ KeyboardShortcutsDialog: () => null })); |
| 67 | +vi.mock('../../chrome/OnboardingWalkthrough', () => ({ OnboardingWalkthrough: () => null })); |
| 68 | +vi.mock('../../views/ObjectView', () => ({ ObjectView: () => <div data-testid="object-view" /> })); |
| 69 | +vi.mock('../../views/RecordFormPage', () => ({ |
| 70 | + RecordFormPage: () => <div data-testid="record-form-page" />, |
| 71 | +})); |
| 72 | + |
| 73 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 74 | + ...(await importOriginal<Record<string, unknown>>()), |
| 75 | + useAuth: () => ({ |
| 76 | + user: { id: 'u1', name: 'Ada', email: 'ada@example.com', role: 'user' }, |
| 77 | + getAuthConfig: async () => ({ features: {} }), |
| 78 | + activeOrganization: null, |
| 79 | + }), |
| 80 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 81 | +})); |
| 82 | + |
| 83 | +const CONTRACT = { |
| 84 | + name: 'crm_contract', |
| 85 | + label: 'Contract', |
| 86 | + fields: { title: { type: 'text', label: 'Title' } }, |
| 87 | + form: { type: 'modal' }, |
| 88 | +}; |
| 89 | +const OBJECTS = [CONTRACT]; |
| 90 | + |
| 91 | +const dataSourceStub = { |
| 92 | + onConnectionStateChange: () => () => {}, |
| 93 | + getConnectionState: () => 'connected', |
| 94 | + getObjectSchema: vi.fn(async (name: string) => OBJECTS.find((o) => o.name === name)), |
| 95 | + findOne: vi.fn(async () => ({ id: 'r1' })), |
| 96 | + find: vi.fn(async () => ({ data: [] })), |
| 97 | + create: vi.fn(), |
| 98 | + update: vi.fn(), |
| 99 | +}; |
| 100 | +vi.mock('../../providers/AdapterProvider', async (importOriginal) => ({ |
| 101 | + ...(await importOriginal<Record<string, unknown>>()), |
| 102 | + useAdapter: () => dataSourceStub, |
| 103 | +})); |
| 104 | + |
| 105 | +const APPS = [{ name: 'crm', label: 'CRM', isDefault: true, navigation: [] }]; |
| 106 | + |
| 107 | +// One value for the whole run: effects key on these members. |
| 108 | +const METADATA = { |
| 109 | + apps: APPS, |
| 110 | + objects: OBJECTS, |
| 111 | + loading: false, |
| 112 | + ensureType: undefined, |
| 113 | + error: null, |
| 114 | + refresh: async () => {}, |
| 115 | +}; |
| 116 | +vi.mock('../../providers/MetadataProvider', async (importOriginal) => ({ |
| 117 | + ...(await importOriginal<Record<string, unknown>>()), |
| 118 | + useMetadata: () => METADATA, |
| 119 | +})); |
| 120 | + |
| 121 | +const ACTION_RUNNER = { execute: () => {}, runner: { registerHandler: () => {}, getContext: () => ({}) } }; |
| 122 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 123 | + ...(await importOriginal<Record<string, unknown>>()), |
| 124 | + useActionRunner: () => ACTION_RUNNER, |
| 125 | + useGlobalUndo: () => {}, |
| 126 | + useMutationInvalidationBridge: () => {}, |
| 127 | +})); |
| 128 | + |
| 129 | +import { AppContent } from '../AppContent'; |
| 130 | +import { __resetRecordApprovalsStore } from '../../hooks/useRecordApprovals'; |
| 131 | + |
| 132 | +type Approvals = { requests: any[]; lock: boolean }; |
| 133 | + |
| 134 | +/** What the approvals routes answer; set per test. */ |
| 135 | +let served: Approvals = { requests: [], lock: false }; |
| 136 | + |
| 137 | +// Installed once for the whole file, so no read the dialog issues after a |
| 138 | +// test body returns can reach a socket; handed back in `afterAll`. |
| 139 | +const fetchSpy = vi.fn(async (input: RequestInfo | URL) => { |
| 140 | + const url = String(input); |
| 141 | + const body = url.includes('/approvals/records/') |
| 142 | + ? { success: true, data: { locked: served.lock } } |
| 143 | + : url.includes('/approvals/requests?') |
| 144 | + ? { data: served.requests } |
| 145 | + : url.includes('/approvals/requests/') |
| 146 | + ? { data: served.requests[0] } |
| 147 | + : {}; |
| 148 | + return new Response(JSON.stringify(body), { status: 200, headers: { 'Content-Type': 'application/json' } }); |
| 149 | +}); |
| 150 | +vi.stubGlobal('fetch', fetchSpy); |
| 151 | +afterAll(() => vi.unstubAllGlobals()); |
| 152 | + |
| 153 | +function serveApprovals(approvals: Approvals) { |
| 154 | + served = approvals; |
| 155 | + return fetchSpy; |
| 156 | +} |
| 157 | + |
| 158 | +const approvalReads = (spy: typeof fetchSpy) => |
| 159 | + spy.mock.calls.map(([u]) => String(u)).filter((u) => u.includes('/approvals/')); |
| 160 | + |
| 161 | +function openAt(url: string) { |
| 162 | + render( |
| 163 | + <MemoryRouter initialEntries={[url]}> |
| 164 | + <Routes> |
| 165 | + <Route path="/apps/:appName/*" element={<AppContent />} /> |
| 166 | + </Routes> |
| 167 | + </MemoryRouter>, |
| 168 | + ); |
| 169 | +} |
| 170 | + |
| 171 | +const lastModal = () => probe.modalProps[probe.modalProps.length - 1]; |
| 172 | + |
| 173 | +const PENDING = { id: 'req1', object_name: 'crm_contract', record_id: 'r1', status: 'pending' }; |
| 174 | + |
| 175 | +beforeEach(() => { |
| 176 | + probe.modalProps.length = 0; |
| 177 | + __resetRecordApprovalsStore(); |
| 178 | + vi.clearAllMocks(); |
| 179 | +}); |
| 180 | +afterEach(cleanup); |
| 181 | + |
| 182 | +describe('[#27138] the console edit dialog reads the approvals record lock', () => { |
| 183 | + it.each([ |
| 184 | + ['a reader outside the slate (the record lock answers)', { requests: [], lock: true }], |
| 185 | + ['a participant whose pending node locks the record', { requests: [PENDING], lock: false }], |
| 186 | + ])('%s: the dialog is handed the approval lock', async (_label, approvals) => { |
| 187 | + const fetchSpy = serveApprovals(approvals); |
| 188 | + openAt('/apps/crm/crm_contract?form=r1'); |
| 189 | + await waitFor(() => expect(lastModal()?.schema?.mode).toBe('edit')); |
| 190 | + await waitFor(() => expect(lastModal().lockedBy).toBe('approval')); |
| 191 | + expect(approvalReads(fetchSpy).some((u) => u.includes('object=crm_contract') && u.includes('recordId=r1'))).toBe(true); |
| 192 | + }); |
| 193 | + |
| 194 | + it.each([ |
| 195 | + ['a pending node that declares lockRecord: false', { requests: [{ ...PENDING, lock_record: false }], lock: false }], |
| 196 | + ['CONTROL — no approval running', { requests: [], lock: false }], |
| 197 | + ])('%s: the dialog is not locked', async (_label, approvals) => { |
| 198 | + const fetchSpy = serveApprovals(approvals); |
| 199 | + openAt('/apps/crm/crm_contract?form=r1'); |
| 200 | + await waitFor(() => expect(lastModal()?.schema?.mode).toBe('edit')); |
| 201 | + await waitFor(() => expect(approvalReads(fetchSpy).length).toBeGreaterThan(0)); |
| 202 | + await new Promise((r) => setTimeout(r, 20)); |
| 203 | + expect(lastModal().lockedBy).toBeUndefined(); |
| 204 | + }); |
| 205 | + |
| 206 | + it('CONTROL — a create dialog asks nothing and is not locked', async () => { |
| 207 | + const fetchSpy = serveApprovals({ requests: [], lock: true }); |
| 208 | + openAt('/apps/crm/crm_contract?form=new'); |
| 209 | + await waitFor(() => expect(lastModal()?.schema?.mode).toBe('create')); |
| 210 | + await new Promise((r) => setTimeout(r, 20)); |
| 211 | + expect(lastModal().lockedBy).toBeUndefined(); |
| 212 | + expect(approvalReads(fetchSpy)).toEqual([]); |
| 213 | + }); |
| 214 | +}); |
0 commit comments