@@ -411,3 +411,164 @@ describe('a pick does not copy an amount across currencies (objectstack#36249)',
411411 } ) ;
412412 } ) ;
413413} ) ;
414+
415+ describe ( 'cross-currency pick follow-ups: a re-pick over a filled cell, and a code-plus-amount pick (objectstack#36412)' , ( ) => {
416+ const recordPrice = { type : 'currency' , currencyConfig : { currencyMode : 'record' as const , currencyField : 'currency_code' } } ;
417+ const parentPrice = { type : 'currency' , currencyConfig : { currencyMode : 'record' as const , currencyField : 'invoice.currency_code' } } ;
418+
419+ it ( "an amount's currency reads this pick's own write to its code field; a parent's code is not the record's to write" , ( ) => {
420+ expect ( amountCurrencyOf ( recordPrice , { } , { currency_code : 'EUR' } ) ) . toEqual ( { kind : 'code' , code : 'EUR' } ) ;
421+ expect ( amountCurrencyOf ( recordPrice , { currency_code : 'JPY' } , { currency_code : 'EUR' } ) ) . toEqual ( { kind : 'code' , code : 'EUR' } ) ;
422+ // Control: without the write, the record as it stands.
423+ expect ( amountCurrencyOf ( recordPrice , { } , { } ) ) . toEqual ( { kind : 'unset' } ) ;
424+ expect ( amountCurrencyOf ( recordPrice , { currency_code : 'JPY' } ) ) . toEqual ( { kind : 'code' , code : 'JPY' } ) ;
425+ expect ( amountCurrencyOf ( parentPrice , { } , { 'invoice.currency_code' : 'EUR' , invoice : { currency_code : 'EUR' } } ) ) . toEqual ( { kind : 'unset' } ) ;
426+ } ) ;
427+
428+ // A product carries its own code and a price in it; a line carries the same pair.
429+ const product = { value : 'p1' , currency_code : 'EUR' , unit_price : 100 , description : 'Euro widget' } ;
430+ const lineDefs : Record < string , unknown > = { currency_code : { type : 'text' } , unit_price : recordPrice } ;
431+ const productDefs : Record < string , unknown > = { currency_code : { type : 'text' } , unit_price : recordPrice } ;
432+ const context = ( line : Record < string , unknown > ) => ( {
433+ home : 'USD' ,
434+ sourceCurrency : ( source : string , v : unknown ) => pickedAmountCurrencyOf ( productDefs [ source ] as never , v , product ) ,
435+ targetCurrency : ( target : string , writes ?: Readonly < Record < string , unknown > > ) => amountCurrencyOf ( lineDefs [ target ] as never , line , writes ) ,
436+ } ) ;
437+ const codeCols = [
438+ { name : 'product' , type : 'lookup' as const , reference : 'product' } ,
439+ { name : 'currency_code' , type : 'text' as const } ,
440+ { name : 'unit_price' , type : 'currency' as const } ,
441+ ] ;
442+
443+ it ( 'the same-named copy writes the code and judges the price in it: both copy onto a fresh line' , ( ) => {
444+ const plan = planLookupAutofill ( codeCols , codeCols [ 0 ] , product , { row : { } } , context ( { } ) ) ;
445+ expect ( plan . patch ) . toEqual ( { product : 'p1' , currency_code : 'EUR' , unit_price : 100 } ) ;
446+ expect ( plan . withheld ) . toEqual ( [ ] ) ;
447+ // Control: a grid that draws no code column writes no code, so the line
448+ // still holds none and the price is withheld.
449+ const noCode = planLookupAutofill ( [ codeCols [ 0 ] , codeCols [ 2 ] ] , codeCols [ 0 ] , product , { row : { } } , context ( { } ) ) ;
450+ expect ( noCode . patch ) . toEqual ( { product : 'p1' } ) ;
451+ expect ( noCode . withheld [ 0 ] ) . toMatchObject ( { target : 'unit_price' , reason : 'noTargetCurrency' , from : 'EUR' } ) ;
452+ } ) ;
453+
454+ it ( 'declared pairs: a pair writing the code puts the price beside it in that code' , ( ) => {
455+ const both = [ { target : 'currency_code' , source : 'currency_code' } , { target : 'unit_price' , source : 'unit_price' } ] ;
456+ expect ( planDeclaredLookupAutofill ( both , product , { } , null , context ( { } ) ) ) . toEqual ( {
457+ patch : { currency_code : 'EUR' , unit_price : 100 } ,
458+ withheld : [ ] ,
459+ } ) ;
460+ // Control: a code the record already holds is kept (no `overwrite`), and
461+ // the price is judged in it.
462+ const kept = planDeclaredLookupAutofill ( both , product , { currency_code : 'JPY' } , null , context ( { currency_code : 'JPY' } ) ) ;
463+ expect ( kept . patch ) . toEqual ( { } ) ;
464+ expect ( kept . withheld [ 0 ] ) . toMatchObject ( { target : 'unit_price' , reason : 'differs' , from : 'EUR' , to : 'JPY' } ) ;
465+ } ) ;
466+
467+ it ( 'a value withheld over a filled target names what the target kept; one over an empty target does not' , ( ) => {
468+ const eur = { home : 'USD' , sourceCurrency : ( ) => ( { kind : 'home' as const } ) , targetCurrency : ( t : string ) => ( t === 'unit_price' ? { kind : 'code' as const , code : 'EUR' } : undefined ) } ;
469+ const over = planDeclaredLookupAutofill ( [ { target : 'unit_price' , source : 'list_price' , overwrite : true } ] , widget , { unit_price : 80 } , null , eur ) ;
470+ expect ( over . withheld ) . toEqual ( [ { target : 'unit_price' , source : 'list_price' , amount : 25 , reason : 'differs' , from : 'USD' , to : 'EUR' , held : 80 } ] ) ;
471+ const empty = planDeclaredLookupAutofill ( [ { target : 'unit_price' , source : 'list_price' } ] , widget , { unit_price : null } , null , eur ) ;
472+ expect ( empty . withheld [ 0 ] ) . not . toHaveProperty ( 'held' ) ;
473+ // The re-pick case: the target still holds what the earlier pick filled.
474+ const repick = planDeclaredLookupAutofill ( pairs , gadget , { unit_price : 25 } , widget , eur ) ;
475+ expect ( repick . withheld [ 0 ] ) . toMatchObject ( { held : 25 } ) ;
476+ } ) ;
477+
478+ describe ( 'through the grid' , ( ) => {
479+ const ROWS = [ { id : 'p1' , name : 'Euro widget' , description : 'Euro widget' , currency_code : 'EUR' , unit_price : 100 } ] ;
480+ const ds = {
481+ getObjectSchema : vi . fn ( async ( ) => ( {
482+ name : 'product' ,
483+ fields : { name : { type : 'text' } , description : { type : 'text' } , currency_code : { type : 'text' } , unit_price : { ...recordPrice , label : 'Unit Price' } } ,
484+ } ) ) ,
485+ find : vi . fn ( async ( _o : string , params ?: { $filter ?: { id ?: { $in ?: unknown [ ] } } } ) => {
486+ const wanted = params ?. $filter ?. id ?. $in ;
487+ const rows = ROWS . filter ( ( r ) => ! wanted || wanted . includes ( r . id ) ) ;
488+ return { data : rows . map ( ( r ) => ( { ...r } ) ) , total : rows . length } ;
489+ } ) ,
490+ findOne : vi . fn ( async ( _o : string , id : string ) => {
491+ const row = ROWS . find ( ( r ) => r . id === id ) ;
492+ return row ? { ...row } : null ;
493+ } ) ,
494+ } ;
495+ const drawn = [
496+ { name : 'product' , type : 'lookup' as const , reference : 'product' } ,
497+ { name : 'description' , type : 'text' as const } ,
498+ { name : 'currency_code' , type : 'text' as const } ,
499+ { name : 'unit_price' , type : 'currency' as const , label : 'Unit Price' } ,
500+ ] ;
501+
502+ function Line ( { columns = drawn , currency, line } : { columns ?: typeof drawn ; currency ?: Record < string , string | undefined > ; line ?: Record < string , unknown > } ) {
503+ const [ rows , setRows ] = React . useState < Record < string , unknown > [ ] > ( [ { product : null , description : null , unit_price : null , ...line } ] ) ;
504+ return (
505+ < LocalizationProvider value = { { currency : 'USD' } } >
506+ < SchemaRendererContext . Provider value = { { dataSource : ds } as never } >
507+ < GridField
508+ value = { rows }
509+ onChange = { ( next : Record < string , unknown > [ ] ) => setRows ( next ) }
510+ field = { { columns } as never }
511+ columnCurrencies = { currency }
512+ columnFields = { { unit_price : recordPrice } }
513+ />
514+ < output data-testid = "rows" > { JSON . stringify ( rows ) } </ output >
515+ </ SchemaRendererContext . Provider >
516+ </ LocalizationProvider >
517+ ) ;
518+ }
519+ const rowsNow = ( ) => JSON . parse ( screen . getByTestId ( 'rows' ) . textContent || '[]' ) ;
520+
521+ async function pickProduct ( ) : Promise < void > {
522+ await waitFor ( ( ) => expect ( ds . getObjectSchema ) . toHaveBeenCalled ( ) ) ;
523+ await act ( async ( ) => { await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ; } ) ;
524+ await act ( async ( ) => {
525+ fireEvent . click ( screen . getAllByTestId ( 'lookup-trigger' ) [ 0 ] ) ;
526+ } ) ;
527+ const option = await waitFor ( ( ) => {
528+ const el = screen . getAllByRole ( 'option' ) . find ( ( o ) => ( o . getAttribute ( 'title' ) ?? o . textContent ?? '' ) . includes ( 'Euro widget' ) ) ;
529+ if ( ! el ) throw new Error ( 'Euro widget not offered' ) ;
530+ return el ;
531+ } , { timeout : 3000 } ) ;
532+ await act ( async ( ) => {
533+ fireEvent . click ( option ) ;
534+ } ) ;
535+ }
536+
537+ it ( 'a fresh line: the pick copies the code and the price in it, with no note' , async ( ) => {
538+ render ( < Line /> ) ;
539+ await pickProduct ( ) ;
540+ await waitFor ( ( ) => expect ( rowsNow ( ) [ 0 ] ) . toMatchObject ( { product : 'p1' , currency_code : 'EUR' , unit_price : 100 } ) ) ;
541+ expect ( screen . queryByTestId ( 'line-items-autofill-withheld-0' ) ) . toBeNull ( ) ;
542+ } ) ;
543+
544+ it ( 'control: a grid that draws no code column leaves the price empty and says no currency is chosen' , async ( ) => {
545+ render ( < Line columns = { drawn . filter ( ( c ) => c . name !== 'currency_code' ) } /> ) ;
546+ await pickProduct ( ) ;
547+ await waitFor ( ( ) => expect ( rowsNow ( ) [ 0 ] ) . toMatchObject ( { product : 'p1' , description : 'Euro widget' } ) ) ;
548+ expect ( rowsNow ( ) [ 0 ] . unit_price ) . toBeNull ( ) ;
549+ expect ( ( await screen . findByTestId ( 'line-items-autofill-withheld-0' ) ) . textContent ) . toContain ( 'no currency is chosen' ) ;
550+ } ) ;
551+
552+ it ( 'a pick over a filled cell in another currency keeps the earlier price and says so, until the cell is edited' , async ( ) => {
553+ render ( < Line columns = { drawn . filter ( ( c ) => c . name !== 'currency_code' ) } currency = { { unit_price : 'JPY' } } line = { { unit_price : 80 } } /> ) ;
554+ await pickProduct ( ) ;
555+ await waitFor ( ( ) => expect ( rowsNow ( ) [ 0 ] ) . toMatchObject ( { product : 'p1' , description : 'Euro widget' } ) ) ;
556+ expect ( rowsNow ( ) [ 0 ] . unit_price ) . toBe ( 80 ) ;
557+ const note = await screen . findByTestId ( 'line-items-autofill-withheld-0' ) ;
558+ expect ( note . textContent ) . toContain ( 'JPY' ) ;
559+ expect ( note . textContent ) . toContain ( 'Unit Price still holds its earlier value.' ) ;
560+ // Once somebody writes the cell, the note has said what it had to say.
561+ fireEvent . change ( screen . getAllByLabelText ( 'Unit Price' ) [ 0 ] , { target : { value : '9000' } } ) ;
562+ await waitFor ( ( ) => expect ( rowsNow ( ) [ 0 ] . unit_price ) . toBe ( 9000 ) ) ;
563+ expect ( screen . queryByTestId ( 'line-items-autofill-withheld-0' ) ) . toBeNull ( ) ;
564+ } ) ;
565+
566+ it ( 'control: a pick over an empty cell in another currency says why without the kept sentence' , async ( ) => {
567+ render ( < Line columns = { drawn . filter ( ( c ) => c . name !== 'currency_code' ) } currency = { { unit_price : 'JPY' } } /> ) ;
568+ await pickProduct ( ) ;
569+ const note = await screen . findByTestId ( 'line-items-autofill-withheld-0' ) ;
570+ expect ( note . textContent ) . toContain ( 'JPY' ) ;
571+ expect ( note . textContent ) . not . toContain ( 'earlier value' ) ;
572+ } ) ;
573+ } ) ;
574+ } ) ;
0 commit comments