Skip to content

Commit f814ccc

Browse files
MelvinBotinimaga
andcommitted
Add direct unit tests for mergeProhibitedViolations in TransactionUtilsTest
Tests the real implementation (not mocked) to verify that showInReview is preserved on the merged violation when at least one source violation has showInReview: true. Co-authored-by: Issa Nimaga <inimaga@users.noreply.github.com>
1 parent 36597a5 commit f814ccc

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

tests/unit/TransactionUtilsTest.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
88
import type {Attendee} from '@src/types/onyx/IOU';
99
import * as TransactionUtils from '../../src/libs/TransactionUtils';
1010
import type {Card, Policy, Report, Transaction} from '../../src/types/onyx';
11+
import type {TransactionViolation} from '../../src/types/onyx/TransactionViolation';
1112
import createRandomPolicy, {createCategoryTaxExpenseRules} from '../utils/collections/policies';
1213
import {createRandomReport} from '../utils/collections/reports';
1314
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
@@ -3033,6 +3034,95 @@ describe('TransactionUtils', () => {
30333034
});
30343035
});
30353036

3037+
describe('mergeProhibitedViolations', () => {
3038+
it('should preserve showInReview as true when at least one source violation has showInReview: true', () => {
3039+
const violations: TransactionViolation[] = [
3040+
{
3041+
name: CONST.VIOLATIONS.PROHIBITED_EXPENSE,
3042+
type: CONST.VIOLATION_TYPES.VIOLATION,
3043+
data: {prohibitedExpenseRule: 'alcohol'},
3044+
showInReview: true,
3045+
},
3046+
{
3047+
name: CONST.VIOLATIONS.PROHIBITED_EXPENSE,
3048+
type: CONST.VIOLATION_TYPES.VIOLATION,
3049+
data: {prohibitedExpenseRule: 'gambling'},
3050+
showInReview: false,
3051+
},
3052+
{
3053+
name: CONST.VIOLATIONS.PROHIBITED_EXPENSE,
3054+
type: CONST.VIOLATION_TYPES.VIOLATION,
3055+
data: {prohibitedExpenseRule: 'tobacco'},
3056+
},
3057+
];
3058+
3059+
const result = TransactionUtils.mergeProhibitedViolations(violations);
3060+
3061+
expect(result).toHaveLength(1);
3062+
expect(result.at(0)?.name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE);
3063+
expect(result.at(0)?.data?.prohibitedExpenseRule).toEqual(['alcohol', 'gambling', 'tobacco']);
3064+
expect(result.at(0)?.showInReview).toBe(true);
3065+
});
3066+
3067+
it('should set showInReview to false when no source violation has showInReview: true', () => {
3068+
const violations: TransactionViolation[] = [
3069+
{
3070+
name: CONST.VIOLATIONS.PROHIBITED_EXPENSE,
3071+
type: CONST.VIOLATION_TYPES.VIOLATION,
3072+
data: {prohibitedExpenseRule: 'alcohol'},
3073+
showInReview: false,
3074+
},
3075+
{
3076+
name: CONST.VIOLATIONS.PROHIBITED_EXPENSE,
3077+
type: CONST.VIOLATION_TYPES.VIOLATION,
3078+
data: {prohibitedExpenseRule: 'gambling'},
3079+
},
3080+
];
3081+
3082+
const result = TransactionUtils.mergeProhibitedViolations(violations);
3083+
3084+
expect(result).toHaveLength(1);
3085+
expect(result.at(0)?.showInReview).toBeFalsy();
3086+
});
3087+
3088+
it('should not modify non-prohibited violations', () => {
3089+
const violations: TransactionViolation[] = [
3090+
{
3091+
name: CONST.VIOLATIONS.DUPLICATED_TRANSACTION,
3092+
type: CONST.VIOLATION_TYPES.VIOLATION,
3093+
},
3094+
{
3095+
name: CONST.VIOLATIONS.PROHIBITED_EXPENSE,
3096+
type: CONST.VIOLATION_TYPES.VIOLATION,
3097+
data: {prohibitedExpenseRule: 'alcohol'},
3098+
showInReview: true,
3099+
},
3100+
];
3101+
3102+
const result = TransactionUtils.mergeProhibitedViolations(violations);
3103+
3104+
expect(result).toHaveLength(2);
3105+
const duplicateViolation = result.find((v) => v.name === CONST.VIOLATIONS.DUPLICATED_TRANSACTION);
3106+
const prohibitedViolation = result.find((v) => v.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE);
3107+
expect(duplicateViolation).toBeDefined();
3108+
expect(prohibitedViolation?.showInReview).toBe(true);
3109+
expect(prohibitedViolation?.data?.prohibitedExpenseRule).toEqual(['alcohol']);
3110+
});
3111+
3112+
it('should return violations unchanged when there are no prohibited violations', () => {
3113+
const violations: TransactionViolation[] = [
3114+
{
3115+
name: CONST.VIOLATIONS.DUPLICATED_TRANSACTION,
3116+
type: CONST.VIOLATION_TYPES.VIOLATION,
3117+
},
3118+
];
3119+
3120+
const result = TransactionUtils.mergeProhibitedViolations(violations);
3121+
3122+
expect(result).toEqual(violations);
3123+
});
3124+
});
3125+
30363126
describe('shouldClearConvertedAmount', () => {
30373127
it('returns false when destinationCurrency is undefined', () => {
30383128
const transaction = generateTransaction({currency: 'USD'});

0 commit comments

Comments
 (0)