Skip to content

Commit ecd8ba7

Browse files
authored
Merge pull request Expensify#68482 from FitseTLT/fix-snapshot-data-missing-new-policy-when-change-workspace
Fix - Reports - Approver button is missing in Reports while offline
2 parents b7b6678 + 24b12df commit ecd8ba7

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/libs/actions/Report.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ import {
160160
populateOptimisticReportFormula,
161161
prepareOnboardingOnyxData,
162162
} from '@libs/ReportUtils';
163+
import {getCurrentSearchQueryJSON} from '@libs/SearchQueryUtils';
163164
import shouldSkipDeepLinkNavigation from '@libs/shouldSkipDeepLinkNavigation';
164165
import playSound, {SOUNDS} from '@libs/Sound';
165166
import {isOnHold} from '@libs/TransactionUtils';
@@ -5766,6 +5767,20 @@ function buildOptimisticChangePolicyData(report: Report, policyID: string, repor
57665767
},
57675768
});
57685769

5770+
const currentSearchQueryJSON = getCurrentSearchQueryJSON();
5771+
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
5772+
5773+
// Search data might not have the new policy data so we should add it optimistically.
5774+
if (policy && currentSearchQueryJSON) {
5775+
optimisticData.push({
5776+
onyxMethod: Onyx.METHOD.MERGE,
5777+
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchQueryJSON.hash}` as const,
5778+
value: {
5779+
data: {[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: policy},
5780+
},
5781+
});
5782+
}
5783+
57695784
// 5. Make sure the expense report is not archived
57705785
optimisticData.push({
57715786
onyxMethod: Onyx.METHOD.MERGE,

tests/actions/ReportTest.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ jest.mock('@libs/ReportUtils', () => {
4444
};
4545
});
4646

47+
const currentHash = 12345;
48+
jest.mock('@src/libs/SearchQueryUtils', () => ({
49+
getCurrentSearchQueryJSON: jest.fn().mockImplementation(() => ({
50+
hash: currentHash,
51+
query: 'test',
52+
type: 'expense',
53+
status: '',
54+
flatFilters: [],
55+
})),
56+
}));
57+
4758
const UTC = 'UTC';
4859
jest.mock('@src/libs/actions/Report', () => {
4960
const originalModule = jest.requireActual<Report>('@src/libs/actions/Report');
@@ -1835,8 +1846,11 @@ describe('actions/Report', () => {
18351846
private_isArchived: DateUtils.getDBTime(),
18361847
});
18371848

1849+
const newPolicy = createRandomPolicy(2);
1850+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
1851+
18381852
// When moving to another workspace
1839-
Report.changeReportPolicy(expenseReport, '2');
1853+
Report.changeReportPolicy(expenseReport, newPolicy.id);
18401854
await waitForBatchedUpdates();
18411855

18421856
// Then the expense report should not be archived anymore
@@ -1850,6 +1864,19 @@ describe('actions/Report', () => {
18501864
});
18511865
});
18521866
expect(isArchived).toBe(false);
1867+
1868+
const snapshotData = await new Promise<OnyxEntry<OnyxTypes.SearchResults>>((resolve) => {
1869+
const connection = Onyx.connect({
1870+
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${currentHash}`,
1871+
callback: (val) => {
1872+
resolve(val);
1873+
Onyx.disconnect(connection);
1874+
},
1875+
});
1876+
});
1877+
1878+
// Then the new policy data should also be populated on the current search snapshot.
1879+
expect(snapshotData?.data?.[`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`]).toBeDefined();
18531880
});
18541881
});
18551882

0 commit comments

Comments
 (0)