From b3fd1082017a54200d9faa9af15d5ea2d45ad4d5 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 17 Jul 2024 01:04:04 +0800 Subject: [PATCH 1/3] simplify the condition --- src/libs/ReportUtils.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e29a2c14e3c4..f2e4133a282c 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5562,16 +5562,13 @@ function getChatByParticipants(newParticipantList: number[], reports: OnyxCollec return Object.values(reports ?? {}).find((report) => { const participantAccountIDs = Object.keys(report?.participants ?? {}); - // If the report has been deleted, or there are no participants (like an empty #admins room) then skip it - if ( - participantAccountIDs.length === 0 || - isChatThread(report) || - isTaskReport(report) || - isMoneyRequestReport(report) || - isChatRoom(report) || - isPolicyExpenseChat(report) || - (isGroupChat(report) && !shouldIncludeGroupChats) - ) { + // Skip if it's not a 1:1 chat + if (!shouldIncludeGroupChats && !isOneOnOneChat(report)) { + return false; + } + + // If we are looking for a group chat, then skip non-group chat report + if (shouldIncludeGroupChats && !isGroupChat(report)) { return false; } From eeed464755b9aa39197448d78ba452d3b0da8dbd Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 17 Jul 2024 01:04:09 +0800 Subject: [PATCH 2/3] added test --- tests/unit/ReportUtilsTest.ts | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 34a9a923bd61..89e44de43ee3 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -970,4 +970,60 @@ describe('ReportUtils', () => { expect(ReportUtils.isChatUsedForOnboarding(report2)).toBeFalsy(); }); }); + + describe('getChatByParticipants', () => { + const userAccountID = 1; + const userAccountID2 = 2; + let oneOnOneChatReport: Report; + let groupChatReport: Report; + + beforeAll(() => { + const invoiceReport: Report = { + reportID: '1', + type: CONST.REPORT.TYPE.INVOICE, + participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + }; + const taskReport: Report = { + reportID: '2', + type: CONST.REPORT.TYPE.TASK, + participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + }; + const iouReport: Report = { + reportID: '3', + type: CONST.REPORT.TYPE.IOU, + participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + }; + groupChatReport = { + reportID: '4', + type: CONST.REPORT.TYPE.CHAT, + chatType: CONST.REPORT.CHAT_TYPE.GROUP, + participants: {[userAccountID]: {hidden: false}, [userAccountID2]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + }; + oneOnOneChatReport = { + reportID: '5', + type: CONST.REPORT.TYPE.CHAT, + participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + }; + const reportCollectionDataSet = toCollectionDataSet( + ONYXKEYS.COLLECTION.REPORT, + [invoiceReport, taskReport, iouReport, groupChatReport, oneOnOneChatReport], + (item) => item.reportID, + ); + return Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, reportCollectionDataSet); + }); + it('should return the 1:1 chat', async () => { + const report = ReportUtils.getChatByParticipants([currentUserAccountID, userAccountID]); + expect(report?.reportID).toEqual(oneOnOneChatReport.reportID); + }); + + it('should return the group chat', () => { + const report = ReportUtils.getChatByParticipants([currentUserAccountID, userAccountID, userAccountID2], undefined, true); + expect(report?.reportID).toEqual(groupChatReport.reportID); + }); + + it('should return undefined when no report is found', () => { + const report = ReportUtils.getChatByParticipants([currentUserAccountID, userAccountID2], undefined); + expect(report).toEqual(undefined); + }); + }); }); From c2bb1d377fd777d4cf8c24c33a8fc02d5133919a Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 17 Jul 2024 01:17:20 +0800 Subject: [PATCH 3/3] remove useless async --- tests/unit/ReportUtilsTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 89e44de43ee3..5f3f36ce61be 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -1011,7 +1011,7 @@ describe('ReportUtils', () => { ); return Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, reportCollectionDataSet); }); - it('should return the 1:1 chat', async () => { + it('should return the 1:1 chat', () => { const report = ReportUtils.getChatByParticipants([currentUserAccountID, userAccountID]); expect(report?.reportID).toEqual(oneOnOneChatReport.reportID); });