-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathuseContacts.test.js
More file actions
277 lines (244 loc) · 11.4 KB
/
useContacts.test.js
File metadata and controls
277 lines (244 loc) · 11.4 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// @flow
import React, { act } from 'react';
import { mount } from 'enzyme';
import API from '../../../api';
import useContacts from '../hooks/useContacts';
import {
MOCK_CONTACTS_API_RESPONSE,
MOCK_CONTACTS_CONVERTED_RESPONSE,
MOCK_GROUP_CONTACTS_API_RESPONSE,
MOCK_GROUP_CONTACTS_CONVERTED_RESPONSE,
MOCK_ITEM_ID,
} from '../../../features/unified-share-modal/utils/__mocks__/USMMocks';
const handleSuccess = jest.fn();
const handleError = jest.fn();
const transformUsersSpy = jest.fn().mockReturnValue(MOCK_CONTACTS_CONVERTED_RESPONSE);
const transformGroupsSpy = jest.fn().mockReturnValue(MOCK_GROUP_CONTACTS_CONVERTED_RESPONSE);
const createAPIMock = (markerBasedGroupsAPI, markerBasedUsersAPI) => ({
getMarkerBasedGroupsAPI: jest.fn().mockReturnValue(markerBasedGroupsAPI),
getMarkerBasedUsersAPI: jest.fn().mockReturnValue(markerBasedUsersAPI),
});
function FakeComponent({
api,
transformGroups,
transformUsers,
}: {
api: API,
transformGroups: Function,
transformUsers: Function,
}) {
const [getContacts, setGetContacts] = React.useState(null);
const updatedGetContactsFn = useContacts(api, MOCK_ITEM_ID, {
currentUserId: '123',
handleSuccess,
handleError,
transformGroups,
transformUsers,
});
if (updatedGetContactsFn && !getContacts) {
setGetContacts(() => updatedGetContactsFn);
}
return (
getContacts && (
<button onClick={getContacts} type="submit">
♫ Box UI Elements ♫
</button>
)
);
}
const MOCK_FILTER = 'Elements';
describe('elements/content-sharing/hooks/useContacts', () => {
let getGroupsInEnterprise;
let getUsersInEnterprise;
let mockAPI;
describe('with successful API calls', () => {
beforeAll(() => {
getGroupsInEnterprise = jest.fn().mockImplementation((itemID, getGroupsInEnterpriseSuccess) => {
return getGroupsInEnterpriseSuccess(MOCK_GROUP_CONTACTS_API_RESPONSE);
});
getUsersInEnterprise = jest.fn().mockImplementation((itemID, getUsersInEnterpriseSuccess) => {
return getUsersInEnterpriseSuccess(MOCK_CONTACTS_API_RESPONSE);
});
mockAPI = createAPIMock({ getGroupsInEnterprise }, { getUsersInEnterprise });
});
test('should set the value of getContacts() and retrieve contacts on invocation', () => {
let fakeComponent;
act(() => {
fakeComponent = mount(
<FakeComponent
api={mockAPI}
transformGroups={transformGroupsSpy}
transformUsers={transformUsersSpy}
/>,
);
});
fakeComponent.update();
const btn = fakeComponent.find('button');
expect(btn.prop('onClick')).toBeDefined();
const contacts = btn.invoke('onClick')(MOCK_FILTER);
expect(getUsersInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ filter_term: MOCK_FILTER },
);
expect(getGroupsInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ fields: 'name,permissions', filter_term: MOCK_FILTER },
);
expect(handleSuccess).toHaveBeenCalledWith(MOCK_CONTACTS_API_RESPONSE);
expect(handleSuccess).toHaveBeenCalledWith(MOCK_GROUP_CONTACTS_API_RESPONSE);
expect(transformGroupsSpy).toHaveBeenCalledWith(MOCK_GROUP_CONTACTS_API_RESPONSE);
expect(transformUsersSpy).toHaveBeenCalledWith(MOCK_CONTACTS_API_RESPONSE);
return expect(contacts).resolves.toEqual([
...MOCK_CONTACTS_CONVERTED_RESPONSE,
...MOCK_GROUP_CONTACTS_CONVERTED_RESPONSE,
]);
});
test('should return the entries from the API data if transformUsers() is not provided', () => {
let fakeComponent;
act(() => {
fakeComponent = mount(<FakeComponent api={mockAPI} />);
});
fakeComponent.update();
const btn = fakeComponent.find('button');
expect(btn.prop('onClick')).toBeDefined();
const contacts = btn.invoke('onClick')(MOCK_FILTER);
expect(getUsersInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ filter_term: MOCK_FILTER },
);
expect(getGroupsInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ fields: 'name,permissions', filter_term: MOCK_FILTER },
);
expect(handleSuccess).toHaveBeenCalledWith(MOCK_CONTACTS_API_RESPONSE);
expect(handleSuccess).toHaveBeenCalledWith(MOCK_GROUP_CONTACTS_API_RESPONSE);
expect(transformGroupsSpy).not.toHaveBeenCalled();
expect(transformUsersSpy).not.toHaveBeenCalled();
return expect(contacts).resolves.toEqual([
...MOCK_CONTACTS_API_RESPONSE.entries,
...MOCK_GROUP_CONTACTS_API_RESPONSE.entries,
]);
});
test('should set the value of getContacts() to an empty array when no results are found', () => {
const EMPTY_GROUPS = { entries: [] };
const EMPTY_USERS = { entries: [] };
getGroupsInEnterprise = jest.fn().mockImplementation((itemID, getGroupsInEnterpriseSuccess) => {
return getGroupsInEnterpriseSuccess(EMPTY_GROUPS);
});
getUsersInEnterprise = jest.fn().mockImplementation((itemID, getUsersInEnterpriseSuccess) => {
return getUsersInEnterpriseSuccess(EMPTY_USERS);
});
mockAPI = createAPIMock({ getGroupsInEnterprise }, { getUsersInEnterprise });
let fakeComponent;
act(() => {
fakeComponent = mount(
<FakeComponent
api={mockAPI}
transformGroups={transformGroupsSpy}
transformUsers={transformUsersSpy}
/>,
);
});
fakeComponent.update();
const btn = fakeComponent.find('button');
expect(btn.prop('onClick')).toBeDefined();
const contacts = btn.invoke('onClick')(MOCK_FILTER);
expect(handleSuccess).toHaveBeenCalledWith(EMPTY_GROUPS);
expect(handleSuccess).toHaveBeenCalledWith(EMPTY_USERS);
expect(transformGroupsSpy).not.toHaveBeenCalled();
expect(transformUsersSpy).not.toHaveBeenCalled();
return expect(contacts).resolves.toEqual([]);
});
/**
* A successful API call will always return an entries array. However, the Flow definitions
* for GroupCollection and UserCollection mark "entries" as optional, so we still need to test
* for the hypothetical case in which the entries array is undefined.
*/
test.each`
groupsResponse | usersResponse | resolvedResponse | description
${undefined} | ${undefined} | ${[]} | ${'both responses are undefined'}
${{}} | ${{}} | ${[]} | ${'both responses are defined, but do not contain an entries array'}
${undefined} | ${MOCK_CONTACTS_API_RESPONSE} | ${MOCK_CONTACTS_CONVERTED_RESPONSE} | ${'users response is defined, and groups response is undefined'}
${MOCK_GROUP_CONTACTS_API_RESPONSE} | ${undefined} | ${MOCK_GROUP_CONTACTS_CONVERTED_RESPONSE} | ${'groups response is defined, and users response is undefined'}
`(
'should set the value of getContacts() when $description',
({ groupsResponse, usersResponse, resolvedResponse }) => {
getGroupsInEnterprise = jest.fn().mockImplementation((itemID, getGroupsInEnterpriseSuccess) => {
return getGroupsInEnterpriseSuccess(groupsResponse);
});
getUsersInEnterprise = jest.fn().mockImplementation((itemID, getUsersInEnterpriseSuccess) => {
return getUsersInEnterpriseSuccess(usersResponse);
});
mockAPI = createAPIMock({ getGroupsInEnterprise }, { getUsersInEnterprise });
let fakeComponent;
act(() => {
fakeComponent = mount(
<FakeComponent
api={mockAPI}
transformGroups={transformGroupsSpy}
transformUsers={transformUsersSpy}
/>,
);
});
fakeComponent.update();
const btn = fakeComponent.find('button');
expect(btn.prop('onClick')).toBeDefined();
const contacts = btn.invoke('onClick')(MOCK_FILTER);
return expect(contacts).resolves.toEqual(resolvedResponse);
},
);
});
describe('with failed API calls', () => {
beforeAll(() => {
getGroupsInEnterprise = jest
.fn()
.mockImplementation((itemID, getGroupsInEnterpriseSuccess, getGroupsInEnterpriseError) => {
return getGroupsInEnterpriseError();
});
getUsersInEnterprise = jest
.fn()
.mockImplementation((itemID, getUsersInEnterpriseSuccess, getUsersInEnterpriseError) => {
return getUsersInEnterpriseError();
});
mockAPI = createAPIMock({ getGroupsInEnterprise }, { getUsersInEnterprise });
});
test('should set the value of getContacts() and call handleError() when invoked', () => {
let fakeComponent;
act(() => {
fakeComponent = mount(
<FakeComponent
api={mockAPI}
transformGroups={transformGroupsSpy}
transformUsers={transformUsersSpy}
/>,
);
});
fakeComponent.update();
const btn = fakeComponent.find('button');
expect(btn.prop('onClick')).toBeDefined();
const contacts = btn.invoke('onClick')(MOCK_FILTER);
expect(getUsersInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ filter_term: MOCK_FILTER },
);
expect(getGroupsInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ fields: 'name,permissions', filter_term: MOCK_FILTER },
);
expect(handleError).toHaveBeenCalled();
expect(contacts).resolves.toBeFalsy();
});
});
});