@@ -119,19 +119,31 @@ import { MasterDetailForm, type MasterDetailDetailConfig } from '../MasterDetail
119119
120120/**
121121 * Every prop object `MasterDetailForm` hands the line grid, captured on the
122- * way in. The REAL `LineItemsField` still renders, so the cells rows 4 and 5
123- * fill are the product's own.
122+ * way in, and the LATEST props of each mounted grid, keyed by a per-instance id
123+ * that the grid also writes into its section as a hidden sentinel. The REAL
124+ * `LineItemsField` still renders, so the cells rows 4 and 5 fill are the
125+ * product's own.
124126 */
125- const { gridProps } = vi . hoisted ( ( ) => ( { gridProps : [ ] as Array < Record < string , any > > } ) ) ;
127+ const { gridProps, latestGridProps } = vi . hoisted ( ( ) => ( {
128+ gridProps : [ ] as Array < Record < string , any > > ,
129+ latestGridProps : new Map < string , Record < string , any > > ( ) ,
130+ } ) ) ;
126131vi . mock ( '@object-ui/fields' , async ( importOriginal ) => {
127132 const actual = await importOriginal < typeof import ( '@object-ui/fields' ) > ( ) ;
128- const { createElement } = await import ( 'react' ) ;
133+ const { createElement, Fragment , useId } = await import ( 'react' ) ;
129134 const RealLineItemsField = actual . LineItemsField ;
130135 return {
131136 ...actual ,
132137 LineItemsField : ( props : Record < string , any > ) => {
138+ const captureId = useId ( ) ;
133139 gridProps . push ( props ) ;
134- return createElement ( RealLineItemsField as any , props ) ;
140+ latestGridProps . set ( captureId , props ) ;
141+ return createElement (
142+ Fragment ,
143+ null ,
144+ createElement ( 'span' , { hidden : true , 'data-grid-capture' : captureId } ) ,
145+ createElement ( RealLineItemsField as any , props ) ,
146+ ) ;
135147 } ,
136148 } ;
137149} ) ;
@@ -199,16 +211,22 @@ async function mount(schema: Record<string, unknown>, dataSource = makeDataSourc
199211 return { ...view , dataSource } ;
200212}
201213
202- /** The props the grid of the section headed `heading` received LAST. */
214+ /**
215+ * The props the grid of the section headed `heading` received LAST. A heading
216+ * draws before its columns load, so a section can exist with no grid in it yet:
217+ * that throws, and a caller inside `waitFor` retries until the grid is
218+ * captured. The section is found by its heading and its grid by the sentinel
219+ * inside it, so a render pass that re-renders only some grids cannot hand back
220+ * another section's props.
221+ */
203222function gridOf ( heading : string ) : Record < string , any > {
204223 const section = screen . getByRole ( 'heading' , { name : heading } ) . closest ( 'section' ) ;
205224 if ( ! section ) throw new Error ( `no section headed ${ heading } ` ) ;
206- const index = Array . from ( document . querySelectorAll ( 'section' ) ) . indexOf ( section ) ;
207- // One grid per section, mounted in section order, so the Nth section's
208- // props are every Nth capture of the latest render pass.
209- const sections = document . querySelectorAll ( 'section' ) . length ;
210- const latest = gridProps . slice ( - sections ) ;
211- return latest [ index ] ;
225+ const sentinels = section . querySelectorAll ( '[data-grid-capture]' ) ;
226+ if ( sentinels . length !== 1 ) throw new Error ( `section ${ heading } holds ${ sentinels . length } captured grids, not one` ) ;
227+ const props = latestGridProps . get ( sentinels [ 0 ] . getAttribute ( 'data-grid-capture' ) ! ) ;
228+ if ( ! props ) throw new Error ( `no props captured for the grid of section ${ heading } ` ) ;
229+ return props ;
212230}
213231
214232const headings = ( container : HTMLElement ) =>
@@ -245,6 +263,7 @@ async function fillQty(n: number, value: string) {
245263describe ( '`object-master-detail-form` — the member shape of `details`' , ( ) => {
246264 beforeEach ( ( ) => {
247265 gridProps . length = 0 ;
266+ latestGridProps . clear ( ) ;
248267 } ) ;
249268
250269 it ( '1. each member is its own section, in AUTHORED order, headed by `title` (or `Line Items`), with `columns` in authored order' , async ( ) => {
@@ -255,11 +274,13 @@ describe('`object-master-detail-form` — the member shape of `details`', () =>
255274 ] ,
256275 } ) ;
257276 await waitFor ( ( ) => expect ( headings ( container ) ) . toEqual ( [ 'Lines' , 'Line Items' ] ) ) ;
258- expect (
259- gridOf ( 'Lines' ) . field . columns . map ( ( c : { name : string } ) => c . name ) ,
260- 'the authored column order, not the child object’s field order' ,
261- ) . toEqual ( [ 'price' , 'qty' ] ) ;
262- expect ( gridOf ( 'Line Items' ) . field . columns . map ( ( c : { name : string } ) => c . name ) ) . toEqual ( [ 'note' ] ) ;
277+ await waitFor ( ( ) => {
278+ expect (
279+ gridOf ( 'Lines' ) . field . columns . map ( ( c : { name : string } ) => c . name ) ,
280+ 'the authored column order, not the child object’s field order' ,
281+ ) . toEqual ( [ 'price' , 'qty' ] ) ;
282+ expect ( gridOf ( 'Line Items' ) . field . columns . map ( ( c : { name : string } ) => c . name ) ) . toEqual ( [ 'note' ] ) ;
283+ } ) ;
263284 } ) ;
264285
265286 it ( '2. four members reach the grid under its keys, as exactly eight keys, and off-list members are not forwarded' , async ( ) => {
@@ -367,7 +388,8 @@ describe('`object-master-detail-form` — the member shape of `details`', () =>
367388 return f ;
368389 } ) ;
369390 expect ( written . sortField , 'the detail’s written `sortField` is read by nothing' ) . toBe ( 'position' ) ;
370- expect ( gridOf ( 'Steps, configured' ) . field . sortField , 'nothing derived, and the written one not read' ) . toBeUndefined ( ) ;
391+ const configured = await waitFor ( ( ) => gridOf ( 'Steps, configured' ) . field ) ;
392+ expect ( configured . sortField , 'nothing derived, and the written one not read' ) . toBeUndefined ( ) ;
371393 } ) ;
372394
373395 it ( '3. `inlineMode` and `formFields` choose the form factor, each arm the other’s control' , async ( ) => {
@@ -390,13 +412,13 @@ describe('`object-master-detail-form` — the member shape of `details`', () =>
390412 expect ( typeof list . onRowExpand , 'list mode edits through the full form' ) . toBe ( 'function' ) ;
391413 expect ( list . field . addLabel , 'list mode names its Add action even when the detail’s `addLabel` is unset' ) . toBe ( 'Add' ) ;
392414
393- const wider = gridOf ( 'Wider form' ) ;
415+ const wider = await waitFor ( ( ) => gridOf ( 'Wider form' ) ) ;
394416 expect ( wider . displayMode ) . toBe ( 'grid' ) ;
395417 expect ( typeof wider . onRowExpand , '`formFields` wider than `columns` offers the row form' ) . toBe ( 'function' ) ;
396418 expect ( wider . onAdd ) . toBeUndefined ( ) ;
397419 expect ( wider . field . addLabel , 'grid mode leaves the label to the grid' ) . toBeUndefined ( ) ;
398420
399- const cells = gridOf ( 'Cells only' ) ;
421+ const cells = await waitFor ( ( ) => gridOf ( 'Cells only' ) ) ;
400422 expect ( cells . displayMode ) . toBe ( 'grid' ) ;
401423 expect ( cells . onRowExpand , 'a form no wider than the cells adds nothing to open' ) . toBeUndefined ( ) ;
402424 } ) ;
@@ -493,7 +515,7 @@ describe('`object-master-detail-form` — the member shape of `details`', () =>
493515 const distinct = ( ) => [ ...new Set ( find . mock . calls . map ( ( call ) => JSON . stringify ( call ) ) ) ] . sort ( ) ;
494516 await waitFor ( ( ) => expect ( distinct ( ) ) . toEqual ( expected ) ) ;
495517 await waitFor ( ( ) => expect ( gridOf ( 'Lines' ) . value ) . toEqual ( [ { id : 'l1' , po : 'po1' , qty : 4 } ] ) ) ;
496- expect ( gridOf ( 'Notes' ) . value ) . toEqual ( [ ] ) ;
518+ await waitFor ( ( ) => expect ( gridOf ( 'Notes' ) . value ) . toEqual ( [ ] ) ) ;
497519 } ) ;
498520
499521 it ( '7. control: `details: []` renders no section and mounts no grid, and the batch is the parent leg alone' , async ( ) => {
0 commit comments