Skip to content

Commit 77e6c33

Browse files
test: added unit tests for useTagsList
1 parent 930cac2 commit 77e6c33

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { ILivechatTag, Serialized } from '@rocket.chat/core-typings';
2+
import { MockedAppRootBuilder } from '@rocket.chat/mock-providers/dist/MockedAppRootBuilder';
3+
import { act, renderHook, waitFor } from '@testing-library/react';
4+
5+
import type { TagListItem } from './useTagsList';
6+
import { useTagsList } from './useTagsList';
7+
import { createFakeTag } from '../../../../tests/mocks/data';
8+
9+
const formatTagItem = (tag: Serialized<ILivechatTag>): TagListItem => ({
10+
_id: tag._id,
11+
label: tag.name,
12+
value: tag.name,
13+
});
14+
15+
const mockGetTags = jest.fn();
16+
17+
const appRoot = new MockedAppRootBuilder().withEndpoint('GET', '/v1/livechat/tags', mockGetTags);
18+
19+
afterEach(() => {
20+
jest.clearAllMocks();
21+
});
22+
23+
it('should fetch tags', async () => {
24+
const limit = 5;
25+
26+
const data = Array.from({ length: 10 }, () => createFakeTag());
27+
28+
mockGetTags.mockImplementation(({ offset, count }: { offset: number; count: number }) => {
29+
const tags = data.slice(offset, offset + count);
30+
31+
return {
32+
tags,
33+
count,
34+
offset,
35+
total: data.length,
36+
};
37+
});
38+
39+
const { result } = renderHook(() => useTagsList({ filter: '', limit }), { wrapper: appRoot.build() });
40+
41+
expect(result.current.isFetching).toBe(true);
42+
await waitFor(() => expect(result.current.isFetching).toBe(false));
43+
44+
expect(mockGetTags).toHaveBeenCalled();
45+
expect(result.current.data).toEqual(data.slice(0, 5).map(formatTagItem));
46+
47+
await act(() => result.current.fetchNextPage());
48+
49+
expect(mockGetTags).toHaveBeenCalledTimes(2);
50+
await waitFor(() => expect(result.current.data).toHaveLength(10));
51+
expect(result.current.data).toEqual(data.map(formatTagItem));
52+
53+
await act(() => result.current.fetchNextPage());
54+
55+
// should not fetch again since total was reached
56+
expect(mockGetTags).toHaveBeenCalledTimes(2);
57+
});

apps/meteor/client/components/Omnichannel/hooks/useTagsList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type TagsListOptions = {
99
limit?: number;
1010
};
1111

12-
type TagListItem = {
12+
export type TagListItem = {
1313
_id: string;
1414
label: string;
1515
value: string;

apps/meteor/tests/mocks/data.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
ILivechatContactChannel,
1313
Serialized,
1414
ILivechatAgent,
15+
ILivechatTag,
1516
} from '@rocket.chat/core-typings';
1617
import { parse } from '@rocket.chat/message-parser';
1718

@@ -363,3 +364,14 @@ export function createFakeAgent(overrides?: Partial<Serialized<ILivechatAgent>>)
363364
...overrides,
364365
};
365366
}
367+
368+
export function createFakeTag(overrides?: Partial<Serialized<ILivechatTag>>): Serialized<ILivechatTag> {
369+
return {
370+
_id: faker.string.uuid(),
371+
name: faker.commerce.department(),
372+
description: 'description',
373+
numDepartments: 0,
374+
departments: [],
375+
...overrides,
376+
};
377+
}

0 commit comments

Comments
 (0)