Skip to content

Commit c908cbd

Browse files
committed
feat: add tests
1 parent 6b359ed commit c908cbd

5 files changed

Lines changed: 468 additions & 72 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { renderHook } from '@testing-library/react';
2+
3+
import { convertGroupContactsResponse, convertUserContactsResponse } from '../../utils';
4+
import { useContactService } from '../useContactService';
5+
import useContacts from '../useContacts';
6+
7+
jest.mock('../useContacts');
8+
jest.mock('../../utils');
9+
10+
describe('elements/content-sharing/hooks/useContactService', () => {
11+
const mockApi = {
12+
getMarkerBasedUsersAPI: jest.fn(),
13+
getMarkerBasedGroupsAPI: jest.fn(),
14+
};
15+
const mockItemID = '123456789';
16+
const mockCurrentUserID = '123';
17+
const mockGetContacts = jest.fn();
18+
19+
beforeEach(() => {
20+
(useContacts as jest.Mock).mockReturnValue(mockGetContacts);
21+
(convertGroupContactsResponse as jest.Mock).mockReturnValue([]);
22+
(convertUserContactsResponse as jest.Mock).mockReturnValue([]);
23+
});
24+
25+
afterEach(() => {
26+
jest.clearAllMocks();
27+
});
28+
29+
test('should return null contactService when currentUserID is null or undefined', () => {
30+
[null, undefined].forEach(currentUserID => {
31+
const { result } = renderHook(() => useContactService(mockApi, mockItemID, currentUserID));
32+
33+
expect(result.current.contactService).toBeNull();
34+
});
35+
});
36+
37+
test('should return contactService with getContacts function', () => {
38+
const { result } = renderHook(() => useContactService(mockApi, mockItemID, mockCurrentUserID));
39+
40+
expect(useContacts).toHaveBeenCalledWith(mockApi, mockItemID, {
41+
transformUsers: expect.any(Function),
42+
transformGroups: expect.any(Function),
43+
});
44+
expect(result.current.contactService).toEqual({
45+
getContacts: mockGetContacts,
46+
});
47+
});
48+
49+
test('should pass transform functions that call correct conversion functions with params', () => {
50+
const mockTransformedUsers = [{ id: 'user1', email: 'user1@test.com' }];
51+
const mockTransformedGroups = [{ id: 'group1', name: 'Test Group' }];
52+
const mockUserData = { entries: mockTransformedUsers };
53+
const mockGroupData = { entries: mockTransformedGroups };
54+
55+
(convertUserContactsResponse as jest.Mock).mockReturnValue(mockTransformedUsers);
56+
(convertGroupContactsResponse as jest.Mock).mockReturnValue(mockTransformedGroups);
57+
58+
renderHook(() => useContactService(mockApi, mockItemID, mockCurrentUserID));
59+
60+
// Get the transform functions that were passed to useContacts
61+
const transformUsersFn = useContacts.mock.calls[0][2].transformUsers;
62+
const transformGroupsFn = useContacts.mock.calls[0][2].transformGroups;
63+
const resultUsers = transformUsersFn(mockUserData);
64+
const resultGroups = transformGroupsFn(mockGroupData);
65+
66+
expect(convertUserContactsResponse as jest.Mock).toHaveBeenCalledWith(mockUserData, mockCurrentUserID);
67+
expect(convertGroupContactsResponse as jest.Mock).toHaveBeenCalledWith(mockGroupData);
68+
expect(resultUsers).toBe(mockTransformedUsers);
69+
expect(resultGroups).toBe(mockTransformedGroups);
70+
});
71+
});
Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,21 @@
11
import * as React from 'react';
22

3-
import { STATUS_INACTIVE } from '../../../constants';
3+
import { convertGroupContactsResponse, convertUserContactsResponse } from '../utils';
44
import useContacts from './useContacts';
5-
import useContactsByEmail from './useContactsByEmail';
6-
import { convertUserContactsByEmailResponse } from '../../../features/unified-share-modal/utils/convertData';
75

8-
const APP_USERS_DOMAIN_REGEXP = /boxdevedition.com/;
9-
const sortByName = ({ name: nameA = '' }, { name: nameB = '' }) => nameA.localeCompare(nameB);
10-
11-
/**
12-
* Convert an enterprise users API response into an array of internal USM contacts.
13-
*/
14-
export const convertUserContactsResponse = (contactsAPIData, currentUserID) => {
15-
const { entries = [] } = contactsAPIData;
16-
17-
// Return all active users except for the current user and app users
18-
return entries
19-
.filter(
20-
({ id, login: email, status }) =>
21-
id !== currentUserID &&
22-
email &&
23-
!APP_USERS_DOMAIN_REGEXP.test(email) &&
24-
status &&
25-
status !== STATUS_INACTIVE,
26-
)
27-
.map(contact => {
28-
const { id, login: email, name, type } = contact;
29-
return {
30-
id,
31-
email,
32-
name,
33-
type,
34-
value: email,
35-
};
36-
})
37-
.sort(sortByName);
38-
};
39-
40-
/**
41-
* Convert an enterprise groups API response into an array of internal USM contacts.
42-
*/
43-
export const convertGroupContactsResponse = contactsAPIData => {
44-
const { entries = [] } = contactsAPIData;
45-
46-
// Only return groups with the correct permissions
47-
return entries
48-
.filter(({ permissions }) => {
49-
return permissions && permissions.can_invite_as_collaborator;
50-
})
51-
.map(contact => {
52-
const { id, name, type } = contact;
53-
return {
54-
id,
55-
email: 'Group', // Need this for the avatar to work for isUserContactType
56-
name,
57-
type,
58-
value: 'Group',
59-
};
60-
})
61-
.sort(sortByName);
62-
};
63-
64-
export const useContactService = (api, itemID, currentUserID) => {
6+
export const useContactService = (api, itemID, currentUserId) => {
657
const getContacts = useContacts(api, itemID, {
66-
transformUsers: data => convertUserContactsResponse(data, currentUserID),
8+
transformUsers: data => convertUserContactsResponse(data, currentUserId),
679
transformGroups: data => convertGroupContactsResponse(data),
6810
});
6911

70-
const getContactsByEmail = useContactsByEmail(api, itemID, {
71-
transformUsers: data => convertUserContactsByEmailResponse(data),
72-
});
73-
7412
const contactService = React.useMemo(() => {
75-
if (!currentUserID) {
13+
if (!currentUserId) {
7614
return null;
7715
}
7816

79-
return {
80-
getContacts,
81-
getContactsByEmail,
82-
};
83-
}, [currentUserID, getContacts, getContactsByEmail]);
17+
return { getContacts };
18+
}, [currentUserId, getContacts]);
8419

8520
return { contactService };
8621
};

0 commit comments

Comments
 (0)