-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathuseInvites.test.js
More file actions
126 lines (110 loc) · 5.08 KB
/
useInvites.test.js
File metadata and controls
126 lines (110 loc) · 5.08 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
import { renderHook } from '@testing-library/react';
import useInvites from '../hooks/useInvites';
import API from '../../../api';
jest.mock('../../../api');
const mockCollaborators = [{ id: '123', type: 'user', email: 'user@test.com', role: 'editor' }];
describe('useInvites hook', () => {
let mockApi;
let mockHandleSuccess;
let mockHandleError;
let mockTransformRequest;
let mockTransformResponse;
beforeEach(() => {
mockApi = new API({});
mockHandleSuccess = jest.fn();
mockHandleError = jest.fn();
mockTransformRequest = jest.fn().mockImplementation(collabRequest => {
if (collabRequest) {
return {
users: collabRequest.users || [{ email: 'user@example.com', role: 'editor' }],
groups: collabRequest.groups || [{ id: 'group123', role: 'viewer' }],
};
}
return {
users: [{ email: 'user@example.com', role: 'editor' }],
groups: [{ id: 'group123', role: 'viewer' }],
};
});
mockTransformResponse = jest.fn().mockReturnValue({ id: '123', type: 'folder' });
jest.spyOn(mockApi, 'getCollaborationsAPI').mockReturnValue({
addCollaboration: jest.fn().mockImplementation((itemData, collab, successCallback, errorCallback) => {
if (collab.email === 'fail@example.com') {
errorCallback(new Error('Failed to add collaboration'));
} else {
successCallback({ id: 'collab123', role: collab.role });
}
}),
});
});
test('invokes setIsLoading, handleSuccess, and transformResponse on successful collaboration addition', async () => {
const { result } = renderHook(() =>
useInvites(mockApi, '123', 'folder', {
handleSuccess: mockHandleSuccess,
handleError: mockHandleError,
transformRequest: mockTransformRequest,
transformResponse: mockTransformResponse,
}),
);
await result.current({ users: [{ email: 'user@example.com', role: 'editor' }] });
expect(mockHandleSuccess).toHaveBeenCalledWith({ id: 'collab123', role: 'editor' });
expect(mockTransformResponse).toHaveBeenCalledWith({ id: 'collab123', role: 'editor' });
});
test('invokes handleError on failed collaboration addition', async () => {
const { result } = renderHook(() =>
useInvites(mockApi, '123', 'folder', {
handleSuccess: mockHandleSuccess,
handleError: mockHandleError,
transformRequest: mockTransformRequest,
transformResponse: mockTransformResponse,
}),
);
await result.current({ users: [{ email: 'fail@example.com', role: 'editor' }] }).catch(() => {});
expect(mockHandleError).toHaveBeenCalled();
});
test('returns null if transformRequest is not provided', async () => {
const { result } = renderHook(() =>
useInvites(mockApi, '123', 'folder', {
handleSuccess: mockHandleSuccess,
handleError: mockHandleError,
transformResponse: mockTransformResponse,
}),
);
const actionResult = await result.current({ users: [{ email: 'user@example.com', role: 'editor' }] });
expect(actionResult).toEqual(null);
expect(mockHandleSuccess).not.toHaveBeenCalled();
expect(mockHandleError).not.toHaveBeenCalled();
});
describe('when isContentSharingV2Enabled is true', () => {
test('processes multiple users and groups in a single call', async () => {
const { result } = renderHook(() =>
useInvites(mockApi, '123', 'folder', {
collaborators: mockCollaborators,
handleSuccess: mockHandleSuccess,
handleError: mockHandleError,
isContentSharingV2Enabled: true,
transformRequest: mockTransformRequest,
transformResponse: mockTransformResponse,
}),
);
await result.current({
users: [{ email: 'user@example.com', role: 'editor' }],
groups: [{ id: 'group123', role: 'viewer' }],
});
expect(mockHandleSuccess).toHaveBeenCalledTimes(2);
expect(mockTransformResponse).toHaveBeenCalledTimes(2);
});
test('Should early return null if collaborators is not provided', async () => {
const { result } = renderHook(() =>
useInvites(mockApi, '123', 'folder', {
handleSuccess: mockHandleSuccess,
handleError: mockHandleError,
isContentSharingV2Enabled: true,
transformResponse: mockTransformResponse,
}),
);
expect(result.current).toBeNull();
expect(mockHandleSuccess).not.toHaveBeenCalled();
expect(mockHandleError).not.toHaveBeenCalled();
});
});
});