Skip to content
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

Fix can't interact with workspace item that is previously failed to delete #54278

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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CreateWorkspaceFromIOUPaymentParams = {
customUnitRateID: string;
iouReportID: string;
memberData: string;
reportActionID: string;
reportActionID: string | undefined;
};

export default CreateWorkspaceFromIOUPaymentParams;
11 changes: 5 additions & 6 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
avatarURL: policy?.avatarURL,
pendingAction: null,
},
},
];
Expand Down Expand Up @@ -2622,14 +2623,15 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry<Report>): WorkspaceF
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
value: {[reportPreview?.reportActionID]: null},
value: {[reportPreview.reportActionID]: null},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
value: {[reportPreview?.reportActionID]: reportPreview},
value: {[reportPreview.reportActionID]: reportPreview},
});
}

// To optimistically remove the GBR from the DM we need to update the hasOutstandingChildRequest param to false
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -2664,13 +2666,10 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry<Report>): WorkspaceF
},
},
});
}

if (reportPreview?.reportActionID) {
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${memberData.workspaceChatReportID}`,
value: {[reportPreview?.reportActionID]: null},
value: {[reportPreview.reportActionID]: null},
});
}

Expand Down
68 changes: 68 additions & 0 deletions tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy as PolicyType, Report, ReportAction, ReportActions} from '@src/types/onyx';
import type {Participant} from '@src/types/onyx/Report';
import createRandomPolicy from '../utils/collections/policies';
import createRandomReport from '../utils/collections/reports';
import * as TestHelper from '../utils/TestHelper';
import type {MockFetch} from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
Expand All @@ -25,8 +26,10 @@ describe('actions/Policy', () => {
});
});

let mockFetch: MockFetch;
beforeEach(() => {
global.fetch = TestHelper.getGlobalFetchMock();
mockFetch = fetch as MockFetch;
return Onyx.clear().then(waitForBatchedUpdates);
});

Expand Down Expand Up @@ -222,4 +225,69 @@ describe('actions/Policy', () => {
expect(policy?.autoReportingFrequency).toBe(autoReportingFrequency);
});
});

describe('deleteWorkspace', () => {
it('should apply failure data when deleteWorkspace fails', async () => {
// Given a policy
const fakePolicy = createRandomPolicy(0);
const fakeReport = {
...createRandomReport(0),
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
policyName: fakePolicy.name,
};
const fakeReimbursementAccount = {errors: {}};
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport);
await Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, fakeReimbursementAccount);

// When deleting a workspace fails
mockFetch?.fail?.();
Policy.deleteWorkspace(fakePolicy.id, fakePolicy.name);

await waitForBatchedUpdates();

// Then it should apply the correct failure data
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
callback: (policy) => {
Onyx.disconnect(connection);
expect(policy?.pendingAction).toBeUndefined();
expect(policy?.avatarURL).toBe(fakePolicy.avatarURL);
resolve();
},
});
});

// Unarchive the report
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`,
callback: (report) => {
Onyx.disconnect(connection);
expect(report?.stateNum).toBe(fakeReport.stateNum);
expect(report?.statusNum).toBe(fakeReport.statusNum);
expect(report?.policyName).toBe(fakeReport.policyName);
expect(report?.oldPolicyName).toBe(fakePolicy.name);
expect(report?.private_isArchived).toBeUndefined();
resolve();
},
});
});

// Restore the reimbursement account errors
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
callback: (reimbursementAccount) => {
Onyx.disconnect(connection);
expect(reimbursementAccount?.errors).not.toBeUndefined();
resolve();
},
});
});
});
});
});
Loading