Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/elements/content-sharing/ContentSharing.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function ContentSharing({
api && (
<ContentSharingV2
api={api}
itemID={itemID}
itemId={itemID}
itemType={itemType}
hasProviders={hasProviders}
language={language}
Expand Down
42 changes: 21 additions & 21 deletions src/elements/content-sharing/ContentSharingV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export interface ContentSharingV2Props {
api: API;
/** children - Children for the element to open the Unified Share Modal */
children?: React.ReactElement;
/** itemID - Box file or folder ID */
itemID: string;
/** itemId - Box file or folder ID */
itemId: string;
/** itemType - "file" or "folder" */
itemType: ItemType;
/** hasProviders - Whether the element has providers for USM already */
Expand All @@ -35,13 +35,13 @@ export interface ContentSharingV2Props {
function ContentSharingV2({
api,
children,
itemID,
itemId,
itemType,
hasProviders,
language,
messages,
}: ContentSharingV2Props) {
const [avatarURLMap, setAvatarURLMap] = React.useState<AvatarURLMap | null>(null);
const [avatarUrlMap, setAvatarUrlMap] = React.useState<AvatarURLMap | null>(null);
const [item, setItem] = React.useState<Item | null>(null);
const [sharedLink, setSharedLink] = React.useState<SharedLink | null>(null);
const [sharingServiceProps, setSharingServiceProps] = React.useState(null);
Expand All @@ -53,19 +53,19 @@ function ContentSharingV2({

const { sharingService } = useSharingService({
api,
avatarURLMap,
avatarUrlMap,
collaborators,
currentUserId: currentUser?.id,
item,
itemId: itemID,
itemId,
itemType,
sharedLink,
sharingServiceProps,
setCollaborators,
setItem,
setSharedLink,
});
const { contactService } = useContactService(api, itemID, currentUser?.id);
const { contactService } = useContactService(api, itemId, currentUser?.id);

// Handle successful GET requests to /files or /folders
const handleGetItemSuccess = React.useCallback(itemData => {
Expand All @@ -90,7 +90,7 @@ function ContentSharingV2({
setSharedLink(null);
setCurrentUser(null);
setCollaborationRoles(null);
setAvatarURLMap(null);
setAvatarUrlMap(null);
setCollaborators(null);
setCollaboratorsData(null);
}, [api]);
Expand All @@ -100,10 +100,10 @@ function ContentSharingV2({
if (!api || isEmpty(api) || item) return;

(async () => {
const itemData = await fetchItem({ api, itemID, itemType });
const itemData = await fetchItem({ api, itemId, itemType });
handleGetItemSuccess(itemData);
})();
}, [api, item, itemID, itemType, sharedLink, handleGetItemSuccess]);
}, [api, item, itemId, itemType, sharedLink, handleGetItemSuccess]);

// Get current user
React.useEffect(() => {
Expand All @@ -122,28 +122,28 @@ function ContentSharingV2({
};

(async () => {
const userData = await fetchCurrentUser({ api, itemID });
const userData = await fetchCurrentUser({ api, itemId });
getUserSuccess(userData);
})();
}, [api, currentUser, item, itemID, itemType, sharedLink]);
}, [api, currentUser, item, itemId, itemType, sharedLink]);

// Get collaborators
React.useEffect(() => {
if (!api || isEmpty(api) || !item || collaboratorsData) return;

(async () => {
try {
const response = await fetchCollaborators({ api, itemID, itemType });
const response = await fetchCollaborators({ api, itemId, itemType });
setCollaboratorsData(response);
} catch {
setCollaboratorsData({ entries: [], next_marker: null });
}
})();
}, [api, collaboratorsData, item, itemID, itemType]);
}, [api, collaboratorsData, item, itemId, itemType]);

// Get avatars when collaborators are available
React.useEffect(() => {
if (avatarURLMap || !collaboratorsData || !collaboratorsData.entries || !owner.id) return;
if (avatarUrlMap || !collaboratorsData || !collaboratorsData.entries || !owner.id) return;
(async () => {
const ownerEntry = {
accessible_by: {
Expand All @@ -154,24 +154,24 @@ function ContentSharingV2({
};
const response = await fetchAvatars({
api,
itemID,
itemId,
collaborators: [...collaboratorsData.entries, ownerEntry],
});
setAvatarURLMap(response);
setAvatarUrlMap(response);
})();
}, [api, avatarURLMap, collaboratorsData, itemID, owner]);
}, [api, avatarUrlMap, collaboratorsData, itemId, owner]);

React.useEffect(() => {
if (avatarURLMap && collaboratorsData && currentUser && owner) {
if (avatarUrlMap && collaboratorsData && currentUser && owner) {
const collaboratorsWithAvatars = convertCollabsResponse(
collaboratorsData,
currentUser.id,
owner,
avatarURLMap,
avatarUrlMap,
);
setCollaborators(collaboratorsWithAvatars);
}
}, [avatarURLMap, collaboratorsData, currentUser, owner]);
}, [avatarUrlMap, collaboratorsData, currentUser, owner]);

const config = { sharedLinkEmail: false };

Expand Down
24 changes: 12 additions & 12 deletions src/elements/content-sharing/__tests__/ContentSharingV2.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import { CONTENT_SHARING_ITEM_FIELDS } from '../constants';

import ContentSharingV2 from '../ContentSharingV2';

const createAPIMock = (fileAPI, folderAPI, usersAPI, collaborationsAPI) => ({
getFileAPI: jest.fn().mockReturnValue(fileAPI),
getFolderAPI: jest.fn().mockReturnValue(folderAPI),
getUsersAPI: jest.fn().mockReturnValue(usersAPI),
getFileCollaborationsAPI: jest.fn().mockReturnValue(collaborationsAPI),
const createApiMock = (fileApi, folderApi, usersApi, collaborationsApi) => ({
getFileAPI: jest.fn().mockReturnValue(fileApi),
getFolderAPI: jest.fn().mockReturnValue(folderApi),
getUsersAPI: jest.fn().mockReturnValue(usersApi),
getFileCollaborationsAPI: jest.fn().mockReturnValue(collaborationsApi),
});

const createSuccessMock = responseFromAPI => (id, successFn) => {
return Promise.resolve(responseFromAPI).then(response => {
const createSuccessMock = responseFromApi => (id, successFn) => {
return Promise.resolve(responseFromApi).then(response => {
successFn(response);
});
};
Expand All @@ -40,7 +40,7 @@ const getDefaultFolderMock = jest.fn().mockImplementation(createSuccessMock(DEFA
const getCollaborationsMock = jest.fn().mockImplementation(createSuccessMock(MOCK_COLLABORATIONS_RESPONSE));
const getAvatarUrlMock = jest.fn().mockImplementation(userID => mockAvatarURLMap[userID] ?? null);

const defaultAPIMock = createAPIMock(
const defaultApiMock = createApiMock(
{ getFile: getDefaultFileMock },
{ getFolderFields: getDefaultFolderMock },
{ getUser: getDefaultUserMock, getAvatarUrlWithAccessToken: getAvatarUrlMock },
Expand All @@ -54,8 +54,8 @@ jest.mock('../hooks/useSharingService', () => ({
const renderComponent = (props = {}): RenderResult =>
render(
<ContentSharingV2
api={defaultAPIMock}
itemID={MOCK_ITEM.id}
api={defaultApiMock}
itemId={MOCK_ITEM.id}
itemType={MOCK_ITEM.type}
hasProviders={true}
{...props}
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('elements/content-sharing/ContentSharingV2', () => {

test('should see the shared link elements if shared link is present', async () => {
const apiWithSharedLink = {
...defaultAPIMock,
...defaultApiMock,
getFileAPI: jest.fn().mockReturnValue({ getFile: getFileMockWithSharedLink }),
};
renderComponent({ api: apiWithSharedLink });
Expand All @@ -122,7 +122,7 @@ describe('elements/content-sharing/ContentSharingV2', () => {

test('should see the classification elements if classification is present', async () => {
const apiWithClassification = {
...defaultAPIMock,
...defaultApiMock,
getFileAPI: jest.fn().mockReturnValue({ getFile: getFileMockWithClassification }),
};

Expand Down
22 changes: 11 additions & 11 deletions src/elements/content-sharing/apis/__tests__/fetchAvatars.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DEFAULT_USER_API_RESPONSE, MOCK_ITEM } from '../../utils/__mocks__/ContentSharingV2Mocks';
import { fetchAvatars } from '..';
import { createSuccessMock, createUsersAPIMock } from './testUtils';
import { createSuccessMock, createUsersApiMock } from './testUtils';

const getAvatarUrlMock = jest.fn();
const getDefaultUserMock = jest.fn().mockImplementation(createSuccessMock(DEFAULT_USER_API_RESPONSE));
const defaultAPIMock = createUsersAPIMock({
const defaultApiMock = createUsersApiMock({
getUser: getDefaultUserMock,
getAvatarUrlWithAccessToken: getAvatarUrlMock,
});
Expand All @@ -27,12 +27,12 @@ describe('content-sharing/apis/fetchAvatars', () => {
.mockResolvedValueOnce('https://example.com/avatar3.jpg');

const result = await fetchAvatars({
api: defaultAPIMock,
itemID: MOCK_ITEM.id,
api: defaultApiMock,
itemId: MOCK_ITEM.id,
collaborators: mockCollaborations,
});

expect(defaultAPIMock.getUsersAPI).toHaveBeenCalledWith(false);
expect(defaultApiMock.getUsersAPI).toHaveBeenCalledWith(false);
expect(getAvatarUrlMock).toHaveBeenCalledTimes(3);
expect(getAvatarUrlMock).toHaveBeenCalledWith('123', MOCK_ITEM.id);
expect(getAvatarUrlMock).toHaveBeenCalledWith('456', MOCK_ITEM.id);
Expand All @@ -51,8 +51,8 @@ describe('content-sharing/apis/fetchAvatars', () => {
.mockResolvedValueOnce('https://example.com/avatar3.jpg');

const result = await fetchAvatars({
api: defaultAPIMock,
itemID: MOCK_ITEM.id,
api: defaultApiMock,
itemId: MOCK_ITEM.id,
collaborators: mockCollaborations,
});

Expand All @@ -69,8 +69,8 @@ describe('content-sharing/apis/fetchAvatars', () => {
getAvatarUrlMock.mockResolvedValue('https://example.com/avatar.jpg');

const result = await fetchAvatars({
api: defaultAPIMock,
itemID: MOCK_ITEM.id,
api: defaultApiMock,
itemId: MOCK_ITEM.id,
collaborators: collaboratorsWithMissingData,
});

Expand All @@ -83,8 +83,8 @@ describe('content-sharing/apis/fetchAvatars', () => {

test('should handle empty collaborators array', async () => {
const result = await fetchAvatars({
api: defaultAPIMock,
itemID: MOCK_ITEM.id,
api: defaultApiMock,
itemId: MOCK_ITEM.id,
collaborators: [],
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MOCK_COLLABORATIONS_RESPONSE, MOCK_ITEM } from '../../utils/__mocks__/ContentSharingV2Mocks';
import { TYPE_FILE, TYPE_FOLDER } from '../../../../constants';
import { fetchCollaborators } from '..';
import { createSuccessMock, createCollabAPIMock } from './testUtils';
import { createSuccessMock, createCollabApiMock } from './testUtils';

const getDefaultFileCollabMock = jest.fn().mockImplementation(createSuccessMock(MOCK_COLLABORATIONS_RESPONSE));
const getDefaultFolderCollabMock = jest.fn().mockImplementation(createSuccessMock(MOCK_COLLABORATIONS_RESPONSE));
const defaultAPIMock = createCollabAPIMock(
const defaultApiMock = createCollabApiMock(
{ getCollaborations: getDefaultFileCollabMock },
{ getCollaborations: getDefaultFolderCollabMock },
);
Expand All @@ -16,17 +16,17 @@ describe('content-sharing/apis/fetchCollaborators', () => {
});

test('should fetch file collaborators successfully', async () => {
const result = await fetchCollaborators({ api: defaultAPIMock, itemID: MOCK_ITEM.id, itemType: TYPE_FILE });
const result = await fetchCollaborators({ api: defaultApiMock, itemId: MOCK_ITEM.id, itemType: TYPE_FILE });

expect(defaultAPIMock.getFileCollaborationsAPI).toHaveBeenCalledWith(false);
expect(defaultApiMock.getFileCollaborationsAPI).toHaveBeenCalledWith(false);
expect(getDefaultFileCollabMock).toHaveBeenCalledWith(MOCK_ITEM.id, expect.any(Function), expect.any(Function));
expect(result).toEqual(MOCK_COLLABORATIONS_RESPONSE);
});

test('should fetch folder collaborators successfully', async () => {
await fetchCollaborators({ api: defaultAPIMock, itemID: MOCK_ITEM.id, itemType: TYPE_FOLDER });
await fetchCollaborators({ api: defaultApiMock, itemId: MOCK_ITEM.id, itemType: TYPE_FOLDER });

expect(defaultAPIMock.getFolderCollaborationsAPI).toHaveBeenCalledWith(false);
expect(defaultApiMock.getFolderCollaborationsAPI).toHaveBeenCalledWith(false);
expect(getDefaultFolderCollabMock).toHaveBeenCalledWith(
MOCK_ITEM.id,
expect.any(Function),
Expand All @@ -35,10 +35,10 @@ describe('content-sharing/apis/fetchCollaborators', () => {
});

test('should return null for non file or folder type', async () => {
const result = await fetchCollaborators({ api: defaultAPIMock, itemID: MOCK_ITEM.id, itemType: 'hubs' });
const result = await fetchCollaborators({ api: defaultApiMock, itemId: MOCK_ITEM.id, itemType: 'hubs' });

expect(result).toBeNull();
expect(defaultAPIMock.getFileCollaborationsAPI).not.toHaveBeenCalled();
expect(defaultAPIMock.getFolderCollaborationsAPI).not.toHaveBeenCalled();
expect(defaultApiMock.getFileCollaborationsAPI).not.toHaveBeenCalled();
expect(defaultApiMock.getFolderCollaborationsAPI).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { DEFAULT_USER_API_RESPONSE, MOCK_ITEM } from '../../utils/__mocks__/ContentSharingV2Mocks';
import { FIELD_ENTERPRISE, FIELD_HOSTNAME } from '../../../../constants';
import { fetchCurrentUser } from '..';
import { createSuccessMock, createUsersAPIMock } from './testUtils';
import { createSuccessMock, createUsersApiMock } from './testUtils';

const getDefaultUserMock = jest.fn().mockImplementation(createSuccessMock(DEFAULT_USER_API_RESPONSE));
const defaultAPIMock = createUsersAPIMock({ getUser: getDefaultUserMock });
const defaultApiMock = createUsersApiMock({ getUser: getDefaultUserMock });

describe('content-sharing/apis/fetchCurrentUser', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('should fetch current user successfully', async () => {
const result = await fetchCurrentUser({ api: defaultAPIMock, itemID: MOCK_ITEM.id });
const result = await fetchCurrentUser({ api: defaultApiMock, itemId: MOCK_ITEM.id });

expect(defaultAPIMock.getUsersAPI).toHaveBeenCalledWith(false);
expect(defaultApiMock.getUsersAPI).toHaveBeenCalledWith(false);
expect(getDefaultUserMock).toHaveBeenCalledWith(MOCK_ITEM.id, expect.any(Function), expect.any(Function), {
params: {
fields: [FIELD_ENTERPRISE, FIELD_HOSTNAME].toString(),
Expand All @@ -27,7 +27,7 @@ describe('content-sharing/apis/fetchCurrentUser', () => {
const errorMock = jest.fn().mockImplementation((userID, successCallback, errorCallback) => {
errorCallback(new Error('API Error'));
});
const errorAPIMock = createUsersAPIMock({ getUser: errorMock });
await expect(fetchCurrentUser({ api: errorAPIMock, itemID: MOCK_ITEM.id })).rejects.toThrow('API Error');
const errorApiMock = createUsersApiMock({ getUser: errorMock });
await expect(fetchCurrentUser({ api: errorApiMock, itemId: MOCK_ITEM.id })).rejects.toThrow('API Error');
});
});
Loading