Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ import {
isValidReportIDFromPath,
prepareOnboardingOnyxData,
} from '@libs/ReportUtils';
import {getCurrentSearchQueryJSON} from '@libs/SearchQueryUtils';
import shouldSkipDeepLinkNavigation from '@libs/shouldSkipDeepLinkNavigation';
import playSound, {SOUNDS} from '@libs/Sound';
import {isOnHold} from '@libs/TransactionUtils';
Expand Down Expand Up @@ -5766,6 +5767,20 @@ function buildOptimisticChangePolicyData(report: Report, policyID: string, repor
},
});

const currentSearchQueryJSON = getCurrentSearchQueryJSON();
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];

// Search data might not have the new policy data so we should add it optimistically.
if (policy && currentSearchQueryJSON) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchQueryJSON.hash}` as const,
value: {
data: {[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: policy},
},
});
}

// 5. Make sure the expense report is not archived
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
Expand Down
29 changes: 28 additions & 1 deletion tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ jest.mock('@libs/ReportUtils', () => {
};
});

const currentHash = 12345;
jest.mock('@src/libs/SearchQueryUtils', () => ({
getCurrentSearchQueryJSON: jest.fn().mockImplementation(() => ({
hash: currentHash,
query: 'test',
type: 'expense',
status: '',
flatFilters: [],
})),
}));

const UTC = 'UTC';
jest.mock('@src/libs/actions/Report', () => {
const originalModule = jest.requireActual<Report>('@src/libs/actions/Report');
Expand Down Expand Up @@ -1835,8 +1846,11 @@ describe('actions/Report', () => {
private_isArchived: DateUtils.getDBTime(),
});

const newPolicy = createRandomPolicy(2);
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);

// When moving to another workspace
Report.changeReportPolicy(expenseReport, '2');
Report.changeReportPolicy(expenseReport, newPolicy.id);
await waitForBatchedUpdates();

// Then the expense report should not be archived anymore
Expand All @@ -1850,6 +1864,19 @@ describe('actions/Report', () => {
});
});
expect(isArchived).toBe(false);

const snapshotData = await new Promise<OnyxEntry<OnyxTypes.SearchResults>>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${currentHash}`,
callback: (val) => {
resolve(val);
Onyx.disconnect(connection);
},
});
});

// Then the new policy data should also be populated on the current search snapshot.
expect(snapshotData?.data?.[`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`]).toBeDefined();
});
});

Expand Down