Skip to content

Commit 020f1aa

Browse files
committed
fix: nits
1 parent fd2a1f6 commit 020f1aa

File tree

4 files changed

+56
-58
lines changed

4 files changed

+56
-58
lines changed

src/elements/content-sharing/__tests__/sharingService.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ const mockItemApiInstance = {
1010
share: jest.fn(),
1111
};
1212
const options = { id: '123', permissions: { can_set_share_access: true, can_share: true } };
13-
const mockOnSuccess = {
14-
handleUpdateSharedLinkSuccess: jest.fn(),
15-
handleRemoveSharedLinkSuccess: jest.fn(),
16-
};
13+
const mockOnUpdateSharedLink = jest.fn();
14+
const mockOnRemoveSharedLink = jest.fn();
1715

1816
const createSharingServiceWrapper = () => {
1917
return createSharingService({
2018
itemApiInstance: mockItemApiInstance,
21-
onSuccess: mockOnSuccess,
19+
onUpdateSharedLink: mockOnUpdateSharedLink,
20+
onRemoveSharedLink: mockOnRemoveSharedLink,
2221
options,
2322
});
2423
};
@@ -59,7 +58,7 @@ describe('elements/content-sharing/sharingService', () => {
5958
expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
6059
options,
6160
{ permissions: expectedPermissions },
62-
mockOnSuccess.handleUpdateSharedLinkSuccess,
61+
mockOnUpdateSharedLink,
6362
{},
6463
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
6564
);
@@ -82,7 +81,7 @@ describe('elements/content-sharing/sharingService', () => {
8281
expect(mockItemApiInstance.share).toHaveBeenCalledWith(
8382
options,
8483
access,
85-
mockOnSuccess.handleUpdateSharedLinkSuccess,
84+
mockOnUpdateSharedLink,
8685
{},
8786
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
8887
);
@@ -104,7 +103,7 @@ describe('elements/content-sharing/sharingService', () => {
104103
expect(mockItemApiInstance.share).toHaveBeenCalledWith(
105104
options,
106105
undefined,
107-
mockOnSuccess.handleUpdateSharedLinkSuccess,
106+
mockOnUpdateSharedLink,
108107
{},
109108
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
110109
);
@@ -118,14 +117,14 @@ describe('elements/content-sharing/sharingService', () => {
118117
expect(typeof service.deleteSharedLink).toBe('function');
119118
});
120119

121-
test('should call share with ACCESS_NONE and handleRemoveSharedLinkSuccess when deleteSharedLink is called', async () => {
120+
test('should call share with ACCESS_NONE and onRemoveSharedLink when deleteSharedLink is called', async () => {
122121
const service = createSharingServiceWrapper();
123122
await service.deleteSharedLink();
124123

125124
expect(mockItemApiInstance.share).toHaveBeenCalledWith(
126125
options,
127126
ACCESS_NONE,
128-
mockOnSuccess.handleRemoveSharedLinkSuccess,
127+
mockOnRemoveSharedLink,
129128
{},
130129
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
131130
);
@@ -168,7 +167,7 @@ describe('elements/content-sharing/sharingService', () => {
168167
expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
169168
options,
170169
expectedConvertedSettings,
171-
mockOnSuccess.handleUpdateSharedLinkSuccess,
170+
mockOnUpdateSharedLink,
172171
{},
173172
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
174173
);
@@ -186,7 +185,8 @@ describe('elements/content-sharing/sharingService', () => {
186185

187186
const service = createSharingService({
188187
itemApiInstance: mockItemApiInstance,
189-
onSuccess: mockOnSuccess,
188+
onUpdateSharedLink: mockOnUpdateSharedLink,
189+
onRemoveSharedLink: mockOnRemoveSharedLink,
190190
options: {
191191
...options,
192192
access: 'open',
@@ -215,7 +215,7 @@ describe('elements/content-sharing/sharingService', () => {
215215
expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
216216
options,
217217
mockConvertedSharedLinkSettings,
218-
mockOnSuccess.handleUpdateSharedLinkSuccess,
218+
mockOnUpdateSharedLink,
219219
{},
220220
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
221221
);

src/elements/content-sharing/hooks/__tests__/useSharingService.test.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ const mockApi = {
1313
getFolderAPI: jest.fn(),
1414
};
1515
const mockSharingService = {
16-
deleteSharedLink: jest.fn(),
16+
createSharedLink: jest.fn(),
1717
changeSharedLinkAccess: jest.fn(),
1818
changeSharedLinkPermission: jest.fn(),
19-
createSharedLink: jest.fn(),
19+
deleteSharedLink: jest.fn(),
2020
updateSharedLink: jest.fn(),
2121
};
2222

@@ -121,10 +121,8 @@ describe('elements/content-sharing/hooks/useSharingService', () => {
121121
expect(result.current.sharingService).toBe(mockSharingService);
122122
expect(createSharingService).toHaveBeenCalledWith({
123123
itemApiInstance: {},
124-
onSuccess: {
125-
handleRemoveSharedLinkSuccess: expect.any(Function),
126-
handleUpdateSharedLinkSuccess: expect.any(Function),
127-
},
124+
onUpdateSharedLink: expect.any(Function),
125+
onRemoveSharedLink: expect.any(Function),
128126
options: {
129127
access: mockSharedLink.access,
130128
isDownloadAvailable: mockSharedLink.settings.isDownloadAvailable,
@@ -139,9 +137,9 @@ describe('elements/content-sharing/hooks/useSharingService', () => {
139137
(convertItemResponse as jest.Mock).mockReturnValue(mockConvertedData);
140138
renderHookWithProps();
141139

142-
// Get the onSuccess callbacks that were passed to mock createSharingService
143-
const onSuccessCallbacks = (createSharingService as jest.Mock).mock.calls[0][0].onSuccess;
144-
onSuccessCallbacks.handleUpdateSharedLinkSuccess(mockConvertedData);
140+
// Get the callbacks that were passed to mock createSharingService
141+
const createSharingServiceArgs = (createSharingService as jest.Mock).mock.calls[0][0];
142+
createSharingServiceArgs.onUpdateSharedLink(mockConvertedData);
145143

146144
expect(convertItemResponse).toHaveBeenCalledWith(mockConvertedData);
147145
expect(mockSetItem).toHaveBeenCalledTimes(1);
@@ -152,9 +150,9 @@ describe('elements/content-sharing/hooks/useSharingService', () => {
152150
(convertItemResponse as jest.Mock).mockReturnValue(mockConvertedData);
153151
renderHookWithProps();
154152

155-
// Get the onSuccess callbacks that were passed to mock createSharingService
156-
const onSuccessCallbacks = (createSharingService as jest.Mock).mock.calls[0][0].onSuccess;
157-
onSuccessCallbacks.handleRemoveSharedLinkSuccess(mockConvertedData);
153+
// Get the callbacks that were passed to mock createSharingService
154+
const createSharingServiceArgs = (createSharingService as jest.Mock).mock.calls[0][0];
155+
createSharingServiceArgs.onRemoveSharedLink(mockConvertedData);
158156

159157
expect(convertItemResponse).toHaveBeenCalledWith(mockConvertedData);
160158
expect(mockSetItem).toHaveBeenCalledTimes(1);
@@ -175,10 +173,8 @@ describe('elements/content-sharing/hooks/useSharingService', () => {
175173
expect(result.current.sharingService).toBe(mockSharingService);
176174
expect(createSharingService).toHaveBeenCalledWith({
177175
itemApiInstance: {},
178-
onSuccess: {
179-
handleRemoveSharedLinkSuccess: expect.any(Function),
180-
handleUpdateSharedLinkSuccess: expect.any(Function),
181-
},
176+
onUpdateSharedLink: expect.any(Function),
177+
onRemoveSharedLink: expect.any(Function),
182178
options: {
183179
access: mockSharedLink.access,
184180
isDownloadAvailable: mockSharedLink.settings.isDownloadAvailable,
@@ -193,9 +189,9 @@ describe('elements/content-sharing/hooks/useSharingService', () => {
193189
(convertItemResponse as jest.Mock).mockReturnValue(mockConvertedData);
194190
renderHookWithProps({ itemType: TYPE_FOLDER });
195191

196-
// Get the onSuccess callbacks that were passed to mock createSharingService
197-
const onSuccessCallbacks = (createSharingService as jest.Mock).mock.calls[0][0].onSuccess;
198-
onSuccessCallbacks.handleUpdateSharedLinkSuccess(mockConvertedData);
192+
// Get the callbacks that were passed to mock createSharingService
193+
const createSharingServiceArgs = (createSharingService as jest.Mock).mock.calls[0][0];
194+
createSharingServiceArgs.onUpdateSharedLink(mockConvertedData);
199195

200196
expect(convertItemResponse).toHaveBeenCalledWith(mockConvertedData);
201197
expect(mockSetItem).toHaveBeenCalledTimes(1);
@@ -206,9 +202,9 @@ describe('elements/content-sharing/hooks/useSharingService', () => {
206202
(convertItemResponse as jest.Mock).mockReturnValue(mockConvertedData);
207203
renderHookWithProps({ itemType: TYPE_FOLDER });
208204

209-
// Get the onSuccess callbacks that were passed to mock createSharingService
210-
const onSuccessCallbacks = (createSharingService as jest.Mock).mock.calls[0][0].onSuccess;
211-
onSuccessCallbacks.handleRemoveSharedLinkSuccess(mockConvertedData);
205+
// Get the callbacks that were passed to mock createSharingService
206+
const createSharingServiceArgs = (createSharingService as jest.Mock).mock.calls[0][0];
207+
createSharingServiceArgs.onRemoveSharedLink(mockConvertedData);
212208

213209
expect(convertItemResponse).toHaveBeenCalledWith(mockConvertedData);
214210
expect(mockSetItem).toHaveBeenCalledTimes(1);

src/elements/content-sharing/hooks/useSharingService.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@ export const useSharingService = ({
4343
isDownloadAvailable: sharedLink.settings?.isDownloadAvailable ?? false,
4444
};
4545

46-
const handleUpdateSharedLinkSuccess = updatedItemData => {
46+
const handleUpdateSharedLink = updatedItemData => {
4747
const { item: updatedItem, sharedLink: updatedSharedLink } = convertItemResponse(updatedItemData);
4848
setItem(prevItem => ({ ...prevItem, ...updatedItem }));
4949
setSharedLink(prevSharedLink => ({ ...prevSharedLink, ...updatedSharedLink }));
5050
};
5151

52-
const handleRemoveSharedLinkSuccess = itemData => {
52+
const handleRemoveSharedLink = itemData => {
5353
const { item: updatedItem } = convertItemResponse(itemData);
5454
setItem(prevItem => ({ ...prevItem, ...updatedItem }));
5555
setSharedLink({});
5656
};
5757

58-
const onSuccess = {
59-
handleUpdateSharedLinkSuccess,
60-
handleRemoveSharedLinkSuccess,
61-
};
62-
63-
return createSharingService({ itemApiInstance, onSuccess, options });
58+
return createSharingService({
59+
itemApiInstance,
60+
onUpdateSharedLink: handleUpdateSharedLink,
61+
onRemoveSharedLink: handleRemoveSharedLink,
62+
options,
63+
});
6464
}, [itemApiInstance, itemId, sharedLink, sharingServiceProps, setItem, setSharedLink]);
6565

6666
return { sharingService };

src/elements/content-sharing/sharingService.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,26 @@ export interface Options extends ItemData {
1919
serverURL?: string;
2020
}
2121

22-
export interface CreateSharingServiceProps {
22+
export interface CreateSharingServiceArgs {
2323
itemApiInstance: API;
24-
onSuccess: {
25-
handleUpdateSharedLinkSuccess: (itemData: ItemData) => void;
26-
handleRemoveSharedLinkSuccess: (itemData: ItemData) => void;
27-
};
24+
onUpdateSharedLink: (itemData: ItemData) => void;
25+
onRemoveSharedLink: (itemData: ItemData) => void;
2826
options: Options;
2927
}
3028

31-
export const createSharingService = ({ itemApiInstance, onSuccess, options }: CreateSharingServiceProps) => {
29+
export const createSharingService = ({
30+
itemApiInstance,
31+
onUpdateSharedLink,
32+
onRemoveSharedLink,
33+
options,
34+
}: CreateSharingServiceArgs) => {
3235
const { id, permissions } = options;
33-
const { handleUpdateSharedLinkSuccess, handleRemoveSharedLinkSuccess } = onSuccess;
3436

3537
const changeSharedLinkAccess = async (access: string) => {
3638
return itemApiInstance.share(
3739
{ id, permissions },
3840
access,
39-
handleUpdateSharedLinkSuccess,
41+
onUpdateSharedLink,
4042
{},
4143
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
4244
);
@@ -46,7 +48,7 @@ export const createSharingService = ({ itemApiInstance, onSuccess, options }: Cr
4648
return itemApiInstance.updateSharedLink(
4749
{ id, permissions },
4850
{ permissions: convertSharedLinkPermissions(permissionLevel) },
49-
handleUpdateSharedLinkSuccess,
51+
onUpdateSharedLink,
5052
{},
5153
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
5254
);
@@ -58,7 +60,7 @@ export const createSharingService = ({ itemApiInstance, onSuccess, options }: Cr
5860
return itemApiInstance.updateSharedLink(
5961
{ id, permissions },
6062
convertSharedLinkSettings(sharedLinkSettings, access, isDownloadAvailable, serverURL),
61-
handleUpdateSharedLinkSuccess,
63+
onUpdateSharedLink,
6264
{},
6365
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
6466
);
@@ -67,8 +69,8 @@ export const createSharingService = ({ itemApiInstance, onSuccess, options }: Cr
6769
const createSharedLink = async () => {
6870
return itemApiInstance.share(
6971
{ id, permissions },
70-
undefined,
71-
handleUpdateSharedLinkSuccess,
72+
options.access ?? undefined, // if "access" is undefined, the backend will set the default access level for the shared link
73+
onUpdateSharedLink,
7274
{},
7375
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
7476
);
@@ -78,17 +80,17 @@ export const createSharingService = ({ itemApiInstance, onSuccess, options }: Cr
7880
return itemApiInstance.share(
7981
{ id, permissions },
8082
ACCESS_NONE,
81-
handleRemoveSharedLinkSuccess,
83+
onRemoveSharedLink,
8284
{},
8385
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
8486
);
8587
};
8688

8789
return {
88-
deleteSharedLink,
90+
createSharedLink,
8991
changeSharedLinkAccess,
9092
changeSharedLinkPermission,
91-
createSharedLink,
93+
deleteSharedLink,
9294
updateSharedLink,
9395
};
9496
};

0 commit comments

Comments
 (0)