@@ -257,3 +257,140 @@ describe('the phone card amount slot takes a formula or summary that draws as mo
257257 expect ( within ( card ) . queryByTestId ( 'grid-card-amount-line' ) ) . toBeNull ( ) ;
258258 } ) ;
259259} ) ;
260+
261+ /**
262+ * objectstack#37891 — when the object declares `highlightFields` (ADR-0085,
263+ * "which fields matter most", in order), the phone card reads its amount and
264+ * subtitle from that list, not from the view's column order: the amount is
265+ * the first highlighted field that draws as money, the subtitle the next
266+ * three, in that order, empty values dropped, and a number, currency or
267+ * percent part carries its field label. A highlighted field no column shows
268+ * is drawn too, and the grid's fetch asks for it. An object that declares no
269+ * `highlightFields` keeps the column-order rule above.
270+ *
271+ * The fixture is HotCRM's order line (`crm_sales_order_line`), whose related
272+ * list shows `line_number, product_title, quantity, unit_price, discount,
273+ * total_price`: on the base tree its card read `1 · 2` with the unit price as
274+ * the amount, whatever `highlightFields` said.
275+ *
276+ * ── Directions on the base tree (predicted before the first run) ─────────
277+ * amount is the highlighted total, not the unit price RED
278+ * subtitle is the labelled highlighted parts, in order RED
279+ * a highlighted field no column shows is drawn and fetched RED
280+ * an empty highlighted part is dropped RED
281+ * control: no highlightFields keeps `1 · 2`, unit price, no labels GREEN
282+ * control: a masked highlighted field is neither amount nor part GREEN
283+ */
284+ describe ( 'the phone card follows the object\'s highlightFields (objectstack#37891)' , ( ) => {
285+ const LINE_OBJECT = 'crm_sales_order_line_37891' ;
286+ const LINE_FIELDS = {
287+ id : { type : 'text' } ,
288+ product_title : { type : 'text' , label : 'Product Title' } ,
289+ crm_product : { type : 'text' , label : 'Product' } ,
290+ quantity : { type : 'number' , label : 'Quantity' , scale : 0 } ,
291+ unit_price : { type : 'currency' , label : 'Sales Price' } ,
292+ discount : { type : 'percent' , label : 'Discount %' , scale : 2 } ,
293+ total_price : {
294+ type : 'formula' ,
295+ label : 'Total' ,
296+ returnType : 'currency' ,
297+ expression : 'record.quantity * record.unit_price * (1 - record.discount / 100)' ,
298+ } ,
299+ line_number : { type : 'number' , label : 'Line #' , scale : 0 } ,
300+ access_pin : { type : 'password' , label : 'Access PIN' } ,
301+ } ;
302+ const LINE_ROWS = [
303+ { id : 'l1' , line_number : 1 , product_title : 'Admin Training Workshop' , crm_product : 'Training' , quantity : 2 , unit_price : 6000 , discount : 10 , total_price : 10800 , access_pin : 's3cr3t-pin' } ,
304+ { id : 'l2' , line_number : 2 , product_title : 'AI Agent Seat (Annual)' , crm_product : 'Seat' , quantity : 10 , unit_price : 1000 , discount : null , total_price : 10000 , access_pin : 's3cr3t-pin' } ,
305+ ] ;
306+ /** HotCRM's `relatedListColumns` for the order's lines. */
307+ const RELATED = [ 'line_number' , 'product_title' , 'quantity' , 'unit_price' , 'discount' , 'total_price' ] ;
308+
309+ async function lineCards ( columns : string [ ] , highlightFields ?: string [ ] ) {
310+ setWidth ( 390 ) ;
311+ const find = vi . fn ( async ( ) => ( { data : LINE_ROWS , total : LINE_ROWS . length , hasMore : false , pageSize : 50 } ) ) ;
312+ const dataSource = {
313+ find,
314+ getObjectSchema : async ( name : string ) => ( {
315+ name,
316+ nameField : 'product_title' ,
317+ ...( highlightFields ? { highlightFields } : { } ) ,
318+ fields : structuredClone ( LINE_FIELDS ) ,
319+ } ) ,
320+ } as never ;
321+ render (
322+ < LocalizationProvider value = { { currency : 'CNY' , locale : 'en' } } >
323+ < ActionProvider >
324+ < SchemaRendererProvider dataSource = { dataSource } >
325+ < ObjectGrid
326+ schema = { { type : 'object-grid' , objectName : LINE_OBJECT , columns : columns . map ( ( field ) => ( { field } ) ) , pagination : { pageSize : 50 } } as never }
327+ dataSource = { dataSource }
328+ />
329+ </ SchemaRendererProvider >
330+ </ ActionProvider >
331+ </ LocalizationProvider > ,
332+ ) ;
333+ // The money cells read the field defs, so a CN¥ amount proves they landed.
334+ await waitFor ( ( ) => expect ( cards ( ) ) . toHaveLength ( 2 ) ) ;
335+ await waitFor ( ( ) => expect ( document . body . textContent ) . toContain ( 'CN¥' ) ) ;
336+ expect ( document . querySelector ( 'table' ) , 'the card layout, not the table' ) . toBeNull ( ) ;
337+ return { find, first : cardOf ( 'Admin Training Workshop' ) , second : cardOf ( 'AI Agent Seat (Annual)' ) } ;
338+ }
339+ const parts = ( card : HTMLElement ) => within ( card ) . getAllByTestId ( 'grid-card-subtitle-part' ) ;
340+ const labels = ( card : HTMLElement ) =>
341+ within ( card ) . queryAllByTestId ( 'grid-card-subtitle-label' ) . map ( ( el ) => el . textContent ?. trim ( ) ) ;
342+
343+ it ( 'takes the first highlighted money field as the amount: the line total, not the unit price the table shows first' , async ( ) => {
344+ const { first } = await lineCards ( RELATED , [ 'total_price' , 'quantity' , 'unit_price' , 'discount' ] ) ;
345+ expect ( within ( first ) . getByTestId ( 'grid-card-amount' ) . textContent ) . toBe ( 'CN¥10,800.00' ) ;
346+ } ) ;
347+
348+ it ( 'draws the next highlighted fields as the subtitle, in order, each figure under its field label' , async ( ) => {
349+ const { first } = await lineCards ( RELATED , [ 'total_price' , 'quantity' , 'unit_price' , 'discount' ] ) ;
350+ expect ( parts ( first ) ) . toHaveLength ( 3 ) ;
351+ expect ( labels ( first ) ) . toEqual ( [ 'Quantity' , 'Sales Price' , 'Discount %' ] ) ;
352+ const [ quantity , price , discount ] = parts ( first ) . map ( ( p ) => p . textContent ) ;
353+ expect ( quantity ) . toBe ( 'Quantity 2' ) ;
354+ expect ( price ) . toBe ( 'Sales Price CN¥6,000.00' ) ;
355+ expect ( discount ) . toMatch ( / ^ D i s c o u n t % 1 0 ( \. 0 0 ) ? \s ? % $ / ) ;
356+ // The line number the table shows first is no longer a subtitle part.
357+ expect ( within ( first ) . getByTestId ( 'grid-card-subtitle' ) . textContent ) . not . toMatch ( / ^ 1 · / ) ;
358+ } ) ;
359+
360+ it ( 'labels only figures: a text part is drawn bare' , async ( ) => {
361+ const { first } = await lineCards ( RELATED , [ 'total_price' , 'crm_product' , 'quantity' ] ) ;
362+ expect ( parts ( first ) . map ( ( p ) => p . textContent ) ) . toEqual ( [ 'Training' , 'Quantity 2' ] ) ;
363+ expect ( labels ( first ) ) . toEqual ( [ 'Quantity' ] ) ;
364+ } ) ;
365+
366+ it ( 'drops an empty highlighted part rather than drawing it' , async ( ) => {
367+ const { second } = await lineCards ( RELATED , [ 'total_price' , 'quantity' , 'unit_price' , 'discount' ] ) ;
368+ expect ( within ( second ) . getByTestId ( 'grid-card-amount' ) . textContent ) . toBe ( 'CN¥10,000.00' ) ;
369+ expect ( labels ( second ) ) . toEqual ( [ 'Quantity' , 'Sales Price' ] ) ;
370+ } ) ;
371+
372+ it ( 'draws a highlighted field no column shows, and the fetch asks for it' , async ( ) => {
373+ const { first, find } = await lineCards (
374+ [ 'line_number' , 'product_title' , 'quantity' , 'unit_price' ] ,
375+ [ 'total_price' , 'quantity' , 'unit_price' , 'discount' ] ,
376+ ) ;
377+ expect ( within ( first ) . getByTestId ( 'grid-card-amount' ) . textContent ) . toBe ( 'CN¥10,800.00' ) ;
378+ expect ( labels ( first ) ) . toEqual ( [ 'Quantity' , 'Sales Price' , 'Discount %' ] ) ;
379+ const select = ( find . mock . calls . at ( - 1 ) as unknown [ ] | undefined ) ?. [ 1 ] as { $select ?: string [ ] } | undefined ;
380+ expect ( select ?. $select ) . toEqual ( expect . arrayContaining ( [ 'total_price' , 'discount' ] ) ) ;
381+ } ) ;
382+
383+ it ( 'CONTROL: an object with no highlightFields keeps the column-order card (line number · quantity, unit price, no labels)' , async ( ) => {
384+ const { first } = await lineCards ( RELATED ) ;
385+ expect ( within ( first ) . getByTestId ( 'grid-card-subtitle' ) . textContent ) . toBe ( '1 · 2' ) ;
386+ expect ( within ( first ) . getByTestId ( 'grid-card-amount' ) . textContent ) . toBe ( 'CN¥6,000.00' ) ;
387+ expect ( labels ( first ) ) . toEqual ( [ ] ) ;
388+ } ) ;
389+
390+ it ( 'CONTROL: a masked highlighted field is neither the amount nor a subtitle part, and its value is never printed' , async ( ) => {
391+ const { first } = await lineCards ( RELATED , [ 'access_pin' , 'total_price' , 'quantity' ] ) ;
392+ expect ( within ( first ) . getByTestId ( 'grid-card-amount' ) . textContent ) . toBe ( 'CN¥10,800.00' ) ;
393+ expect ( labels ( first ) ) . toEqual ( [ 'Quantity' ] ) ;
394+ expect ( first . textContent ) . not . toContain ( 's3cr3t-pin' ) ;
395+ } ) ;
396+ } ) ;
0 commit comments