|
| 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#27146 (the residual of objectstack#26594, ruling B) — the record |
| 11 | + * page's in-place edit asks the server for THIS record's field projection, the |
| 12 | + * answer the record form's edit dialog already reads. |
| 13 | + * |
| 14 | + * The persona is the contract app's finance role: `summary` is locked from |
| 15 | + * `approved` onward (`FieldPermissionSchema.when`). `/auth/me/permissions` |
| 16 | + * holds no row, so it serves that entry as its lock, and before this card the |
| 17 | + * details body and the highlights strip disabled `summary` on the draft the |
| 18 | + * server lets the caller save. `ref_no` is locked on every row: the control |
| 19 | + * that a lock the server keeps stays locked. |
| 20 | + * |
| 21 | + * Rows, for `record:details` and for `record:highlights`, each under the record |
| 22 | + * page's providers (record context, inline-edit session): |
| 23 | + * - a draft the server unlocks → `summary` edits in place, `ref_no` stays a |
| 24 | + * read display; |
| 25 | + * - an approved row → both stay read displays; |
| 26 | + * - a save after which the server judges the row approved → asked again, |
| 27 | + * and `summary` locks (the answer follows the stored row, not the mount); |
| 28 | + * and the controls: |
| 29 | + * - a data source without the method keeps the row-blind lock; |
| 30 | + * - a session that may not edit asks nothing. |
| 31 | + */ |
| 32 | +import * as React from 'react'; |
| 33 | +import { describe, it, expect, vi, beforeAll, beforeEach, afterEach } from 'vitest'; |
| 34 | +import { render, screen, waitFor, act, cleanup, fireEvent } from '@testing-library/react'; |
| 35 | +import '@testing-library/jest-dom'; |
| 36 | +import { HighlightFieldsProvider, InlineEditProvider, RecordContextProvider, useInlineEdit } from '@object-ui/react'; |
| 37 | +import { MePermissionsProvider, type MePermissionsResponse } from '@object-ui/permissions'; |
| 38 | +import { RecordDetailsRenderer } from '../record-details'; |
| 39 | +import { RecordHighlightsRenderer } from '../record-highlights'; |
| 40 | + |
| 41 | +const OBJECT = 'rfa_contract'; |
| 42 | +const FIELDS = ['region', 'summary', 'ref_no']; |
| 43 | + |
| 44 | +const OBJECT_SCHEMA = { |
| 45 | + name: OBJECT, |
| 46 | + label: 'Contract', |
| 47 | + // The title field the details body draws in the header, not as a row. |
| 48 | + nameField: 'name', |
| 49 | + highlightFields: FIELDS, |
| 50 | + fields: { |
| 51 | + name: { type: 'text', label: 'Name' }, |
| 52 | + region: { type: 'text', label: 'Region' }, |
| 53 | + summary: { type: 'text', label: 'Summary' }, |
| 54 | + ref_no: { type: 'text', label: 'Reference number' }, |
| 55 | + }, |
| 56 | +}; |
| 57 | + |
| 58 | +const PRINCIPAL: MePermissionsResponse = { |
| 59 | + authenticated: true, |
| 60 | + userId: 'u-1', |
| 61 | + tenantId: null, |
| 62 | + roles: ['finance'], |
| 63 | + permissionSets: ['rfa_fixture_member'], |
| 64 | + objects: { [OBJECT]: { allowCreate: true, allowRead: true, allowEdit: true, allowDelete: false } }, |
| 65 | + // As `/auth/me/permissions` serves them: the conditioned entry as its lock. |
| 66 | + fields: { |
| 67 | + [`${OBJECT}.summary`]: { readable: true, editable: false }, |
| 68 | + [`${OBJECT}.ref_no`]: { readable: true, editable: false }, |
| 69 | + }, |
| 70 | +} as MePermissionsResponse; |
| 71 | + |
| 72 | +const RECORD = { id: 'c1', name: 'MSA', region: 'EMEA', summary: 'Draft terms', ref_no: 'R-1' }; |
| 73 | + |
| 74 | +const ACCESS = { |
| 75 | + draft: { object: OBJECT, recordId: 'c1', readable: ['id', ...FIELDS], editable: ['region', 'summary'] }, |
| 76 | + approved: { object: OBJECT, recordId: 'c1', readable: ['id', ...FIELDS], editable: ['region'] }, |
| 77 | +}; |
| 78 | + |
| 79 | +function makeDS(access: (typeof ACCESS)[keyof typeof ACCESS], withMethod = true) { |
| 80 | + const listeners = new Set<(e: any) => void>(); |
| 81 | + return { |
| 82 | + getObjectSchema: vi.fn(async () => OBJECT_SCHEMA), |
| 83 | + findOne: vi.fn(async () => ({ ...RECORD })), |
| 84 | + find: vi.fn(async () => ({ data: [] })), |
| 85 | + update: vi.fn(async (resource: string, id: string, data: any) => { |
| 86 | + const record = { ...RECORD, ...data }; |
| 87 | + // As the adapter does: every successful write is reported to `onMutation` subscribers. |
| 88 | + for (const listener of listeners) listener({ type: 'update', resource, id, record }); |
| 89 | + return record; |
| 90 | + }), |
| 91 | + onMutation: (listener: (e: any) => void) => { |
| 92 | + listeners.add(listener); |
| 93 | + return () => listeners.delete(listener); |
| 94 | + }, |
| 95 | + ...(withMethod ? { getRecordFieldAccess: vi.fn(async () => access) } : {}), |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +/** Opens the page's inline-edit session, as the pencil does. */ |
| 100 | +function EnterEdit() { |
| 101 | + const inline = useInlineEdit(); |
| 102 | + return <button type="button" onClick={() => inline?.enter()}>enter-edit</button>; |
| 103 | +} |
| 104 | + |
| 105 | +const BLOCKS = [ |
| 106 | + { block: 'record:details', mount: () => <RecordDetailsRenderer schema={{} as never} /> }, |
| 107 | + { block: 'record:highlights', mount: () => <RecordHighlightsRenderer schema={{ fields: FIELDS } as never} /> }, |
| 108 | +]; |
| 109 | + |
| 110 | +function renderPage(ds: ReturnType<typeof makeDS>, block: React.ReactNode, canEdit = true) { |
| 111 | + return render( |
| 112 | + <MePermissionsProvider initialPermissions={PRINCIPAL}> |
| 113 | + <RecordContextProvider |
| 114 | + objectName={OBJECT} |
| 115 | + recordId={RECORD.id} |
| 116 | + data={RECORD} |
| 117 | + objectSchema={OBJECT_SCHEMA} |
| 118 | + dataSource={ds as never} |
| 119 | + > |
| 120 | + <HighlightFieldsProvider> |
| 121 | + <InlineEditProvider canEdit={canEdit}> |
| 122 | + <EnterEdit /> |
| 123 | + {block} |
| 124 | + </InlineEditProvider> |
| 125 | + </HighlightFieldsProvider> |
| 126 | + </RecordContextProvider> |
| 127 | + </MePermissionsProvider>, |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +/** With the session open: which fields draw an input (edit in place) rather than their read display. */ |
| 132 | +function editable(): Record<string, boolean> { |
| 133 | + return { |
| 134 | + region: screen.queryByDisplayValue(RECORD.region) !== null, |
| 135 | + summary: screen.queryByDisplayValue(RECORD.summary) !== null, |
| 136 | + ref_no: screen.queryByDisplayValue(RECORD.ref_no) !== null, |
| 137 | + }; |
| 138 | +} |
| 139 | + |
| 140 | +async function openSession() { |
| 141 | + await screen.findByText(RECORD.summary); |
| 142 | + fireEvent.click(screen.getByText('enter-edit')); |
| 143 | + await waitFor(() => expect(screen.getByDisplayValue(RECORD.region)).toBeInTheDocument()); |
| 144 | +} |
| 145 | + |
| 146 | +beforeAll(() => Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 })); |
| 147 | + |
| 148 | +beforeEach(() => { |
| 149 | + // `useRecordEditable` probes `POST /api/v1/security/explain`; serve it from a |
| 150 | + // double so no real socket opens. Its answer is orthogonal to the field locks. |
| 151 | + vi.stubGlobal('fetch', vi.fn(async () => ({ |
| 152 | + ok: true, |
| 153 | + status: 200, |
| 154 | + json: async () => ({ allowed: true }), |
| 155 | + text: async () => '{"allowed":true}', |
| 156 | + })) as never); |
| 157 | +}); |
| 158 | + |
| 159 | +afterEach(() => { |
| 160 | + cleanup(); |
| 161 | + vi.unstubAllGlobals(); |
| 162 | +}); |
| 163 | + |
| 164 | +describe.each(BLOCKS)('[#27146] $block', ({ mount }) => { |
| 165 | + it('on a draft the server unlocks, the conditioned field edits in place and the unconditioned lock holds', async () => { |
| 166 | + const ds = makeDS(ACCESS.draft); |
| 167 | + renderPage(ds, mount()); |
| 168 | + await openSession(); |
| 169 | + await waitFor(() => expect(editable()).toEqual({ region: true, summary: true, ref_no: false })); |
| 170 | + expect(ds.getRecordFieldAccess).toHaveBeenCalledWith(OBJECT, 'c1'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('on a row the condition names, the field stays locked', async () => { |
| 174 | + const ds = makeDS(ACCESS.approved); |
| 175 | + renderPage(ds, mount()); |
| 176 | + await openSession(); |
| 177 | + await waitFor(() => expect(ds.getRecordFieldAccess).toHaveBeenCalled()); |
| 178 | + await act(async () => { await new Promise((r) => setTimeout(r, 0)); }); |
| 179 | + expect(editable()).toEqual({ region: true, summary: false, ref_no: false }); |
| 180 | + }); |
| 181 | + |
| 182 | + it('a save that moves the row into the state the condition names locks the field again', async () => { |
| 183 | + const ds = makeDS(ACCESS.draft); |
| 184 | + renderPage(ds, mount()); |
| 185 | + await openSession(); |
| 186 | + await waitFor(() => expect(editable().summary).toBe(true)); |
| 187 | + // The server now judges the stored row approved: the next answer locks `summary`. |
| 188 | + ds.getRecordFieldAccess!.mockResolvedValue(ACCESS.approved); |
| 189 | + await act(async () => { await ds.update(OBJECT, 'c1', { region: 'APAC' }); }); |
| 190 | + await waitFor(() => expect(ds.getRecordFieldAccess).toHaveBeenCalledTimes(2)); |
| 191 | + await waitFor(() => expect(editable().summary).toBe(false)); |
| 192 | + expect(editable().ref_no).toBe(false); |
| 193 | + }); |
| 194 | + |
| 195 | + it('a data source without the method keeps the row-blind lock', async () => { |
| 196 | + const ds = makeDS(ACCESS.draft, false); |
| 197 | + renderPage(ds, mount()); |
| 198 | + await openSession(); |
| 199 | + expect(editable()).toEqual({ region: true, summary: false, ref_no: false }); |
| 200 | + }); |
| 201 | + |
| 202 | + it('a session that may not edit asks nothing', async () => { |
| 203 | + const ds = makeDS(ACCESS.draft); |
| 204 | + renderPage(ds, mount(), false); |
| 205 | + await screen.findByText(RECORD.summary); |
| 206 | + await act(async () => { await new Promise((r) => setTimeout(r, 0)); }); |
| 207 | + expect(ds.getRecordFieldAccess).not.toHaveBeenCalled(); |
| 208 | + }); |
| 209 | +}); |
0 commit comments