From 41da61bcd376094c3a2d743f794a982d657df3bf Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Mon, 20 Jan 2025 15:55:27 +0100 Subject: [PATCH 1/4] add workspace chats mock, basic tests --- src/libs/OptionsListUtils.ts | 1 + tests/unit/OptionsListUtilsTest.ts | 58 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 7ef893dca7e1..4f87343c005c 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -2080,6 +2080,7 @@ export { getAlternateText, hasReportErrors, combineOrderingOfReportsAndPersonalDetails, + filterWorkspaceChats, }; export type {Section, SectionBase, MemberForList, Options, OptionList, SearchOption, PayeePersonalDetails, Option, OptionTree, ReportAndPersonalDetailOptions, GetUserToInviteConfig}; diff --git a/tests/unit/OptionsListUtilsTest.ts b/tests/unit/OptionsListUtilsTest.ts index 6c0ad88619cb..b2d6880f72a1 100644 --- a/tests/unit/OptionsListUtilsTest.ts +++ b/tests/unit/OptionsListUtilsTest.ts @@ -157,6 +157,49 @@ describe('OptionsListUtils', () => { }, }; + const WORKSPACE_CHATS: ReportUtils.OptionData[] = [ + { + reportID: '1', + text: 'John Doe', + }, + { + reportID: '2', + text: 'Jane Doe', + }, + { + reportID: '3', + text: 'Alice Johnson', + }, + { + reportID: '4', + text: 'Bob Johnson', + }, + { + reportID: '5', + text: 'Charlie Davis', + }, + { + reportID: '6', + text: 'Emily Brown', + }, + { + reportID: '7', + text: 'George Brown', + }, + { + reportID: '8', + text: 'Helen Martinez', + }, + { + reportID: '9', + text: 'Ian Clark', + }, + { + reportID: '10', + text: 'Laura Clark', + }, + ]; + // And a set of personalDetails some with existing reports and some without const PERSONAL_DETAILS: PersonalDetailsList = { // These exist in our reports @@ -1037,4 +1080,19 @@ describe('OptionsListUtils', () => { expect(canCreate).toBe(false); }); }); + + describe('filterWorkspaceChats', () => { + it('should return an empty array if there are no workspace chats', () => { + const result = OptionsListUtils.filterWorkspaceChats([], []); + + expect(result.length).toEqual(0); + }); + + it('should return all chats if there are no search terms', () => { + const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, []); + + expect(result).toEqual(WORKSPACE_CHATS); + expect(result.length).toEqual(WORKSPACE_CHATS.length); + }); + }); }); From 89f998fb48395d2ec74214a00117cc1a1ff2e399 Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Mon, 20 Jan 2025 17:03:58 +0100 Subject: [PATCH 2/4] add more tests --- tests/unit/OptionsListUtilsTest.ts | 46 +++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/unit/OptionsListUtilsTest.ts b/tests/unit/OptionsListUtilsTest.ts index b2d6880f72a1..1052e3a8d544 100644 --- a/tests/unit/OptionsListUtilsTest.ts +++ b/tests/unit/OptionsListUtilsTest.ts @@ -160,43 +160,43 @@ describe('OptionsListUtils', () => { const WORKSPACE_CHATS: ReportUtils.OptionData[] = [ { reportID: '1', - text: 'John Doe', + text: 'Google Workspace', }, { reportID: '2', - text: 'Jane Doe', + text: 'Google Drive Workspace', }, { reportID: '3', - text: 'Alice Johnson', + text: 'Slack Team Workspace', }, { reportID: '4', - text: 'Bob Johnson', + text: 'Slack Development Workspace', }, { reportID: '5', - text: 'Charlie Davis', + text: 'Microsoft Teams Workspace', }, { reportID: '6', - text: 'Emily Brown', + text: 'Microsoft Project Workspace', }, { reportID: '7', - text: 'George Brown', + text: 'Notion Design Workspace', }, { reportID: '8', - text: 'Helen Martinez', + text: 'Notion Workspace for Marketing', }, { reportID: '9', - text: 'Ian Clark', + text: 'Asana Task Workspace', }, { reportID: '10', - text: 'Laura Clark', + text: 'Asana Project Management', }, ]; @@ -1088,11 +1088,35 @@ describe('OptionsListUtils', () => { expect(result.length).toEqual(0); }); - it('should return all chats if there are no search terms', () => { + it('should return all workspace chats if there are no search terms', () => { const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, []); expect(result).toEqual(WORKSPACE_CHATS); expect(result.length).toEqual(WORKSPACE_CHATS.length); }); + + it('should filter multiple workspace chats by single search term', () => { + const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, ['Google']); + + expect(result.length).toEqual(2); + }); + + it('should filter multiple workspace chats by two search terms', () => { + const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, ['Google', 'Workspace']); + + expect(result.length).toEqual(2); + }); + + it('should filter workspace chat by exact name', () => { + const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, ['Microsoft', 'Teams', 'Workspace']); + + expect(result.length).toEqual(1); + }); + + it('should return an empty array if there are no matching workspace chats', () => { + const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, ['XYZ']); + + expect(result.length).toEqual(0); + }); }); }); From e13fecf50f4e47fa0e6db9c5c90c210b7cbe2fe7 Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Tue, 21 Jan 2025 16:25:27 +0100 Subject: [PATCH 3/4] add unit tests for orderWorkspaceOptions util function --- src/libs/OptionsListUtils.ts | 1 + tests/unit/OptionsListUtilsTest.ts | 31 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 4f87343c005c..46fc5c11e1bc 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -2081,6 +2081,7 @@ export { hasReportErrors, combineOrderingOfReportsAndPersonalDetails, filterWorkspaceChats, + orderWorkspaceOptions, }; export type {Section, SectionBase, MemberForList, Options, OptionList, SearchOption, PayeePersonalDetails, Option, OptionTree, ReportAndPersonalDetailOptions, GetUserToInviteConfig}; diff --git a/tests/unit/OptionsListUtilsTest.ts b/tests/unit/OptionsListUtilsTest.ts index 1052e3a8d544..1dd297d17429 100644 --- a/tests/unit/OptionsListUtilsTest.ts +++ b/tests/unit/OptionsListUtilsTest.ts @@ -157,46 +157,68 @@ describe('OptionsListUtils', () => { }, }; + const activePolicyID = 'DEF456'; + const WORKSPACE_CHATS: ReportUtils.OptionData[] = [ { reportID: '1', text: 'Google Workspace', + policyID: '11', + isPolicyExpenseChat: true, }, { reportID: '2', text: 'Google Drive Workspace', + policyID: '22', + isPolicyExpenseChat: false, }, { reportID: '3', text: 'Slack Team Workspace', + policyID: '33', + isPolicyExpenseChat: false, }, { reportID: '4', text: 'Slack Development Workspace', + policyID: '44', + isPolicyExpenseChat: true, }, { reportID: '5', text: 'Microsoft Teams Workspace', + policyID: '55', + isPolicyExpenseChat: false, }, { reportID: '6', text: 'Microsoft Project Workspace', + policyID: '66', + isPolicyExpenseChat: false, }, { reportID: '7', text: 'Notion Design Workspace', + policyID: '77', + isPolicyExpenseChat: false, }, { reportID: '8', text: 'Notion Workspace for Marketing', + policyID: activePolicyID, + isPolicyExpenseChat: true, }, { reportID: '9', text: 'Asana Task Workspace', + policyID: '99', + isPolicyExpenseChat: false, }, { reportID: '10', text: 'Asana Project Management', + policyID: '1010', + isPolicyExpenseChat: true, }, ]; @@ -423,6 +445,7 @@ describe('OptionsListUtils', () => { total: 1000, }, [`${ONYXKEYS.COLLECTION.POLICY}${policyID}` as const]: POLICY, + [ONYXKEYS.NVP_ACTIVE_POLICY_ID]: activePolicyID, }, }); Onyx.registerLogger(() => {}); @@ -1119,4 +1142,12 @@ describe('OptionsListUtils', () => { expect(result.length).toEqual(0); }); }); + + describe('orderWorkspaceOptions', () => { + it('should put the default workspace on top of the list', () => { + const result = OptionsListUtils.orderWorkspaceOptions(WORKSPACE_CHATS); + + expect(result.at(0)?.text).toEqual('Notion Workspace for Marketing'); + }); + }); }); From fdfe33889deba8ab78aed1e766489026e70c9683 Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Wed, 22 Jan 2025 11:51:07 +0100 Subject: [PATCH 4/4] add unit tests for filterSelfDMChat --- src/libs/OptionsListUtils.ts | 1 + tests/unit/OptionsListUtilsTest.ts | 98 +++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 46fc5c11e1bc..1a6a60ac79e0 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -2082,6 +2082,7 @@ export { combineOrderingOfReportsAndPersonalDetails, filterWorkspaceChats, orderWorkspaceOptions, + filterSelfDMChat, }; export type {Section, SectionBase, MemberForList, Options, OptionList, SearchOption, PayeePersonalDetails, Option, OptionTree, ReportAndPersonalDetailOptions, GetUserToInviteConfig}; diff --git a/tests/unit/OptionsListUtilsTest.ts b/tests/unit/OptionsListUtilsTest.ts index 1dd297d17429..8acc6d17cf43 100644 --- a/tests/unit/OptionsListUtilsTest.ts +++ b/tests/unit/OptionsListUtilsTest.ts @@ -1118,18 +1118,12 @@ describe('OptionsListUtils', () => { expect(result.length).toEqual(WORKSPACE_CHATS.length); }); - it('should filter multiple workspace chats by single search term', () => { + it('should filter multiple workspace chats by search term', () => { const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, ['Google']); expect(result.length).toEqual(2); }); - it('should filter multiple workspace chats by two search terms', () => { - const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, ['Google', 'Workspace']); - - expect(result.length).toEqual(2); - }); - it('should filter workspace chat by exact name', () => { const result = OptionsListUtils.filterWorkspaceChats(WORKSPACE_CHATS, ['Microsoft', 'Teams', 'Workspace']); @@ -1150,4 +1144,94 @@ describe('OptionsListUtils', () => { expect(result.at(0)?.text).toEqual('Notion Workspace for Marketing'); }); }); + + describe('filterSelfDMChat', () => { + const REPORT = { + reportID: '1', + text: 'Google Workspace', + policyID: '11', + isPolicyExpenseChat: true, + }; + const LOGIN = 'johndoe@test.com'; + const ALTERNATE_TEXT = 'John William Doe'; + const SUBTITLE = 'Software Engineer'; + + it('should return the report when there are no search terms', () => { + const result = OptionsListUtils.filterSelfDMChat(REPORT, []); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should return undefined, when the search term does not match the report', () => { + const result = OptionsListUtils.filterSelfDMChat(REPORT, ['XYZ']); + + expect(result).toBeUndefined(); + }); + + it('should filter report by text', () => { + const result = OptionsListUtils.filterSelfDMChat(REPORT, ['Google']); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should filter report by exact text', () => { + const result = OptionsListUtils.filterSelfDMChat(REPORT, ['Google', 'Workspace']); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should filter report by login', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, login: LOGIN}, ['john']); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should filter report by exact login', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, login: LOGIN}, [LOGIN]); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should filter report by alternate text', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, alternateText: ALTERNATE_TEXT, isThread: true}, ['William']); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should filter report by exact alternate text', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, alternateText: ALTERNATE_TEXT, isThread: true}, ['John', 'William', 'Doe']); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should filter report by alternate text if it is not a thread', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, alternateText: ALTERNATE_TEXT, isThread: false}, ['William']); + + expect(result?.reportID).toBeUndefined(); + }); + + it('should filter report by subtitle', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, subtitle: SUBTITLE}, ['Software']); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should filter report by exact subtitle', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, subtitle: SUBTITLE}, ['Software', 'Engineer']); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + + it('should not filter report by subtitle if it is not an expense chat nor a chat room', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, subtitle: SUBTITLE, isPolicyExpenseChat: false, isChatRoom: false}, ['Software']); + + expect(result).toBeUndefined(); + }); + + it('should filter report by subtitle if it is a chat room', () => { + const result = OptionsListUtils.filterSelfDMChat({...REPORT, subtitle: SUBTITLE, isPolicyExpenseChat: false, isChatRoom: true}, ['Software']); + + expect(result?.reportID).toEqual(REPORT.reportID); + }); + }); });