|
| 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 | + * `record:details` no longer asks `objectSchema.primaryField` WHICH ROW to |
| 11 | + * hide (objectui#7586). |
| 12 | + * |
| 13 | + * ## The surface under test is the DEDUPE, not the title |
| 14 | + * |
| 15 | + * This renderer does not draw the H1. It draws the body grid, and drops from |
| 16 | + * it the one field whose value the page H1 is already showing — otherwise |
| 17 | + * every record page repeats itself ("客户名称: Acme Corporation" directly under |
| 18 | + * an H1 reading "Acme Corporation"). The ladder that picks that field was |
| 19 | + * topped by `objSchema?.primaryField`, so the question these cases answer is |
| 20 | + * **which row disappears**, never what the heading says. A title-only pin |
| 21 | + * passes while this ladder is still wrong, because the two are decided in |
| 22 | + * different packages: `@object-ui/components`' `PageHeaderRenderer` renders |
| 23 | + * the H1, this file renders the grid under it. |
| 24 | + * |
| 25 | + * ## Why the rung had to go |
| 26 | + * |
| 27 | + * `primaryField` is a `DetailViewSchema` key (`@object-ui/types` `views.ts`) — |
| 28 | + * a VIEW key, which `DetailView.resolveDisplayTitle` legitimately reads off |
| 29 | + * `schema`. Read off an OBJECT def it is undeclared: `@objectstack/spec`'s |
| 30 | + * object schema is a `strictObject` answering `unrecognized_keys: |
| 31 | + * ['primaryField']`, and `ObjectSchema.create()` throws. objectstack#6326 |
| 32 | + * removed the identical read from two lint rules; objectui#7287 / PR #7585 |
| 33 | + * removed it from `resolveTitleField`. Three of this repo's changelogs |
| 34 | + * already call the probe "not a spec property — always undefined" while the |
| 35 | + * code kept honouring it. |
| 36 | + * |
| 37 | + * ⚠️ Its removal is a real BEHAVIOUR change for a payload that carries the key |
| 38 | + * anyway — a different row survives the grid — which is why it is pinned here |
| 39 | + * rather than waved through as a dead-code deletion. |
| 40 | + * |
| 41 | + * ⚠️ The docstring this ladder carried ("the header chip resolves the title |
| 42 | + * from objectSchema.primaryField → …") went stale the moment PR #7585 landed; |
| 43 | + * it now describes the chip as it actually is. |
| 44 | + */ |
| 45 | + |
| 46 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 47 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 48 | +import * as React from 'react'; |
| 49 | +import { RecordContextProvider } from '@object-ui/react'; |
| 50 | +import { RecordDetailsRenderer } from '../record-details'; |
| 51 | + |
| 52 | +/** |
| 53 | + * An object whose identity field is `ref_no`, spelled the ONE way no producer |
| 54 | + * can spell it. `name` is an ordinary field here, and the two carry different |
| 55 | + * values so "which row is hidden" is answerable from the DOM alone. |
| 56 | + */ |
| 57 | +const objectSchema = { |
| 58 | + name: 'contract', |
| 59 | + label: 'Contract', |
| 60 | + primaryField: 'ref_no', |
| 61 | + fields: { |
| 62 | + ref_no: { type: 'text', label: 'Ref No' }, |
| 63 | + name: { type: 'text', label: 'Name' }, |
| 64 | + amount: { type: 'number', label: 'Amount' }, |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +const FIELDS = ['ref_no', 'name', 'amount']; |
| 69 | + |
| 70 | +function renderBody(record: any, schema: any = objectSchema) { |
| 71 | + return render( |
| 72 | + <RecordContextProvider |
| 73 | + objectName="contract" |
| 74 | + recordId={record.id} |
| 75 | + data={record} |
| 76 | + objectSchema={schema} |
| 77 | + > |
| 78 | + <RecordDetailsRenderer schema={{ fields: FIELDS } as never} /> |
| 79 | + </RecordContextProvider>, |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +beforeEach(() => { |
| 84 | + // `useRecordEditable` probes `POST /api/v1/security/explain` for the |
| 85 | + // ROW-level verdict; happy-dom resolves that relative URL to a REAL socket, |
| 86 | + // which the repo's network-escape guard fails the file for (objectui#6640). |
| 87 | + // Serve it from a double — its answer is orthogonal to which row is hidden. |
| 88 | + vi.stubGlobal('fetch', vi.fn(async () => ({ |
| 89 | + ok: true, |
| 90 | + status: 200, |
| 91 | + json: async () => ({ allowed: true }), |
| 92 | + text: async () => '{"allowed":true}', |
| 93 | + })) as never); |
| 94 | +}); |
| 95 | + |
| 96 | +afterEach(() => { |
| 97 | + cleanup(); |
| 98 | + vi.unstubAllGlobals(); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('record:details dedupe — `primaryField` no longer picks the hidden row (#7586)', () => { |
| 102 | + it('hides the row the H1 actually shows, not the `primaryField` row', () => { |
| 103 | + renderBody({ id: 'C1', ref_no: 'HT-2026-001', name: 'Acme Corporation', amount: 42 }); |
| 104 | + |
| 105 | + // `name` is what the header chip resolves to for this record, so `name` is |
| 106 | + // the row that goes. Before this fix `primaryField` took `ref_no` out |
| 107 | + // instead, leaving the grid repeating the H1 and hiding a field nothing |
| 108 | + // else showed. |
| 109 | + expect(screen.queryByText('Acme Corporation')).toBeNull(); |
| 110 | + expect(screen.getByText('HT-2026-001')).toBeInTheDocument(); |
| 111 | + |
| 112 | + // An ordinary row is untouched — the dedupe drops ONE field, it does not |
| 113 | + // filter the grid down. |
| 114 | + expect(screen.getByText('42')).toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('hides nothing when `primaryField` names the only value on the record', () => { |
| 118 | + // Nothing in the literal display-name walk has a value here. Before the |
| 119 | + // fix `ref_no` was hidden and the body grid rendered a single `amount` |
| 120 | + // row; the H1 for this record is the `${objectLabel} ${id}` floor, which |
| 121 | + // no grid row duplicates, so nothing should be dropped. |
| 122 | + renderBody({ id: 'C2', ref_no: 'HT-2026-002', amount: 7 }); |
| 123 | + |
| 124 | + expect(screen.getByText('HT-2026-002')).toBeInTheDocument(); |
| 125 | + expect(screen.getByText('7')).toBeInTheDocument(); |
| 126 | + }); |
| 127 | + |
| 128 | + /** |
| 129 | + * CONTROL — the dedupe still runs. |
| 130 | + * |
| 131 | + * Without this, deleting the whole dedupe block would pass both cases above: |
| 132 | + * "the `ref_no` row survives" is satisfied by a renderer that hides nothing |
| 133 | + * at all. This is the case that goes red for that, and it is the reason the |
| 134 | + * two cases above are readable as a measurement of the LADDER rather than of |
| 135 | + * its absence. |
| 136 | + */ |
| 137 | + it('CONTROL: the H1 field is still dropped from the grid', () => { |
| 138 | + renderBody( |
| 139 | + { id: 'C3', ref_no: 'HT-2026-003', name: 'Acme Corporation', amount: 42 }, |
| 140 | + { ...objectSchema, primaryField: undefined }, |
| 141 | + ); |
| 142 | + |
| 143 | + expect(screen.queryByText('Acme Corporation')).toBeNull(); |
| 144 | + expect(screen.getByText('HT-2026-003')).toBeInTheDocument(); |
| 145 | + }); |
| 146 | + |
| 147 | + /** |
| 148 | + * CONTROL — `primaryField` on a VIEW schema is a different key and stays. |
| 149 | + * |
| 150 | + * `DetailViewSchema.primaryField` is declared (`packages/types/src/views.ts`) |
| 151 | + * and `DetailView.resolveDisplayTitle` reads it as rung 1. Nothing in this |
| 152 | + * card touches that; a change that deleted the key everywhere rather than |
| 153 | + * only where it sits on an OBJECT def turns this red. |
| 154 | + */ |
| 155 | + it('CONTROL: the declared VIEW-level `primaryField` is untouched', async () => { |
| 156 | + const { DetailView } = await import('../../DetailView'); |
| 157 | + |
| 158 | + render( |
| 159 | + <DetailView |
| 160 | + schema={ |
| 161 | + { |
| 162 | + type: 'detail-view', |
| 163 | + objectName: 'contract', |
| 164 | + primaryField: 'ref_no', |
| 165 | + data: { id: 'C4', ref_no: 'HT-2026-004', name: 'Acme Corporation' }, |
| 166 | + fields: ['name'], |
| 167 | + showHeader: true, |
| 168 | + } as never |
| 169 | + } |
| 170 | + />, |
| 171 | + ); |
| 172 | + |
| 173 | + expect(screen.getByText('HT-2026-004')).toBeInTheDocument(); |
| 174 | + }); |
| 175 | +}); |
0 commit comments