Skip to content

Commit

Permalink
Reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
rique223 committed Oct 7, 2024
1 parent 9315d9c commit f8d670f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DepartmentListItem[]> => {
const isSelectedDepartmentAlreadyOnList = () => departments.some((department) => department._id === selectedDepartment);
if (!selectedDepartment || selectedDepartment === 'all' || isSelectedDepartmentAlreadyOnList()) {
return departments;
}

Expand Down

0 comments on commit f8d670f

Please sign in to comment.