From f8d670fc8d637e358b068dc959ad41ac8d2ed4ea Mon Sep 17 00:00:00 2001 From: rique223 Date: Mon, 7 Oct 2024 14:20:27 -0300 Subject: [PATCH] Reviews --- .../Omnichannel/hooks/useDepartmentsList.spec.ts | 8 ++++++-- .../components/Omnichannel/hooks/useDepartmentsList.ts | 2 +- .../Omnichannel/{ => utils}/normalizeDepartments.ts | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) rename apps/meteor/client/components/Omnichannel/{ => utils}/normalizeDepartments.ts (68%) diff --git a/apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.spec.ts b/apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.spec.ts index 86bb71f66401c..173677799b08b 100644 --- a/apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.spec.ts +++ b/apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.spec.ts @@ -20,13 +20,15 @@ const initialDepartmentsListMock = Array.from(Array(25)).map((_, index) => { }; }); -it('should not add selected department if it is already in the departments list on first fetch', async () => { +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({ @@ -45,16 +47,18 @@ it('should not add selected department if it is already in the departments list 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 add selected department if it is not part of departments list on first fetch', async () => { +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', diff --git a/apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.ts b/apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.ts index 3ed33b56491f1..f251c453c4d04 100644 --- a/apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.ts +++ b/apps/meteor/client/components/Omnichannel/hooks/useDepartmentsList.ts @@ -4,7 +4,7 @@ import { useCallback, useState } from 'react'; import { useScrollableRecordList } from '../../../hooks/lists/useScrollableRecordList'; import { useComponentDidUpdate } from '../../../hooks/useComponentDidUpdate'; import { RecordList } from '../../../lib/lists/RecordList'; -import { normalizeDepartments } from '../normalizeDepartments'; +import { normalizeDepartments } from '../utils/normalizeDepartments'; type DepartmentsListOptions = { filter: string; diff --git a/apps/meteor/client/components/Omnichannel/normalizeDepartments.ts b/apps/meteor/client/components/Omnichannel/utils/normalizeDepartments.ts similarity index 68% rename from apps/meteor/client/components/Omnichannel/normalizeDepartments.ts rename to apps/meteor/client/components/Omnichannel/utils/normalizeDepartments.ts index 1ccfbee076a6d..13cbe69eaf2d7 100644 --- a/apps/meteor/client/components/Omnichannel/normalizeDepartments.ts +++ b/apps/meteor/client/components/Omnichannel/utils/normalizeDepartments.ts @@ -1,14 +1,14 @@ import type { EndpointFunction } from '@rocket.chat/ui-contexts'; -import type { DepartmentListItem } from './hooks/useDepartmentsList'; +import type { DepartmentListItem } from '../hooks/useDepartmentsList'; export const normalizeDepartments = async ( departments: DepartmentListItem[], selectedDepartment: string, getDepartment: EndpointFunction<'GET', '/v1/livechat/department/:_id'>, -) => { - const isSelectedDepartmentAlreadyOnList = departments.find((department) => department._id === selectedDepartment); - if (!selectedDepartment || selectedDepartment === 'all' || isSelectedDepartmentAlreadyOnList) { +): Promise => { + const isSelectedDepartmentAlreadyOnList = () => departments.some((department) => department._id === selectedDepartment); + if (!selectedDepartment || selectedDepartment === 'all' || isSelectedDepartmentAlreadyOnList()) { return departments; }