-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathsharingService.test.ts
More file actions
186 lines (159 loc) · 6.67 KB
/
sharingService.test.ts
File metadata and controls
186 lines (159 loc) · 6.67 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
import { PERMISSION_CAN_DOWNLOAD, PERMISSION_CAN_PREVIEW } from '../../../constants';
import { CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS } from '../constants';
import { createSharingService } from '../sharingService';
import { convertSharedLinkPermissions, convertSharedLinkSettings } from '../utils';
jest.mock('../utils');
const mockItemApiInstance = {
updateSharedLink: jest.fn(),
};
const options = { id: '123', permissions: { can_set_share_access: true, can_share: true } };
const mockOnSuccess = jest.fn();
describe('elements/content-sharing/sharingService', () => {
beforeEach(() => {
(convertSharedLinkPermissions as jest.Mock).mockReturnValue({
[PERMISSION_CAN_DOWNLOAD]: true,
[PERMISSION_CAN_PREVIEW]: false,
});
(convertSharedLinkSettings as jest.Mock).mockReturnValue({
unshared_at: null,
vanity_url: 'https://example.com/vanity-url',
});
});
afterEach(() => {
jest.clearAllMocks();
});
describe('changeSharedLinkPermission', () => {
test('should return an object with changeSharedLinkPermission method', () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options,
});
expect(service).toHaveProperty('changeSharedLinkPermission');
expect(typeof service.changeSharedLinkPermission).toBe('function');
});
test('should call updateSharedLink with correct parameters when changeSharedLinkPermission is called', async () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options,
});
const permissionLevel = PERMISSION_CAN_DOWNLOAD;
const expectedPermissions = {
[PERMISSION_CAN_DOWNLOAD]: true,
[PERMISSION_CAN_PREVIEW]: false,
};
await service.changeSharedLinkPermission(permissionLevel);
expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
options,
{ permissions: expectedPermissions },
mockOnSuccess,
{},
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
);
});
});
describe('updateSharedLink', () => {
test('should return an object with updateSharedLink method', () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options,
});
expect(service).toHaveProperty('updateSharedLink');
expect(typeof service.updateSharedLink).toBe('function');
});
test('should call updateSharedLink with basic shared link settings', async () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options,
});
const sharedLinkSettings = {
expiration: null,
isDownloadEnabled: true,
isExpirationEnabled: false,
isPasswordEnabled: false,
password: '',
vanityName: 'vanity-name',
};
const expectedConvertedSettings = {
unshared_at: null,
vanity_url: 'https://example.com/vanity-url',
};
await service.updateSharedLink(sharedLinkSettings);
expect(convertSharedLinkSettings).toHaveBeenCalledWith(
sharedLinkSettings,
undefined, // access
undefined, // isDownloadAvailable
undefined, // serverURL
);
expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
options,
expectedConvertedSettings,
mockOnSuccess,
{},
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
);
});
test('should call updateSharedLink with options including access, isDownloadAvailable, and serverURL', async () => {
const mockConvertedSharedLinkSettings = {
password: 'test-password',
permissions: { can_download: false, can_preview: true },
unshared_at: null,
vanity_url: 'https://example.com/vanity-url',
};
(convertSharedLinkSettings as jest.Mock).mockReturnValue(mockConvertedSharedLinkSettings);
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options: {
...options,
access: 'open',
isDownloadAvailable: true,
serverURL: 'https://example.com/server-url',
},
});
const sharedLinkSettings = {
expiration: null,
isDownloadEnabled: false,
isExpirationEnabled: true,
isPasswordEnabled: true,
password: 'test-password',
vanityName: 'vanity-name',
};
await service.updateSharedLink(sharedLinkSettings);
expect(convertSharedLinkSettings).toHaveBeenCalledWith(
sharedLinkSettings,
'open',
true,
'https://example.com/server-url',
);
expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
options,
mockConvertedSharedLinkSettings,
mockOnSuccess,
{},
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
);
});
test('should handle shared link settings correctly', async () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options,
});
const expirationDate = new Date('2024-12-31T23:59:59Z');
const sharedLinkSettings = {
expiration: expirationDate,
isDownloadEnabled: false,
isExpirationEnabled: true,
isPasswordEnabled: false,
password: 'test-password',
vanityName: 'vanity-name',
};
await service.updateSharedLink(sharedLinkSettings);
expect(convertSharedLinkSettings).toHaveBeenCalledWith(sharedLinkSettings, undefined, undefined, undefined);
});
});
});