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
16 changes: 16 additions & 0 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5671,6 +5671,22 @@ function buildOptimisticChangePolicyData(report: Report, policyID: string, repor
},
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have also optimistically added the policy's data to the search snapshot if the policy doesn't exist yet in the snapshot. Otherwise this can cause issues with canApprove always returning false, which prevents the Approve button from showing.

// 5. Make sure the expense report is not archived
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`,
value: {
private_isArchived: null,
},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`,
value: {
private_isArchived: DateUtils.getDBTime(),
},
});

return {optimisticData, successData, failureData, optimisticReportPreviewAction, optimisticMovedReportAction};
}

Expand Down
71 changes: 71 additions & 0 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,77 @@ describe('actions/Report', () => {
});
});

describe('changeReportPolicy', () => {
it('should unarchive the expense report', async () => {
// Given an archived expense report
const expenseReport: OnyxTypes.Report = {
...createRandomReport(1),
type: CONST.REPORT.TYPE.EXPENSE,
policyID: '1',
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${expenseReport.reportID}`, {
private_isArchived: DateUtils.getDBTime(),
});

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

// Then the expense report should not be archived anymore
const isArchived = await new Promise<boolean>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${expenseReport.reportID}`,
callback: (val) => {
resolve(!!val?.private_isArchived);
Onyx.disconnect(connection);
},
});
});
expect(isArchived).toBe(false);
});
});

describe('changeReportPolicyAndInviteSubmitter', () => {
it('should unarchive the expense report', async () => {
// Given an archived expense report
const ownerAccountID = 1;
const ownerEmail = 'owner@gmail.com';
const adminEmail = 'admin@gmail.com';
const expenseReport: OnyxTypes.Report = {
...createRandomReport(1),
type: CONST.REPORT.TYPE.EXPENSE,
policyID: '1',
ownerAccountID,
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${expenseReport.reportID}`, {
private_isArchived: DateUtils.getDBTime(),
});
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {
[ownerAccountID]: {
login: ownerEmail,
},
});

// When moving to another workspace
Report.changeReportPolicyAndInviteSubmitter(expenseReport, '2', {
[adminEmail]: {role: CONST.POLICY.ROLE.ADMIN},
});
await waitForBatchedUpdates();

// Then the expense report should not be archived anymore
const isArchived = await new Promise<boolean>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${expenseReport.reportID}`,
callback: (val) => {
resolve(!!val?.private_isArchived);
Onyx.disconnect(connection);
},
});
});
expect(isArchived).toBe(false);
});
});

describe('buildOptimisticChangePolicyData', () => {
it('should build the optimistic data next step for the change policy data', () => {
const report: OnyxTypes.Report = {
Expand Down