|
| 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 | + * objectui#7516 — the object page relays a per-view `fieldOrder`. |
| 11 | + * |
| 12 | + * ## The defect this pins |
| 13 | + * |
| 14 | + * `renderListView` builds `fullSchema` by spreading the host's `listSchema` |
| 15 | + * and then relaying keys off the active `viewDef`. `hiddenFields` had a rung; |
| 16 | + * `fieldOrder`, the other per-view half of the same field composition, had |
| 17 | + * NONE. So `schema.fieldOrder` at the `ListView` end could only ever be what |
| 18 | + * the host put on the list schema, and a per-view order was unreachable — |
| 19 | + * declared by the protocol, accepted and served, then dropped here. The |
| 20 | + * relay-rung census (`ObjectView.relayRungCensus-7559.test.ts`) carried it as a |
| 21 | + * `known-gap` until this card. |
| 22 | + * |
| 23 | + * ## What the rung carries, and what it does not decide |
| 24 | + * |
| 25 | + * objectstack#15184 ruling B kept `fieldOrder` and wrote the composition into |
| 26 | + * the contract: `columns` projects, `hiddenFields` subtracts from the |
| 27 | + * projection, `fieldOrder` sorts what survives (a surviving column it does not |
| 28 | + * list sorts last, keeping its `columns`-relative order). `ListView`'s |
| 29 | + * `effectiveFields` memo already runs those steps in that order on whatever |
| 30 | + * `schema.fieldOrder` it receives. The rung therefore only DELIVERS the view's |
| 31 | + * value into that slot — the same slot the list-node spelling fills — so the |
| 32 | + * two spellings compose identically by construction. Where both are present, |
| 33 | + * the view wins, the precedence the `hiddenFields` rung beside it uses. |
| 34 | + * |
| 35 | + * ## The value DOES arrive here — the relay is where it died |
| 36 | + * |
| 37 | + * `buildViewTabs` composes each entry through `viewEntry`, which is |
| 38 | + * `Object.assign` over the authored body and stamps only `id` afterwards, so an |
| 39 | + * authored `fieldOrder` is present on `viewDef`. The in-tree host |
| 40 | + * (`plugin-view`'s `ObjectView`) fills its own `fieldOrder` slot from a NAMED |
| 41 | + * list view only, and this page hands it no `listViews`, so on this route the |
| 42 | + * spread carries nothing for the key. |
| 43 | + * |
| 44 | + * ## Why the schema is captured rather than rendered |
| 45 | + * |
| 46 | + * The claim is about what THIS file hands down, so `ListView` is stubbed and |
| 47 | + * its `schema` prop recorded — the same posture as |
| 48 | + * `ObjectView.rowColorRelay-7218.test.tsx`. How the captured order then sorts |
| 49 | + * the columns is `plugin-list`'s half, and is not re-pinned here. |
| 50 | + */ |
| 51 | + |
| 52 | +import * as React from 'react'; |
| 53 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 54 | +import { render, cleanup, waitFor } from '@testing-library/react'; |
| 55 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 56 | + |
| 57 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 58 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 59 | + return { |
| 60 | + ...actual, |
| 61 | + usePermissions: () => ({ |
| 62 | + check: () => ({ allowed: true }), |
| 63 | + checkField: () => true, |
| 64 | + getFieldPermissions: () => [], |
| 65 | + getRowFilter: () => undefined, |
| 66 | + getObjectApiOperations: () => undefined, |
| 67 | + roles: [], |
| 68 | + isLoaded: false, |
| 69 | + hasCapabilities: () => true, |
| 70 | + can: () => true, |
| 71 | + cannot: () => false, |
| 72 | + }), |
| 73 | + useFieldPermissions: () => ({ canRead: () => true, canWrite: () => true, permissions: [] }), |
| 74 | + }; |
| 75 | +}); |
| 76 | + |
| 77 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 78 | + ...(await importOriginal<Record<string, unknown>>()), |
| 79 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: null }), |
| 80 | + useWorkspaceAdminStatus: () => ({ isAdmin: false, isResolved: true }), |
| 81 | + createAuthenticatedFetch: () => vi.fn(), |
| 82 | +})); |
| 83 | + |
| 84 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 85 | + ...(await importOriginal<Record<string, unknown>>()), |
| 86 | + useRealtimeSubscription: () => ({ lastMessage: null }), |
| 87 | + useConflictResolution: () => ({ hasConflicts: false, resolveAllConflicts: () => {} }), |
| 88 | +})); |
| 89 | + |
| 90 | +vi.mock('sonner', () => ({ |
| 91 | + toast: Object.assign(vi.fn(), { |
| 92 | + success: vi.fn(), error: vi.fn(), info: vi.fn(), |
| 93 | + warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(), |
| 94 | + }), |
| 95 | +})); |
| 96 | + |
| 97 | +/** The list schema this page hands down — captured, not rendered. */ |
| 98 | +let captured: any = null; |
| 99 | +vi.mock('@object-ui/plugin-list', async (importOriginal) => ({ |
| 100 | + ...(await importOriginal<typeof import('@object-ui/plugin-list')>()), |
| 101 | + ListView: (props: any) => { |
| 102 | + captured = props.schema; |
| 103 | + return null; |
| 104 | + }, |
| 105 | +})); |
| 106 | + |
| 107 | +/** |
| 108 | + * What the HOST puts on the list schema before this page's relay runs — the |
| 109 | + * `listSchema` the `...listSchema` spread carries in. It stands in for the |
| 110 | + * list-node spelling (the in-tree host fills it from a named list view), and |
| 111 | + * it is the only way to exercise the rung's second limb from here. |
| 112 | + */ |
| 113 | +let hostList: Record<string, unknown> = {}; |
| 114 | + |
| 115 | +vi.mock('@object-ui/plugin-view', async (importOriginal) => ({ |
| 116 | + ...(await importOriginal<Record<string, unknown>>()), |
| 117 | + ObjectView: (props: any) => |
| 118 | + props.renderListView?.({ |
| 119 | + schema: { ...(props.schema ?? {}), ...hostList }, |
| 120 | + dataSource: props.dataSource, |
| 121 | + onEdit: props.onEdit, |
| 122 | + className: '', |
| 123 | + refreshKey: 0, |
| 124 | + }) ?? null, |
| 125 | + ViewTabBar: () => null, |
| 126 | + ManageViewsDialog: () => null, |
| 127 | +})); |
| 128 | + |
| 129 | +vi.mock('./MetadataInspector', () => ({ |
| 130 | + MetadataPanel: () => null, |
| 131 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 132 | +})); |
| 133 | +vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null })); |
| 134 | + |
| 135 | +import { ObjectView } from './ObjectView'; |
| 136 | +import { ExpressionProvider } from '../providers/ExpressionProvider'; |
| 137 | + |
| 138 | +const OBJECT_NAME = 'duly_task'; |
| 139 | + |
| 140 | +/** The per-view order — the value this card is about. */ |
| 141 | +const VIEW_FIELD_ORDER = ['stage', 'name']; |
| 142 | +/** A list-node one. Distinct so a crossed wire fails instead of passing. */ |
| 143 | +const LIST_FIELD_ORDER = ['owner', 'stage', 'name']; |
| 144 | + |
| 145 | +function objectsWith(view: Record<string, unknown>) { |
| 146 | + return [ |
| 147 | + { |
| 148 | + name: OBJECT_NAME, |
| 149 | + label: 'Task', |
| 150 | + fields: { |
| 151 | + id: { type: 'text', label: 'Id' }, |
| 152 | + name: { type: 'text', label: 'Name' }, |
| 153 | + stage: { type: 'text', label: 'Stage' }, |
| 154 | + owner: { type: 'text', label: 'Owner' }, |
| 155 | + }, |
| 156 | + listViews: { |
| 157 | + by_stage: { label: 'By stage', type: 'grid', columns: ['name', 'stage', 'owner'], ...view }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + ]; |
| 161 | +} |
| 162 | + |
| 163 | +function makeDataSource() { |
| 164 | + return { |
| 165 | + find: vi.fn(async () => ({ data: [], total: 0 })), |
| 166 | + findOne: vi.fn(async () => null), |
| 167 | + create: vi.fn(async () => ({})), |
| 168 | + update: vi.fn(async () => ({})), |
| 169 | + delete: vi.fn(async () => ({})), |
| 170 | + } as any; |
| 171 | +} |
| 172 | + |
| 173 | +/** Render the object list and return the whole schema the relay handed down. */ |
| 174 | +async function relayed(objects: any[]): Promise<any> { |
| 175 | + captured = null; |
| 176 | + render( |
| 177 | + <ExpressionProvider user={{ id: 'u1', name: 'Ada', profile: 'admin' }}> |
| 178 | + <MemoryRouter initialEntries={[`/apps/demo/${OBJECT_NAME}`]}> |
| 179 | + <Routes> |
| 180 | + <Route |
| 181 | + path="/apps/:appName/:objectName" |
| 182 | + element={<ObjectView dataSource={makeDataSource()} objects={objects} onEdit={() => {}} />} |
| 183 | + /> |
| 184 | + </Routes> |
| 185 | + </MemoryRouter> |
| 186 | + </ExpressionProvider>, |
| 187 | + ); |
| 188 | + // `options` is built unconditionally by the same object literal as the rung |
| 189 | + // under test, so its arrival is the signal that the relay actually ran — |
| 190 | + // waiting on `fieldOrder` itself would hang rather than fail on a regression. |
| 191 | + await waitFor(() => { |
| 192 | + expect(captured?.options).toBeTruthy(); |
| 193 | + }); |
| 194 | + return captured; |
| 195 | +} |
| 196 | + |
| 197 | +beforeEach(() => { |
| 198 | + cleanup(); |
| 199 | + captured = null; |
| 200 | + hostList = {}; |
| 201 | + vi.stubGlobal( |
| 202 | + 'fetch', |
| 203 | + vi.fn(async () => |
| 204 | + new Response(JSON.stringify({ data: [] }), { |
| 205 | + status: 200, |
| 206 | + headers: { 'content-type': 'application/json' }, |
| 207 | + }), |
| 208 | + ), |
| 209 | + ); |
| 210 | +}); |
| 211 | + |
| 212 | +afterEach(() => { |
| 213 | + vi.unstubAllGlobals(); |
| 214 | + vi.clearAllMocks(); |
| 215 | +}); |
| 216 | + |
| 217 | +describe("ObjectView relays the active view's own fieldOrder (objectui#7516)", () => { |
| 218 | + it('THE FIX: a per-view `fieldOrder` reaches the renderer, verbatim', async () => { |
| 219 | + // Before the rung existed this was `undefined` for every object on this |
| 220 | + // route, which is the whole of the defect. Verbatim, because the order IS |
| 221 | + // the value: the relay sorts nothing and filters nothing — composing it |
| 222 | + // with `columns` and `hiddenFields` is `ListView`'s job. |
| 223 | + const schema = await relayed(objectsWith({ fieldOrder: VIEW_FIELD_ORDER })); |
| 224 | + expect(schema.fieldOrder).toEqual(VIEW_FIELD_ORDER); |
| 225 | + }); |
| 226 | + |
| 227 | + it('THE FIX: the per-view value OVERRIDES a list-node `fieldOrder`', async () => { |
| 228 | + hostList = { fieldOrder: LIST_FIELD_ORDER }; |
| 229 | + const schema = await relayed(objectsWith({ fieldOrder: VIEW_FIELD_ORDER })); |
| 230 | + expect(schema.fieldOrder).toEqual(VIEW_FIELD_ORDER); |
| 231 | + }); |
| 232 | + |
| 233 | + it('THE FIX: `fieldOrder` resolves by the same precedence as the `hiddenFields` rung beside it', async () => { |
| 234 | + // The two per-view halves of one composition. Both are supplied by the |
| 235 | + // list node AND by the view; both must come from the view, or `ListView` |
| 236 | + // would sort the view's subtraction by the list node's order. |
| 237 | + hostList = { hiddenFields: ['owner'], fieldOrder: LIST_FIELD_ORDER }; |
| 238 | + const schema = await relayed( |
| 239 | + objectsWith({ hiddenFields: ['name'], fieldOrder: VIEW_FIELD_ORDER }), |
| 240 | + ); |
| 241 | + expect(schema.hiddenFields).toEqual(['name']); |
| 242 | + expect(schema.fieldOrder).toEqual(VIEW_FIELD_ORDER); |
| 243 | + }); |
| 244 | + |
| 245 | + it("CONTROL: the list node's own `fieldOrder` is unchanged when the view authors none", async () => { |
| 246 | + // The rung is a fallback, not a replacement: a fix that stomped the |
| 247 | + // incoming value with `undefined` fails here. |
| 248 | + hostList = { fieldOrder: LIST_FIELD_ORDER }; |
| 249 | + const schema = await relayed(objectsWith({})); |
| 250 | + expect(schema.fieldOrder).toEqual(LIST_FIELD_ORDER); |
| 251 | + }); |
| 252 | + |
| 253 | + it('CONTROL: no `fieldOrder` anywhere stays absent', async () => { |
| 254 | + const schema = await relayed(objectsWith({})); |
| 255 | + expect(schema.fieldOrder).toBeUndefined(); |
| 256 | + }); |
| 257 | +}); |
0 commit comments