Skip to content

Commit

Permalink
Adding case for OpenReport when creating a thread
Browse files Browse the repository at this point in the history
  • Loading branch information
gedu committed Oct 16, 2024
1 parent db367ac commit ecb7aaa
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
15 changes: 14 additions & 1 deletion src/libs/actions/RequestConflictUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {WriteCommand} from '@libs/API/types';
import {WRITE_COMMANDS} from '@libs/API/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type OnyxRequest from '@src/types/onyx/Request';
Expand Down Expand Up @@ -43,14 +44,25 @@ function resolveDuplicationConflictAction(persistedRequests: OnyxRequest[], comm

function resolveCommentDeletionConflicts(persistedRequests: OnyxRequest[], reportActionID: string, originalReportID: string): ConflictActionData {
const indices: number[] = [];
const commentCouldBeThread: Record<string, number> = {};
let addCommentFound = false;

persistedRequests.forEach((request, index) => {
// If the request will open a Thread, we should not delete the comment and we should send all the requests
if (request.command === WRITE_COMMANDS.OPEN_REPORT && request.data?.parentReportActionID === reportActionID && reportActionID in commentCouldBeThread) {
const indexToRemove = commentCouldBeThread[reportActionID];
indices.splice(indexToRemove, 1);
return;
}

if (!commentsToBeDeleted.has(request.command) || request.data?.reportActionID !== reportActionID) {
return;
}

// If we find a new message, we probably want to remove it and not perform any request given that the server
// doesn't know about it yet.
if (addNewMessage.has(request.command)) {
addCommentFound = true;
commentCouldBeThread[reportActionID] = index;
}
indices.push(index);
});
Expand All @@ -64,6 +76,7 @@ function resolveCommentDeletionConflicts(persistedRequests: OnyxRequest[], repor
}

if (addCommentFound) {
// The new message performs some changes in Onyx, so we need to rollback those changes.
const rollbackData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down
80 changes: 67 additions & 13 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,7 @@ describe('actions/Report', () => {
const reportAction = TestHelper.buildTestReportComment(created, TEST_USER_ACCOUNT_ID, reportActionID);
Report.editReportComment(REPORT_ID, reportAction, 'Testing an edited comment');

// wait for Onyx.connect execute the callback and start processing the queue
await Promise.resolve();
await waitForBatchedUpdates();

await new Promise<void>((resolve) => {
const connection = Onyx.connect({
Expand All @@ -795,12 +794,13 @@ describe('actions/Report', () => {
});
});

// Checking the Report Action exists before dleting it
// Checking the Report Action exists before deleting it
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`,
callback: (reportActions) => {
Onyx.disconnect(connection);

expect(reportActions?.[reportActionID]).not.toBeNull();
expect(reportActions?.[reportActionID].reportActionID).toBe(reportActionID);
resolve();
Expand Down Expand Up @@ -859,9 +859,7 @@ describe('actions/Report', () => {
key: ONYXKEYS.PERSISTED_REQUESTS,
callback: (persistedRequests) => {
Onyx.disconnect(connection);

expect(persistedRequests?.at(0)?.command).toBe(WRITE_COMMANDS.UPDATE_COMMENT);

resolve();
},
});
Expand Down Expand Up @@ -909,9 +907,7 @@ describe('actions/Report', () => {
key: ONYXKEYS.PERSISTED_REQUESTS,
callback: (persistedRequests) => {
Onyx.disconnect(connection);

expect(persistedRequests?.at(0)?.command).toBe(WRITE_COMMANDS.UPDATE_COMMENT);

resolve();
},
});
Expand Down Expand Up @@ -950,21 +946,20 @@ describe('actions/Report', () => {
const reportAction = TestHelper.buildTestReportComment(created, TEST_USER_ACCOUNT_ID, reportActionID);

// wait for Onyx.connect execute the callback and start processing the queue
await Promise.resolve();
await waitForBatchedUpdates();

await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.PERSISTED_REQUESTS,
callback: (persistedRequests) => {
Onyx.disconnect(connection);

expect(persistedRequests?.at(0)?.command).toBe(WRITE_COMMANDS.ADD_ATTACHMENT);
resolve();
},
});
});

// Checking the Report Action exists before dleting it
// Checking the Report Action exists before deleting it
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`,
Expand Down Expand Up @@ -1018,21 +1013,20 @@ describe('actions/Report', () => {
const reportAction = TestHelper.buildTestReportComment(created, TEST_USER_ACCOUNT_ID, reportActionID);

// wait for Onyx.connect execute the callback and start processing the queue
await Promise.resolve();
await waitForBatchedUpdates();

await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.PERSISTED_REQUESTS,
callback: (persistedRequests) => {
Onyx.disconnect(connection);

expect(persistedRequests?.at(0)?.command).toBe(WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT);
resolve();
},
});
});

// Checking the Report Action exists before dleting it
// Checking the Report Action exists before deleting it
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`,
Expand Down Expand Up @@ -1228,4 +1222,64 @@ describe('actions/Report', () => {
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.REMOVE_EMOJI_REACTION, 0);
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.DELETE_COMMENT, 1);
});

it('should create and delete thread processing all the requests', async () => {
global.fetch = TestHelper.getGlobalFetchMock();

const TEST_USER_ACCOUNT_ID = 1;
const REPORT_ID = '1';
const TEN_MINUTES_AGO = subMinutes(new Date(), 10);
const created = format(addSeconds(TEN_MINUTES_AGO, 10), CONST.DATE.FNS_DB_FORMAT_STRING);

await Onyx.set(ONYXKEYS.NETWORK, {isOffline: true});
await waitForBatchedUpdates();

Report.addComment(REPORT_ID, 'Testing a comment');

const newComment = PersistedRequests.getAll().at(0);
const reportActionID = (newComment?.data?.reportActionID as string) ?? '-1';
const reportAction = TestHelper.buildTestReportComment(created, TEST_USER_ACCOUNT_ID, reportActionID);

Report.openReport(
REPORT_ID,
undefined,
['[email protected]'],
{
isOptimisticReport: true,
parentReportID: REPORT_ID,
parentReportActionID: reportActionID,
reportID: '2',
},
reportActionID,
);

Report.deleteReportComment(REPORT_ID, reportAction);

expect(PersistedRequests.getAll().length).toBe(3);

await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.PERSISTED_REQUESTS,
callback: (persistedRequests) => {
if (persistedRequests?.length !== 3) {
return;
}
Onyx.disconnect(connection);

expect(persistedRequests?.at(0)?.command).toBe(WRITE_COMMANDS.ADD_COMMENT);
expect(persistedRequests?.at(1)?.command).toBe(WRITE_COMMANDS.OPEN_REPORT);
expect(persistedRequests?.at(2)?.command).toBe(WRITE_COMMANDS.DELETE_COMMENT);
resolve();
},
});
});

Onyx.set(ONYXKEYS.NETWORK, {isOffline: false});
await waitForBatchedUpdates();

// Checking no requests were or will be made
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.ADD_COMMENT, 1);
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.OPEN_REPORT, 1);
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.DELETE_COMMENT, 1);
});
});

0 comments on commit ecb7aaa

Please sign in to comment.