From db626377bf30743309ba22326b67436c36eb3aab Mon Sep 17 00:00:00 2001 From: Matheus <20505926+MattSilvaa@users.noreply.github.com> Date: Mon, 19 Aug 2024 04:19:18 -0700 Subject: [PATCH] [MM-58562] View Members search matches entire string for the Position field instead of each word (#27384) --- .../src/selectors/entities/users.test.ts | 2 + .../src/utils/user_utils.test.ts | 51 +++++++++++++++++++ .../mattermost-redux/src/utils/user_utils.ts | 3 +- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/users.test.ts b/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/users.test.ts index c414396902..f0cc3d02d4 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/users.test.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/users.test.ts @@ -28,6 +28,7 @@ describe('Selectors.Users', () => { const group2 = TestHelper.fakeGroupWithId(''); const user1 = TestHelper.fakeUserWithId(''); + user1.position = 'Software Engineer at Mattermost'; user1.notify_props = {mention_keys: 'testkey1,testkey2'} as UserProfile['notify_props']; user1.roles = 'system_admin system_user'; const user2 = TestHelper.fakeUserWithId(); @@ -402,6 +403,7 @@ describe('Selectors.Users', () => { it('searchProfilesInCurrentChannel', () => { expect(Selectors.searchProfilesInCurrentChannel(testState, user1.username)).toEqual([user1]); + expect(Selectors.searchProfilesInCurrentChannel(testState, 'engineer at mattermost')).toEqual([user1]); expect(Selectors.searchProfilesInCurrentChannel(testState, user1.username, true)).toEqual([]); }); diff --git a/webapp/channels/src/packages/mattermost-redux/src/utils/user_utils.test.ts b/webapp/channels/src/packages/mattermost-redux/src/utils/user_utils.test.ts index adbc22d1af..92d07f9c4a 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/utils/user_utils.test.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/utils/user_utils.test.ts @@ -9,6 +9,7 @@ import { getSuggestionsSplitByMultiple, includesAnAdminRole, applyRolesFilters, + nameSuggestionsForUser, } from 'mattermost-redux/utils/user_utils'; import TestHelper from '../../test/test_helper'; @@ -59,6 +60,56 @@ describe('user utils', () => { }); }); + describe('nameSuggestionsForUser', () => { + const userObj = TestHelper.getUserMock({ + username: 'test.user', + nickname: 'tester', + first_name: 'Test', + last_name: 'UsEr NaMe', + position: 'Software Engineer at Mattermost', + email: 'test.user_name@example.com', + }); + + it('should return correct suggestions for user', () => { + const suggestions = nameSuggestionsForUser(userObj); + const expectedSuggestions = [ + 'test.user', '.user', 'user', + 'test', 'user name', 'test user name', 'tester', + 'software engineer at mattermost', 'engineer at mattermost', 'at mattermost', 'mattermost', + 'test.user_name@example.com', 'example.com', + ]; + expect(suggestions).toEqual(expectedSuggestions); + }); + + it('should gracefully handle missing values for fields', () => { + const suggestions: string[] = nameSuggestionsForUser({...userObj, + username: '', + nickname: '', + first_name: '', + last_name: '', + position: '', + email: '', + }); + expect(suggestions).toEqual(expect.arrayContaining([''])); + }); + + it('should handle different split username characters correctly', () => { + const suggestions: string[] = nameSuggestionsForUser({...userObj, username: 'john-doe_jr'}); + const expectedUsernameSuggestions: string[] = [ + 'john-doe_jr', '-doe_jr', 'doe_jr', '_jr', 'jr', + ]; + expect(suggestions).toEqual(expect.arrayContaining(expectedUsernameSuggestions)); + }); + + it('should split position on whitespace', () => { + const suggestions: string[] = nameSuggestionsForUser({...userObj, position: 'test-position split is corre_ct'}); + const expectedPositionSuggestions: string[] = [ + 'test-position split is corre_ct', 'split is corre_ct', 'is corre_ct', 'corre_ct', + ]; + expect(suggestions).toEqual(expect.arrayContaining(expectedPositionSuggestions)); + }); + }); + describe('filterProfilesStartingWithTerm', () => { const userA = TestHelper.getUserMock({ id: '100', diff --git a/webapp/channels/src/packages/mattermost-redux/src/utils/user_utils.ts b/webapp/channels/src/packages/mattermost-redux/src/utils/user_utils.ts index 8dd9a86eb0..0a54aa6052 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/utils/user_utils.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/utils/user_utils.ts @@ -158,7 +158,8 @@ export function nameSuggestionsForUser(user: UserProfile): string[] { const full = first + ' ' + last; profileSuggestions.push(first, last, full); profileSuggestions.push((user.nickname || '').toLowerCase()); - profileSuggestions.push((user.position || '').toLowerCase()); + const positionSuggestions = getSuggestionsSplitBy((user.position || '').toLowerCase(), ' '); + profileSuggestions.push(...positionSuggestions); const email = (user.email || '').toLowerCase(); profileSuggestions.push(email);