|
| 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 | +}); |
0 commit comments