@@ -352,6 +352,84 @@ describe('PolicyUtils', () => {
352352 getCustomUnitsForDuplication ( policyWithoutCustomUnits , true , true , { distanceCustomUnitID : otherUnit . customUnitID , perDiemCustomUnitID : perDiemUnit . customUnitID } ) ,
353353 ) . toBeUndefined ( ) ;
354354 } ) ;
355+
356+ it ( 'clones the source default rate (lowest enabled index) under the API-known customUnitRateID' , ( ) => {
357+ const distanceUnitWithMultipleRates = {
358+ customUnitID : 'srcDist' ,
359+ name : CONST . CUSTOM_UNITS . NAME_DISTANCE ,
360+ enabled : true ,
361+ attributes : { unit : CONST . CUSTOM_UNITS . DISTANCE_UNIT_MILES , taxEnabled : true } ,
362+ rates : {
363+ rateB : { customUnitRateID : 'rateB' , name : 'New Rate 1' , rate : 100 , currency : 'USD' , enabled : true , index : 1 , attributes : { taxRateExternalID : 'tax_other' } } ,
364+ rateA : { customUnitRateID : 'rateA' , name : 'Default Rate' , rate : 70 , currency : 'USD' , enabled : true , index : 0 , attributes : { taxRateExternalID : 'tax_default' } } ,
365+ } ,
366+ } ;
367+ const policyWithMultipleRates : Policy = {
368+ ...createRandomPolicy ( 0 ) ,
369+ customUnits : { [ distanceUnitWithMultipleRates . customUnitID ] : distanceUnitWithMultipleRates } ,
370+ } ;
371+ const result = getCustomUnitsForDuplication ( policyWithMultipleRates , true , false , {
372+ distanceCustomUnitID : 'newDist' ,
373+ perDiemCustomUnitID : 'newPerDiem' ,
374+ customUnitRateID : 'newRate' ,
375+ } ) ;
376+ expect ( result ) . toEqual ( {
377+ newDist : {
378+ ...distanceUnitWithMultipleRates ,
379+ customUnitID : 'newDist' ,
380+ rates : {
381+ newRate : { customUnitRateID : 'newRate' , name : 'Default Rate' , rate : 70 , currency : 'USD' , enabled : true , index : 0 , attributes : { taxRateExternalID : 'tax_default' } } ,
382+ } ,
383+ } ,
384+ } ) ;
385+ } ) ;
386+
387+ it ( 'drops all rates when no enabled rate exists' , ( ) => {
388+ const distanceUnitAllDisabled = {
389+ customUnitID : 'srcDist' ,
390+ name : CONST . CUSTOM_UNITS . NAME_DISTANCE ,
391+ enabled : true ,
392+ attributes : { unit : CONST . CUSTOM_UNITS . DISTANCE_UNIT_MILES } ,
393+ rates : {
394+ rateA : { customUnitRateID : 'rateA' , name : 'Disabled' , rate : 50 , currency : 'USD' , enabled : false , index : 0 } ,
395+ } ,
396+ } ;
397+ const policyAllDisabled : Policy = {
398+ ...createRandomPolicy ( 0 ) ,
399+ customUnits : { [ distanceUnitAllDisabled . customUnitID ] : distanceUnitAllDisabled } ,
400+ } ;
401+ const result = getCustomUnitsForDuplication ( policyAllDisabled , true , false , {
402+ distanceCustomUnitID : 'newDist' ,
403+ perDiemCustomUnitID : 'newPerDiem' ,
404+ customUnitRateID : 'newRate' ,
405+ } ) ;
406+ expect ( result ?. newDist . rates ) . toEqual ( { } ) ;
407+ } ) ;
408+
409+ it ( 'treats missing index as 0 when picking the default rate' , ( ) => {
410+ const distanceUnitWithMissingIndex = {
411+ customUnitID : 'srcDist' ,
412+ name : CONST . CUSTOM_UNITS . NAME_DISTANCE ,
413+ enabled : true ,
414+ attributes : { unit : CONST . CUSTOM_UNITS . DISTANCE_UNIT_MILES } ,
415+ rates : {
416+ rateB : { customUnitRateID : 'rateB' , name : 'Indexed Rate' , rate : 100 , currency : 'USD' , enabled : true , index : 1 } ,
417+ rateA : { customUnitRateID : 'rateA' , name : 'No-Index Rate' , rate : 70 , currency : 'USD' , enabled : true } ,
418+ } ,
419+ } ;
420+ const policyWithMissingIndex : Policy = {
421+ ...createRandomPolicy ( 0 ) ,
422+ customUnits : { [ distanceUnitWithMissingIndex . customUnitID ] : distanceUnitWithMissingIndex } ,
423+ } ;
424+ const result = getCustomUnitsForDuplication ( policyWithMissingIndex , true , false , {
425+ distanceCustomUnitID : 'newDist' ,
426+ perDiemCustomUnitID : 'newPerDiem' ,
427+ customUnitRateID : 'newRate' ,
428+ } ) ;
429+ expect ( result ?. newDist . rates ) . toEqual ( {
430+ newRate : { customUnitRateID : 'newRate' , name : 'No-Index Rate' , rate : 70 , currency : 'USD' , enabled : true } ,
431+ } ) ;
432+ } ) ;
355433 } ) ;
356434 describe ( 'getRateDisplayValue' , ( ) => {
357435 it ( 'should return an empty string for NaN' , ( ) => {
0 commit comments