-
Notifications
You must be signed in to change notification settings - Fork 344
feat(content-sharing): Create sharing service changeSharedLinkPermission #4332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0a4e404
feat(content-sharing): Create sharing service changeSharedLinkPermission
reneshen0328 e97bb3c
feat(content-sharing): Create sharing service changeSharedLinkPermission
reneshen0328 aa3e67a
feat(content-sharing): Create sharing service changeSharedLinkPermission
reneshen0328 b937d4c
feat(content-sharing): nits
reneshen0328 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/elements/content-sharing/__tests__/sharingService.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { PERMISSION_CAN_DOWNLOAD, PERMISSION_CAN_PREVIEW } from '../../../constants'; | ||
| import { CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS } from '../constants'; | ||
| import { convertSharedLinkPermissions, createSharingService } from '../sharingService'; | ||
|
|
||
| describe('elements/content-sharing/sharingService', () => { | ||
| describe('convertSharedLinkPermissions', () => { | ||
| test.each([ | ||
| [PERMISSION_CAN_DOWNLOAD, { [PERMISSION_CAN_DOWNLOAD]: true, [PERMISSION_CAN_PREVIEW]: false }], | ||
| [PERMISSION_CAN_PREVIEW, { [PERMISSION_CAN_DOWNLOAD]: false, [PERMISSION_CAN_PREVIEW]: true }], | ||
| ])('should return correct permissions for download permission level', (permissionLevel, expected) => { | ||
| const result = convertSharedLinkPermissions(permissionLevel); | ||
| expect(result).toEqual(expected); | ||
| }); | ||
|
|
||
| test('should handle empty string permission level', () => { | ||
| const result = convertSharedLinkPermissions(''); | ||
| expect(result).toEqual({}); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createSharingService', () => { | ||
| const mockItemApiInstance = { | ||
| updateSharedLink: jest.fn(), | ||
| }; | ||
| const mockItemData = { id: '123' }; | ||
| const mockOnSuccess = jest.fn(); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('should return an object with changeSharedLinkPermission method', () => { | ||
| const service = createSharingService({ | ||
| itemApiInstance: mockItemApiInstance, | ||
| itemData: mockItemData, | ||
| onSuccess: mockOnSuccess, | ||
| }); | ||
|
|
||
| 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, | ||
| itemData: mockItemData, | ||
| onSuccess: mockOnSuccess, | ||
| }); | ||
|
|
||
| const permissionLevel = PERMISSION_CAN_DOWNLOAD; | ||
| const expectedPermissions = { | ||
| [PERMISSION_CAN_DOWNLOAD]: true, | ||
| [PERMISSION_CAN_PREVIEW]: false, | ||
| }; | ||
|
|
||
| await service.changeSharedLinkPermission(permissionLevel); | ||
|
|
||
| expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith( | ||
| mockItemData, | ||
| { permissions: expectedPermissions }, | ||
| mockOnSuccess, | ||
| {}, | ||
| CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS, | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/elements/content-sharing/hooks/__tests__/useSharingService.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
|
|
||
| import { TYPE_FILE, TYPE_FOLDER } from '../../../../constants'; | ||
| import { createSharingService } from '../../sharingService'; | ||
| import { convertItemResponse } from '../../utils/convertItemResponse'; | ||
| import { useSharingService } from '../useSharingService'; | ||
|
|
||
| jest.mock('../../utils/convertItemResponse'); | ||
| jest.mock('../../sharingService'); | ||
|
|
||
| const mockApi = { | ||
| getFileAPI: jest.fn(), | ||
| getFolderAPI: jest.fn(), | ||
| }; | ||
| const mockItemApiInstance = { | ||
| updateSharedLink: jest.fn(), | ||
| }; | ||
| const mockSharingService = { | ||
| changeSharedLinkPermission: jest.fn(), | ||
| }; | ||
|
|
||
| const mockItemId = '123'; | ||
| const mockItem = { | ||
| id: mockItemId, | ||
| permissions: { | ||
| can_download: true, | ||
| can_preview: false, | ||
| }, | ||
| }; | ||
|
|
||
| const mockSetItem = jest.fn(); | ||
| const mockSetSharedLink = jest.fn(); | ||
|
|
||
| describe('elements/content-sharing/hooks/useSharingService', () => { | ||
| beforeEach(() => { | ||
| (createSharingService as jest.Mock).mockReturnValue(mockSharingService); | ||
| (convertItemResponse as jest.Mock).mockReturnValue({ | ||
| item: mockItem, | ||
| sharedLink: {}, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('should return null itemApiInstance and sharingService when item is null', () => { | ||
| const { result } = renderHook(() => | ||
| useSharingService(mockApi, null, mockItemId, TYPE_FILE, mockSetItem, mockSetSharedLink), | ||
| ); | ||
|
|
||
| expect(result.current.sharingService).toBeNull(); | ||
| expect(mockApi.getFileAPI).not.toHaveBeenCalled(); | ||
| expect(mockApi.getFolderAPI).not.toHaveBeenCalled(); | ||
| expect(createSharingService).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should return null itemApiInstance and sharingService when itemType is neither TYPE_FILE nor TYPE_FOLDER', () => { | ||
| const { result } = renderHook(() => | ||
| useSharingService(mockApi, mockItem, mockItemId, 'hubs', mockSetItem, mockSetSharedLink), | ||
| ); | ||
|
|
||
| expect(result.current.sharingService).toBeNull(); | ||
| expect(mockApi.getFileAPI).not.toHaveBeenCalled(); | ||
| expect(mockApi.getFolderAPI).not.toHaveBeenCalled(); | ||
| expect(createSharingService).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| describe('when itemType is TYPE_FILE', () => { | ||
| beforeEach(() => { | ||
| mockApi.getFileAPI.mockReturnValue(mockItemApiInstance); | ||
| }); | ||
|
|
||
| test('should create file API instance and sharing service', () => { | ||
| const { result } = renderHook(() => | ||
| useSharingService(mockApi, mockItem, mockItemId, TYPE_FILE, mockSetItem, mockSetSharedLink), | ||
| ); | ||
|
|
||
| expect(mockApi.getFileAPI).toHaveBeenCalled(); | ||
| expect(mockApi.getFolderAPI).not.toHaveBeenCalled(); | ||
| expect(result.current.sharingService).toBe(mockSharingService); | ||
| expect(createSharingService).toHaveBeenCalledWith({ | ||
| itemApiInstance: mockItemApiInstance, | ||
| itemData: { | ||
| id: mockItemId, | ||
| permissions: mockItem.permissions, | ||
| }, | ||
| onSuccess: expect.any(Function), | ||
| }); | ||
| }); | ||
|
|
||
| test('should handle success callback correctly', () => { | ||
| const mockConvertedData = { | ||
| item: { | ||
| id: mockItemId, | ||
| permissions: { can_download: false }, | ||
| }, | ||
| sharedLink: {}, | ||
| }; | ||
|
|
||
| (convertItemResponse as jest.Mock).mockReturnValue(mockConvertedData); | ||
| renderHook(() => | ||
| useSharingService(mockApi, mockItem, mockItemId, TYPE_FILE, mockSetItem, mockSetSharedLink), | ||
| ); | ||
|
|
||
| // Get the onSuccess callback that was passed to mock createSharingService | ||
| const onSuccessCallback = (createSharingService as jest.Mock).mock.calls[0][0].onSuccess; | ||
| onSuccessCallback(mockConvertedData); | ||
|
|
||
| expect(mockSetItem).toHaveBeenCalledTimes(1); | ||
| expect(mockSetSharedLink).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when itemType is TYPE_FOLDER', () => { | ||
| beforeEach(() => { | ||
| mockApi.getFolderAPI.mockReturnValue(mockItemApiInstance); | ||
| }); | ||
|
|
||
| test('should create folder API instance and sharing service', () => { | ||
| const { result } = renderHook(() => | ||
| useSharingService(mockApi, mockItem, mockItemId, TYPE_FOLDER, mockSetItem, mockSetSharedLink), | ||
| ); | ||
|
|
||
| expect(mockApi.getFolderAPI).toHaveBeenCalled(); | ||
| expect(mockApi.getFileAPI).not.toHaveBeenCalled(); | ||
| expect(result.current.sharingService).toBe(mockSharingService); | ||
| expect(createSharingService).toHaveBeenCalledWith({ | ||
| itemApiInstance: mockItemApiInstance, | ||
| itemData: { | ||
| id: mockItemId, | ||
| permissions: mockItem.permissions, | ||
| }, | ||
| onSuccess: expect.any(Function), | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import { TYPE_FILE, TYPE_FOLDER } from '../../../constants'; | ||
| import { convertItemResponse } from '../utils/convertItemResponse'; | ||
| import { createSharingService } from '../sharingService'; | ||
|
|
||
| export const useSharingService = (api, item, itemId, itemType, setItem, setSharedLink) => { | ||
| const itemApiInstance = React.useMemo(() => { | ||
| if (!item) { | ||
| return null; | ||
reneshen0328 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (itemType === TYPE_FILE) { | ||
| return api.getFileAPI(); | ||
| } | ||
|
|
||
| if (itemType === TYPE_FOLDER) { | ||
| return api.getFolderAPI(); | ||
| } | ||
|
|
||
| return null; | ||
| }, [api, item, itemType]); | ||
|
|
||
| const sharingService = React.useMemo(() => { | ||
| if (!itemApiInstance) { | ||
| return null; | ||
| } | ||
|
|
||
| const itemData = { | ||
| id: itemId, | ||
| permissions: item.permissions, | ||
| }; | ||
|
|
||
| const handleSuccess = updatedItemData => { | ||
| const { item: updatedItem, sharedLink: updatedSharedLink } = convertItemResponse(updatedItemData); | ||
| setItem(prevItem => ({ ...prevItem, ...updatedItem })); | ||
| setSharedLink(prevSharedLink => ({ ...prevSharedLink, ...updatedSharedLink })); | ||
| }; | ||
|
|
||
| return createSharingService({ itemApiInstance, itemData, onSuccess: handleSuccess }); | ||
| }, [itemApiInstance, item, itemId, setItem, setSharedLink]); | ||
|
|
||
| return { sharingService }; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.