From a22e976027e26ac2db759d66cc16b8f34400e08f Mon Sep 17 00:00:00 2001 From: Hubert Sosinski Date: Tue, 17 Mar 2026 10:49:47 +0100 Subject: [PATCH 1/5] fix report badge reactivity on policy field changes --- .../OnyxDerived/configs/reportAttributes.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts index 2360aff7e04b3..cbebe08284274 100644 --- a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts +++ b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts @@ -26,6 +26,26 @@ const prepareReportKeys = (keys: string[]) => { ]; }; +const hasPolicyRelevantFieldChanged = (prev: Policy | null | undefined, next: Policy | null | undefined): boolean => { + if (!prev && !next) { + return false; + } + if (!prev || !next) { + return true; + } + return ( + prev.name !== next.name || + prev.type !== next.type || + prev.approvalMode !== next.approvalMode || + prev.reimbursementChoice !== next.reimbursementChoice || + prev.autoReimbursementLimit !== next.autoReimbursementLimit || + prev.role !== next.role || + prev.achAccount?.accountNumber !== next.achAccount?.accountNumber || + JSON.stringify(prev.fieldList) !== JSON.stringify(next.fieldList) || + JSON.stringify(prev.autoReimbursement) !== JSON.stringify(next.autoReimbursement) + ); +}; + const checkDisplayNamesChanged = (personalDetails: OnyxEntry) => { if (!personalDetails) { return false; @@ -98,9 +118,25 @@ export default createOnyxDerivedValueConfig({ } // if policies are loaded first time, we need to recompute all report attributes to get correct action badge in LHN, such as Approve because it depends on policy's type (see canApproveIOU function) + const policyChangedReportKeys: string[] = []; if (hasKeyTriggeredCompute(ONYXKEYS.COLLECTION.POLICY, sourceValues)) { if (isFullyComputed && Object.keys(previousPolicies ?? {}).length === 0 && Object.keys(policies ?? {}).length > 0) { isFullyComputed = false; + } else if (isFullyComputed) { + // Policy updated — only recompute reports whose relevant fields actually changed + const changedPolicyIDs = new Set(); + for (const key of Object.keys(sourceValues?.[ONYXKEYS.COLLECTION.POLICY] ?? {})) { + if (hasPolicyRelevantFieldChanged(previousPolicies?.[key], policies?.[key])) { + changedPolicyIDs.add(key.replace(ONYXKEYS.COLLECTION.POLICY, '')); + } + } + if (changedPolicyIDs.size > 0) { + for (const [reportKey, report] of Object.entries(reports ?? {})) { + if (report?.policyID && changedPolicyIDs.has(report.policyID)) { + policyChangedReportKeys.push(reportKey); + } + } + } } previousPolicies = policies; } @@ -139,6 +175,7 @@ export default createOnyxDerivedValueConfig({ ...Object.keys(reportActionsUpdates), ...Object.keys(reportNameValuePairsUpdates), ...Array.from(reportUpdatesRelatedToReportActions), + ...policyChangedReportKeys, ]; if (isFullyComputed) { From 14579a32dc52bc00ad8cc42a5d487f955d430f7f Mon Sep 17 00:00:00 2001 From: Hubert Sosinski Date: Thu, 19 Mar 2026 09:35:55 +0100 Subject: [PATCH 2/5] only badges are handled when policy is changed --- src/libs/actions/OnyxDerived/configs/reportAttributes.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts index e30d4ae1a8d05..3f1c5edbb4bd4 100644 --- a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts +++ b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts @@ -33,14 +33,11 @@ const hasPolicyRelevantFieldChanged = (prev: Policy | null | undefined, next: Po return true; } return ( - prev.name !== next.name || prev.type !== next.type || prev.approvalMode !== next.approvalMode || prev.reimbursementChoice !== next.reimbursementChoice || prev.autoReimbursementLimit !== next.autoReimbursementLimit || prev.role !== next.role || - prev.achAccount?.accountNumber !== next.achAccount?.accountNumber || - JSON.stringify(prev.fieldList) !== JSON.stringify(next.fieldList) || JSON.stringify(prev.autoReimbursement) !== JSON.stringify(next.autoReimbursement) ); }; From e4802df43810c959fb20b3696a22d58ce953223a Mon Sep 17 00:00:00 2001 From: Hubert Sosinski Date: Thu, 19 Mar 2026 10:46:36 +0100 Subject: [PATCH 3/5] only badges are handled when policy is changed --- src/libs/actions/OnyxDerived/configs/reportAttributes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts index 3f1c5edbb4bd4..8a8a191cefaf4 100644 --- a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts +++ b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts @@ -38,7 +38,7 @@ const hasPolicyRelevantFieldChanged = (prev: Policy | null | undefined, next: Po prev.reimbursementChoice !== next.reimbursementChoice || prev.autoReimbursementLimit !== next.autoReimbursementLimit || prev.role !== next.role || - JSON.stringify(prev.autoReimbursement) !== JSON.stringify(next.autoReimbursement) + prev.autoReimbursement?.limit !== next.autoReimbursement?.limit ); }; From f7e20524caf248aae1d8e761d59c8d578cd625ac Mon Sep 17 00:00:00 2001 From: Hubert Sosinski Date: Fri, 20 Mar 2026 11:49:51 +0100 Subject: [PATCH 4/5] tests for hasPolicyRelevantFieldChanged in reportAttributesTest.ts --- .../OnyxDerived/configs/reportAttributes.ts | 2 + tests/unit/reportAttributesTest.ts | 254 ++++++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 tests/unit/reportAttributesTest.ts diff --git a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts index 5dbf97e0229e3..f361b006314d1 100644 --- a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts +++ b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts @@ -354,3 +354,5 @@ export default createOnyxDerivedValueConfig({ }; }, }); + +export {hasPolicyRelevantFieldChanged}; diff --git a/tests/unit/reportAttributesTest.ts b/tests/unit/reportAttributesTest.ts new file mode 100644 index 0000000000000..c81532ff33580 --- /dev/null +++ b/tests/unit/reportAttributesTest.ts @@ -0,0 +1,254 @@ +import type {OnyxCollection} from 'react-native-onyx'; +import type reportAttributesModuleDefault from '@userActions/OnyxDerived/configs/reportAttributes'; +import {hasPolicyRelevantFieldChanged} from '@userActions/OnyxDerived/configs/reportAttributes'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy, Report, ReportAttributesDerivedValue} from '@src/types/onyx'; + +type ReportAttributesConfig = typeof reportAttributesModuleDefault; + +jest.mock('@libs/ReportUtils', () => ({ + generateReportAttributes: jest.fn(() => ({ + hasAnyViolations: false, + requiresAttention: false, + reportErrors: {}, + oneTransactionThreadReportID: undefined, + actionBadge: undefined, + actionTargetReportActionID: undefined, + })), + generateIsEmptyReport: jest.fn(() => false), + hasVisibleReportFieldViolations: jest.fn(() => false), + isArchivedReport: jest.fn(() => false), + isValidReport: jest.fn(() => true), +})); + +jest.mock('@libs/SidebarUtils', () => ({ + // eslint-disable-next-line @typescript-eslint/naming-convention + __esModule: true, + default: { + getReasonAndReportActionThatHasRedBrickRoad: jest.fn(() => undefined), + }, +})); + +jest.mock('@libs/ReportNameUtils', () => ({ + computeReportName: jest.fn(() => 'Test Report'), +})); + +const basePolicy: Policy = { + id: 'policy1', + name: 'Test Policy', + type: CONST.POLICY.TYPE.CORPORATE, + outputCurrency: CONST.CURRENCY.USD, + role: CONST.POLICY.ROLE.ADMIN, + approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC, + reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES, + autoReimbursementLimit: 1000, + autoReimbursement: {limit: 500}, +} as unknown as Policy; + +describe('hasPolicyRelevantFieldChanged', () => { + describe('null / undefined edge cases', () => { + it('returns false when both are null', () => { + expect(hasPolicyRelevantFieldChanged(null, null)).toBe(false); + }); + + it('returns false when both are undefined', () => { + expect(hasPolicyRelevantFieldChanged(undefined, undefined)).toBe(false); + }); + + it('returns false when both are null/undefined mix', () => { + expect(hasPolicyRelevantFieldChanged(null, undefined)).toBe(false); + expect(hasPolicyRelevantFieldChanged(undefined, null)).toBe(false); + }); + + it('returns true when prev is null and next has a policy', () => { + expect(hasPolicyRelevantFieldChanged(null, basePolicy)).toBe(true); + }); + + it('returns true when next is null and prev had a policy', () => { + expect(hasPolicyRelevantFieldChanged(basePolicy, null)).toBe(true); + }); + }); + + describe('identical policies', () => { + it('returns false when all tracked fields are the same', () => { + const copy = {...basePolicy}; + expect(hasPolicyRelevantFieldChanged(basePolicy, copy)).toBe(false); + }); + + it('returns false when only a non-tracked field changes', () => { + const updated = {...basePolicy, name: 'Updated Name'} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(basePolicy, updated)).toBe(false); + }); + }); + + describe('tracked field changes', () => { + it('returns true when type changes', () => { + const updated = {...basePolicy, type: CONST.POLICY.TYPE.TEAM} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(basePolicy, updated)).toBe(true); + }); + + it('returns true when approvalMode changes', () => { + const updated = {...basePolicy, approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(basePolicy, updated)).toBe(true); + }); + + it('returns true when reimbursementChoice changes', () => { + const updated = {...basePolicy, reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(basePolicy, updated)).toBe(true); + }); + + it('returns true when autoReimbursementLimit changes', () => { + const updated = {...basePolicy, autoReimbursementLimit: 2000} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(basePolicy, updated)).toBe(true); + }); + + it('returns true when role changes', () => { + const updated = {...basePolicy, role: CONST.POLICY.ROLE.USER} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(basePolicy, updated)).toBe(true); + }); + + it('returns true when autoReimbursement.limit changes', () => { + const updated = {...basePolicy, autoReimbursement: {limit: 999}} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(basePolicy, updated)).toBe(true); + }); + + it('returns true when autoReimbursement goes from defined to undefined', () => { + const updated = {...basePolicy, autoReimbursement: undefined} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(basePolicy, updated)).toBe(true); + }); + + it('returns true when autoReimbursement goes from undefined to defined', () => { + const withoutAutoReimburse = {...basePolicy, autoReimbursement: undefined} as unknown as Policy; + expect(hasPolicyRelevantFieldChanged(withoutAutoReimburse, basePolicy)).toBe(true); + }); + }); +}); + +describe('reportAttributes compute — policy change code flow', () => { + let config: ReportAttributesConfig; + + const report1 = { + reportID: 'r1', + policyID: 'policy1', + chatReportID: undefined, + participants: {}, + } as unknown as Report; + + const report2 = { + reportID: 'r2', + policyID: 'policy2', + chatReportID: undefined, + participants: {}, + } as unknown as Report; + + const reports: OnyxCollection = { + [`${ONYXKEYS.COLLECTION.REPORT}r1`]: report1, + [`${ONYXKEYS.COLLECTION.REPORT}r2`]: report2, + }; + + const policy1 = {...basePolicy, id: 'policy1'} as unknown as Policy; + const policy2 = {...basePolicy, id: 'policy2'} as unknown as Policy; + + const policies: OnyxCollection = { + [`${ONYXKEYS.COLLECTION.POLICY}policy1`]: policy1, + [`${ONYXKEYS.COLLECTION.POLICY}policy2`]: policy2, + }; + + beforeEach(() => { + jest.resetModules(); + // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-member-access + config = (require('@userActions/OnyxDerived/configs/reportAttributes') as {default: ReportAttributesConfig}).default; + }); + + const buildArgs = (overridePolicies?: OnyxCollection) => + [ + reports, // reports + null, // preferredLocale + null, // transactionViolations + null, // reportActions + null, // reportNameValuePairs + null, // transactions + null, // personalDetails + null, // session + overridePolicies ?? policies, // policies + null, // policyTags + null, // reportViolations + null, // reportMetadata + ] as unknown as Parameters[0]; + + it('triggers full recompute when policies are loaded for the first time', () => { + const result = config.compute(buildArgs(), { + currentValue: undefined, + sourceValues: {[ONYXKEYS.COLLECTION.POLICY]: policies as never}, + }); + + expect(result?.reports).toHaveProperty('r1'); + expect(result?.reports).toHaveProperty('r2'); + }); + + it('only recomputes reports for the changed policy when a tracked field changes', () => { + // Seed previousPolicies by doing an initial compute + config.compute(buildArgs(), { + currentValue: undefined, + sourceValues: {[ONYXKEYS.COLLECTION.POLICY]: policies as never}, + }); + + const policy1Changed = {...policy1, approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL} as unknown as Policy; + const updatedPolicies: OnyxCollection = { + ...policies, + [`${ONYXKEYS.COLLECTION.POLICY}policy1`]: policy1Changed, + }; + + const existingValue: ReportAttributesDerivedValue = { + reports: { + r1: {reportName: 'Old Name 1', isEmpty: false, brickRoadStatus: undefined, requiresAttention: false, reportErrors: {}}, + r2: {reportName: 'Old Name 2', isEmpty: false, brickRoadStatus: undefined, requiresAttention: false, reportErrors: {}}, + }, + locale: null, + }; + + const computeReportName = (jest.requireMock('@libs/ReportNameUtils') as unknown as {computeReportName: jest.Mock}).computeReportName; + computeReportName.mockReturnValue('New Name'); + + const result = config.compute(buildArgs(updatedPolicies), { + currentValue: existingValue, + sourceValues: {[ONYXKEYS.COLLECTION.POLICY]: {[`${ONYXKEYS.COLLECTION.POLICY}policy1`]: policy1Changed} as never}, + }); + + // r1 (policy1 changed) should be recomputed with new name + expect(result?.reports.r1?.reportName).toBe('New Name'); + // r2 (policy2 unchanged) should keep its value from currentValue + expect(result?.reports.r2?.reportName).toBe('Old Name 2'); + }); + + it('skips recompute when a non-tracked policy field changes', () => { + // Seed previousPolicies + config.compute(buildArgs(), { + currentValue: undefined, + sourceValues: {[ONYXKEYS.COLLECTION.POLICY]: policies as never}, + }); + + const policy1WithNameChange = {...policy1, name: 'New Policy Name'} as unknown as Policy; + const updatedPolicies: OnyxCollection = { + ...policies, + [`${ONYXKEYS.COLLECTION.POLICY}policy1`]: policy1WithNameChange, + }; + + const existingValue: ReportAttributesDerivedValue = { + reports: { + r1: {reportName: 'Existing r1', isEmpty: false, brickRoadStatus: undefined, requiresAttention: false, reportErrors: {}}, + r2: {reportName: 'Existing r2', isEmpty: false, brickRoadStatus: undefined, requiresAttention: false, reportErrors: {}}, + }, + locale: null, + }; + + const result = config.compute(buildArgs(updatedPolicies), { + currentValue: existingValue, + sourceValues: {[ONYXKEYS.COLLECTION.POLICY]: {[`${ONYXKEYS.COLLECTION.POLICY}policy1`]: policy1WithNameChange} as never}, + }); + + // No tracked fields changed → return currentValue unchanged + expect(result).toEqual(existingValue); + }); +}); From 7974f03e1d2c948174412137d225e4569d6d7904 Mon Sep 17 00:00:00 2001 From: Hubert Sosinski Date: Fri, 20 Mar 2026 11:57:46 +0100 Subject: [PATCH 5/5] tests for hasPolicyRelevantFieldChanged in reportAttributesTest.ts --- tests/unit/reportAttributesTest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/reportAttributesTest.ts b/tests/unit/reportAttributesTest.ts index c81532ff33580..ad5831ddb7799 100644 --- a/tests/unit/reportAttributesTest.ts +++ b/tests/unit/reportAttributesTest.ts @@ -23,7 +23,7 @@ jest.mock('@libs/ReportUtils', () => ({ })); jest.mock('@libs/SidebarUtils', () => ({ - // eslint-disable-next-line @typescript-eslint/naming-convention + // eslint-disable-next-line @typescript-eslint/naming-convention -- Required because Jest mock objects use __esModule which violates the naming convention rule __esModule: true, default: { getReasonAndReportActionThatHasRedBrickRoad: jest.fn(() => undefined), @@ -157,7 +157,7 @@ describe('reportAttributes compute — policy change code flow', () => { beforeEach(() => { jest.resetModules(); - // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-member-access + // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-member-access -- Dynamic require is needed here because jest.resetModules() clears the module registry and we need a fresh import config = (require('@userActions/OnyxDerived/configs/reportAttributes') as {default: ReportAttributesConfig}).default; });