@@ -8,6 +8,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
88import type { Attendee } from '@src/types/onyx/IOU' ;
99import * as TransactionUtils from '../../src/libs/TransactionUtils' ;
1010import type { Card , Policy , Report , Transaction } from '../../src/types/onyx' ;
11+ import type { TransactionViolation } from '../../src/types/onyx/TransactionViolation' ;
1112import createRandomPolicy , { createCategoryTaxExpenseRules } from '../utils/collections/policies' ;
1213import { createRandomReport } from '../utils/collections/reports' ;
1314import waitForBatchedUpdates from '../utils/waitForBatchedUpdates' ;
@@ -3205,6 +3206,95 @@ describe('TransactionUtils', () => {
32053206 } ) ;
32063207 } ) ;
32073208
3209+ describe ( 'mergeProhibitedViolations' , ( ) => {
3210+ it ( 'should preserve showInReview as true when at least one source violation has showInReview: true' , ( ) => {
3211+ const violations : TransactionViolation [ ] = [
3212+ {
3213+ name : CONST . VIOLATIONS . PROHIBITED_EXPENSE ,
3214+ type : CONST . VIOLATION_TYPES . VIOLATION ,
3215+ data : { prohibitedExpenseRule : 'alcohol' } ,
3216+ showInReview : true ,
3217+ } ,
3218+ {
3219+ name : CONST . VIOLATIONS . PROHIBITED_EXPENSE ,
3220+ type : CONST . VIOLATION_TYPES . VIOLATION ,
3221+ data : { prohibitedExpenseRule : 'gambling' } ,
3222+ showInReview : false ,
3223+ } ,
3224+ {
3225+ name : CONST . VIOLATIONS . PROHIBITED_EXPENSE ,
3226+ type : CONST . VIOLATION_TYPES . VIOLATION ,
3227+ data : { prohibitedExpenseRule : 'tobacco' } ,
3228+ } ,
3229+ ] ;
3230+
3231+ const result = TransactionUtils . mergeProhibitedViolations ( violations ) ;
3232+
3233+ expect ( result ) . toHaveLength ( 1 ) ;
3234+ expect ( result . at ( 0 ) ?. name ) . toBe ( CONST . VIOLATIONS . PROHIBITED_EXPENSE ) ;
3235+ expect ( result . at ( 0 ) ?. data ?. prohibitedExpenseRule ) . toEqual ( [ 'alcohol' , 'gambling' , 'tobacco' ] ) ;
3236+ expect ( result . at ( 0 ) ?. showInReview ) . toBe ( true ) ;
3237+ } ) ;
3238+
3239+ it ( 'should set showInReview to false when no source violation has showInReview: true' , ( ) => {
3240+ const violations : TransactionViolation [ ] = [
3241+ {
3242+ name : CONST . VIOLATIONS . PROHIBITED_EXPENSE ,
3243+ type : CONST . VIOLATION_TYPES . VIOLATION ,
3244+ data : { prohibitedExpenseRule : 'alcohol' } ,
3245+ showInReview : false ,
3246+ } ,
3247+ {
3248+ name : CONST . VIOLATIONS . PROHIBITED_EXPENSE ,
3249+ type : CONST . VIOLATION_TYPES . VIOLATION ,
3250+ data : { prohibitedExpenseRule : 'gambling' } ,
3251+ } ,
3252+ ] ;
3253+
3254+ const result = TransactionUtils . mergeProhibitedViolations ( violations ) ;
3255+
3256+ expect ( result ) . toHaveLength ( 1 ) ;
3257+ expect ( result . at ( 0 ) ?. showInReview ) . toBeFalsy ( ) ;
3258+ } ) ;
3259+
3260+ it ( 'should not modify non-prohibited violations' , ( ) => {
3261+ const violations : TransactionViolation [ ] = [
3262+ {
3263+ name : CONST . VIOLATIONS . DUPLICATED_TRANSACTION ,
3264+ type : CONST . VIOLATION_TYPES . VIOLATION ,
3265+ } ,
3266+ {
3267+ name : CONST . VIOLATIONS . PROHIBITED_EXPENSE ,
3268+ type : CONST . VIOLATION_TYPES . VIOLATION ,
3269+ data : { prohibitedExpenseRule : 'alcohol' } ,
3270+ showInReview : true ,
3271+ } ,
3272+ ] ;
3273+
3274+ const result = TransactionUtils . mergeProhibitedViolations ( violations ) ;
3275+
3276+ expect ( result ) . toHaveLength ( 2 ) ;
3277+ const duplicateViolation = result . find ( ( v ) => v . name === CONST . VIOLATIONS . DUPLICATED_TRANSACTION ) ;
3278+ const prohibitedViolation = result . find ( ( v ) => v . name === CONST . VIOLATIONS . PROHIBITED_EXPENSE ) ;
3279+ expect ( duplicateViolation ) . toBeDefined ( ) ;
3280+ expect ( prohibitedViolation ?. showInReview ) . toBe ( true ) ;
3281+ expect ( prohibitedViolation ?. data ?. prohibitedExpenseRule ) . toEqual ( [ 'alcohol' ] ) ;
3282+ } ) ;
3283+
3284+ it ( 'should return violations unchanged when there are no prohibited violations' , ( ) => {
3285+ const violations : TransactionViolation [ ] = [
3286+ {
3287+ name : CONST . VIOLATIONS . DUPLICATED_TRANSACTION ,
3288+ type : CONST . VIOLATION_TYPES . VIOLATION ,
3289+ } ,
3290+ ] ;
3291+
3292+ const result = TransactionUtils . mergeProhibitedViolations ( violations ) ;
3293+
3294+ expect ( result ) . toEqual ( violations ) ;
3295+ } ) ;
3296+ } ) ;
3297+
32083298 describe ( 'shouldClearConvertedAmount' , ( ) => {
32093299 it ( 'returns false when destinationCurrency is undefined' , ( ) => {
32103300 const transaction = generateTransaction ( { currency : 'USD' } ) ;
0 commit comments