-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathuseContactService.test.ts
More file actions
65 lines (53 loc) · 2.73 KB
/
useContactService.test.ts
File metadata and controls
65 lines (53 loc) · 2.73 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
import { renderHook } from '@testing-library/react';
import { convertGroupContactsResponse, convertUserContactsResponse } from '../../utils';
import { useContactService } from '../useContactService';
import useContacts from '../useContacts';
jest.mock('../useContacts');
jest.mock('../../utils');
const mockApi = {
getMarkerBasedUsersAPI: jest.fn(),
getMarkerBasedGroupsAPI: jest.fn(),
};
const mockItemID = '123456789';
const mockCurrentUserID = '123';
const mockGetContacts = jest.fn();
describe('elements/content-sharing/hooks/useContactService', () => {
beforeEach(() => {
(useContacts as jest.Mock).mockReturnValue(mockGetContacts);
(convertGroupContactsResponse as jest.Mock).mockReturnValue([]);
(convertUserContactsResponse as jest.Mock).mockReturnValue([]);
});
afterEach(() => {
jest.clearAllMocks();
});
test('should return contactService with getContacts function', () => {
const { result } = renderHook(() => useContactService(mockApi, mockItemID, mockCurrentUserID));
expect(useContacts).toHaveBeenCalledWith(mockApi, mockItemID, {
currentUserId: mockCurrentUserID,
isContentSharingV2Enabled: true,
transformUsers: expect.any(Function),
transformGroups: expect.any(Function),
});
expect(result.current.contactService).toEqual({
getContacts: mockGetContacts,
});
});
test('should pass transform functions that call correct conversion functions with params', () => {
const mockTransformedUsers = [{ id: 'user1', email: 'user1@test.com' }];
const mockTransformedGroups = [{ id: 'group1', name: 'Test Group' }];
const mockUserData = { entries: mockTransformedUsers };
const mockGroupData = { entries: mockTransformedGroups };
(convertUserContactsResponse as jest.Mock).mockReturnValue(mockTransformedUsers);
(convertGroupContactsResponse as jest.Mock).mockReturnValue(mockTransformedGroups);
renderHook(() => useContactService(mockApi, mockItemID, mockCurrentUserID));
// Get the transform functions that were passed to useContacts
const transformUsersFn = useContacts.mock.calls[0][2].transformUsers;
const transformGroupsFn = useContacts.mock.calls[0][2].transformGroups;
const resultUsers = transformUsersFn(mockUserData);
const resultGroups = transformGroupsFn(mockGroupData);
expect(convertUserContactsResponse as jest.Mock).toHaveBeenCalledWith(mockUserData, mockCurrentUserID);
expect(convertGroupContactsResponse as jest.Mock).toHaveBeenCalledWith(mockGroupData);
expect(resultUsers).toBe(mockTransformedUsers);
expect(resultGroups).toBe(mockTransformedGroups);
});
});