-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathuseAgentsList.spec.ts
More file actions
78 lines (57 loc) · 2.48 KB
/
useAgentsList.spec.ts
File metadata and controls
78 lines (57 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { ILivechatAgent, Serialized } from '@rocket.chat/core-typings';
import { MockedAppRootBuilder } from '@rocket.chat/mock-providers/dist/MockedAppRootBuilder';
import { act, renderHook, waitFor } from '@testing-library/react';
import { useAgentsList } from './useAgentsList';
import { createFakeAgent } from '../../../../tests/mocks/data';
const formatAgentItem = (agent: Serialized<ILivechatAgent>) => ({
_id: agent._id,
label: `${agent.name || agent._id} (@${agent.username})`,
value: agent._id,
});
const mockGetAgents = jest.fn();
const appRoot = new MockedAppRootBuilder()
.withTranslations('en', 'core', { All: 'All', Empty_no_agent_selected: 'Empty, no agent selected' })
.withEndpoint('GET', '/v1/livechat/users/:type', mockGetAgents);
afterEach(() => {
jest.clearAllMocks();
});
it('should fetch agents', async () => {
const limit = 5;
const data = Array.from({ length: 10 }, () => createFakeAgent());
mockGetAgents.mockImplementation(({ offset, count }: { offset: number; count: number }) => {
const users = data.slice(offset, offset + count);
return {
users,
count,
offset,
total: data.length,
};
});
const { result } = renderHook(() => useAgentsList({ filter: '', limit }), { wrapper: appRoot.build() });
await waitFor(() => expect(result.current.data).toEqual(data.slice(0, 5).map(formatAgentItem)));
await act(() => result.current.fetchNextPage());
await waitFor(() => expect(result.current.data).toEqual(data.map(formatAgentItem)));
await act(() => result.current.fetchNextPage());
// should not fetch again since total was reached
expect(mockGetAgents).toHaveBeenCalledTimes(2);
});
it('should include "All" item if haveAll is true', async () => {
mockGetAgents.mockResolvedValueOnce({
users: Array.from({ length: 5 }, () => createFakeAgent()),
count: 5,
offset: 0,
total: 5,
});
const { result } = renderHook(() => useAgentsList({ filter: '', haveAll: true }), { wrapper: appRoot.build() });
await waitFor(() => expect(result.current.data[0].label).toBe('All'));
});
it('should include "Empty_no_agent_selected" item if haveNoAgentsSelectedOption is true', async () => {
mockGetAgents.mockResolvedValueOnce({
users: Array.from({ length: 5 }, () => createFakeAgent()),
count: 5,
offset: 0,
total: 5,
});
const { result } = renderHook(() => useAgentsList({ filter: '', haveNoAgentsSelectedOption: true }), { wrapper: appRoot.build() });
await waitFor(() => expect(result.current.data[0].label).toBe('Empty, no agent selected'));
});