-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fix LHN report badge reactivity to policy field changes #85523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NikkiWines
merged 8 commits into
Expensify:main
from
callstack-internal:fix-report-attributes-policy-change-reactivity
Mar 20, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a22e976
fix report badge reactivity on policy field changes
sosek108 772b30b
Merge branch 'main' into fix-report-attributes-policy-change-reactivity
sosek108 4cc0b9b
Merge branch 'main' into fix-report-attributes-policy-change-reactivity
sosek108 14579a3
only badges are handled when policy is changed
sosek108 e4802df
only badges are handled when policy is changed
sosek108 5c5e82b
Merge branch 'main' into fix-report-attributes-policy-change-reactivity
sosek108 f7e2052
tests for hasPolicyRelevantFieldChanged in reportAttributesTest.ts
sosek108 7974f03
tests for hasPolicyRelevantFieldChanged in reportAttributesTest.ts
sosek108 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 -- Required because Jest mock objects use __esModule which violates the naming convention rule | ||
| __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<Report> = { | ||
| [`${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<Policy> = { | ||
| [`${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 -- 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; | ||
| }); | ||
|
|
||
| const buildArgs = (overridePolicies?: OnyxCollection<Policy>) => | ||
| [ | ||
| 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<ReportAttributesConfig['compute']>[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<Policy> = { | ||
| ...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<Policy> = { | ||
| ...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); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.