-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Blank selected department when selecting out of range (#33386)
- Loading branch information
Showing
8 changed files
with
175 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@rocket.chat/meteor": minor | ||
--- | ||
|
||
Fixes the departments filter on the omnichannel current chats page by ensuring that the selected department is fetched and | ||
added if it was not part of the initial department list. This prevents the filter from becoming blank and avoids potential | ||
UX issues. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
apps/meteor/client/components/Omnichannel/Definitions/DepartmentsDefinitions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type DepartmentListItem = { | ||
_id: string; | ||
label: string; | ||
value: string; | ||
}; |
111 changes: 111 additions & 0 deletions
111
apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
|
||
import { useDepartmentsList } from './useDepartmentsList'; | ||
|
||
const initialDepartmentsListMock = Array.from(Array(25)).map((_, index) => { | ||
return { | ||
_id: `${index}`, | ||
name: `test_department_${index}`, | ||
enabled: true, | ||
email: `test${index}@email.com`, | ||
showOnRegistration: false, | ||
showOnOfflineForm: false, | ||
type: 'd', | ||
_updatedAt: '2024-09-26T20:05:31.330Z', | ||
offlineMessageChannelName: '', | ||
numAgents: 0, | ||
ancestors: undefined, | ||
parentId: undefined, | ||
}; | ||
}); | ||
|
||
it('should not fetch and add selected department if it is already in the departments list on first fetch', async () => { | ||
const selectedDepartmentMappedToOption = { | ||
_id: '5', | ||
label: 'test_department_5', | ||
value: '5', | ||
}; | ||
|
||
const getDepartmentByIdCallback = jest.fn(); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useDepartmentsList({ | ||
filter: '', | ||
onlyMyDepartments: true, | ||
haveAll: true, | ||
showArchived: true, | ||
selectedDepartment: '5', | ||
}), | ||
{ | ||
legacyRoot: true, | ||
wrapper: mockAppRoot() | ||
.withEndpoint('GET', '/v1/livechat/department', () => ({ | ||
count: 25, | ||
offset: 0, | ||
total: 25, | ||
departments: initialDepartmentsListMock, | ||
})) | ||
.withEndpoint('GET', `/v1/livechat/department/:_id`, getDepartmentByIdCallback) | ||
.build(), | ||
}, | ||
); | ||
|
||
expect(getDepartmentByIdCallback).not.toHaveBeenCalled(); | ||
await waitFor(() => expect(result.current.itemsList.items).toContainEqual(selectedDepartmentMappedToOption)); | ||
// The expected length is 26 because the hook will add the 'All' item on run time | ||
await waitFor(() => expect(result.current.itemsList.items.length).toBe(26)); | ||
}); | ||
|
||
it('should fetch and add selected department if it is not part of departments list on first fetch', async () => { | ||
const missingDepartmentRawMock = { | ||
_id: '56f5be8bcf8cd67f9e9bcfdc', | ||
name: 'test_department_25', | ||
enabled: true, | ||
email: 'test25@email.com', | ||
showOnRegistration: false, | ||
showOnOfflineForm: false, | ||
type: 'd', | ||
_updatedAt: '2024-09-26T20:05:31.330Z', | ||
offlineMessageChannelName: '', | ||
numAgents: 0, | ||
ancestors: undefined, | ||
parentId: undefined, | ||
}; | ||
|
||
const missingDepartmentMappedToOption = { | ||
_id: '56f5be8bcf8cd67f9e9bcfdc', | ||
label: 'test_department_25', | ||
value: '56f5be8bcf8cd67f9e9bcfdc', | ||
}; | ||
|
||
const { result } = renderHook( | ||
() => | ||
useDepartmentsList({ | ||
filter: '', | ||
onlyMyDepartments: true, | ||
haveAll: true, | ||
showArchived: true, | ||
selectedDepartment: '56f5be8bcf8cd67f9e9bcfdc', | ||
}), | ||
{ | ||
legacyRoot: true, | ||
wrapper: mockAppRoot() | ||
.withEndpoint('GET', '/v1/livechat/department', () => ({ | ||
count: 25, | ||
offset: 0, | ||
total: 25, | ||
departments: initialDepartmentsListMock, | ||
})) | ||
.withEndpoint('GET', `/v1/livechat/department/:_id`, () => ({ | ||
department: missingDepartmentRawMock, | ||
})) | ||
.build(), | ||
}, | ||
); | ||
|
||
await waitFor(() => expect(result.current.itemsList.items).toContainEqual(missingDepartmentMappedToOption)); | ||
// The expected length is 27 because the hook will add the 'All' item and the missing department on run time | ||
await waitFor(() => expect(result.current.itemsList.items.length).toBe(27)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
apps/meteor/client/components/Omnichannel/utils/normalizeDepartments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { EndpointFunction } from '@rocket.chat/ui-contexts'; | ||
|
||
import type { DepartmentListItem } from '../Definitions/DepartmentsDefinitions'; | ||
|
||
export const normalizeDepartments = async ( | ||
departments: DepartmentListItem[], | ||
selectedDepartment: string, | ||
getDepartment: EndpointFunction<'GET', '/v1/livechat/department/:_id'>, | ||
): Promise<DepartmentListItem[]> => { | ||
const isSelectedDepartmentAlreadyOnList = () => departments.some((department) => department._id === selectedDepartment); | ||
if (!selectedDepartment || selectedDepartment === 'all' || isSelectedDepartmentAlreadyOnList()) { | ||
return departments; | ||
} | ||
|
||
try { | ||
const { department: missingDepartment } = await getDepartment({}); | ||
|
||
return missingDepartment | ||
? [...departments, { _id: missingDepartment._id, label: missingDepartment.name, value: missingDepartment._id }] | ||
: departments; | ||
} catch { | ||
return departments; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters