Skip to content
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

chore(js-ts): Convert content-sharing hooks to TypeScript #3934

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
const handleSuccess = jest.fn().mockReturnValue(MOCK_COLLABS_CONVERTED_RESPONSE);
const handleError = jest.fn();

function FakeComponent({ api, itemType }: { api: API, itemType: string }) {
function FakeComponent({ api, itemType }: { api: API; itemType: string }) {
const [collaboratorsList, setCollaboratorsList] = React.useState(null);

const collabsResponse = useCollaborators(api, MOCK_ITEM_ID, itemType, { handleSuccess, handleError });
Expand Down Expand Up @@ -53,11 +53,7 @@ describe('elements/content-sharing/hooks/useCollaborators', () => {
fakeComponent = mount(<FakeComponent api={mockAPI} itemType={itemType} />);
});
fakeComponent.update();
expect(getCollaborations).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
);
expect(getCollaborations).toHaveBeenCalledWith(MOCK_ITEM_ID, expect.anything(), expect.anything());
expect(handleSuccess).toHaveBeenCalled();
expect(fakeComponent.find('div').text()).toBe(STRINGIFIED_MOCK_RESPONSE);
});
Expand Down Expand Up @@ -85,11 +81,7 @@ describe('elements/content-sharing/hooks/useCollaborators', () => {
fakeComponent = mount(<FakeComponent api={mockAPI} itemType={itemType} />);
});
fakeComponent.update();
expect(getCollaborations).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
);
expect(getCollaborations).toHaveBeenCalledWith(MOCK_ITEM_ID, expect.anything(), expect.anything());
expect(handleError).toHaveBeenCalled();
expect(fakeComponent.find('div').text()).toBe(JSON.stringify({ entries: [], next_marker: null }));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// @flow

import React, { act } from 'react';
import { mount } from 'enzyme';
import API from '../../../api';
import { GetContactsByEmailFnType } from '../types';
import useContactsByEmail from '../hooks/useContactsByEmail';
import {
MOCK_CONTACTS_API_RESPONSE,
Expand All @@ -14,12 +13,12 @@ const handleSuccess = jest.fn();
const handleError = jest.fn();
const transformUsersSpy = jest.fn().mockReturnValue(MOCK_CONTACTS_BY_EMAIL_CONVERTED_RESPONSE);

const createAPIMock = markerBasedUsersAPI => ({
const createAPIMock = (markerBasedUsersAPI: Record<string, unknown>): { getMarkerBasedUsersAPI: jest.Mock } => ({
getMarkerBasedUsersAPI: jest.fn().mockReturnValue(markerBasedUsersAPI),
});

function FakeComponent({ api, transformUsers }: { api: API, transformUsers: Function }) {
const [getContactsByEmail, setGetContactsByEmail] = React.useState(null);
function FakeComponent({ api, transformUsers }: { api: API; transformUsers?: Function }) {
const [getContactsByEmail, setGetContactsByEmail] = React.useState<GetContactsByEmailFnType | null>(null);

const updatedGetContactsByEmailFn = useContactsByEmail(api, MOCK_ITEM_ID, {
handleSuccess,
Expand Down Expand Up @@ -63,12 +62,9 @@ describe('elements/content-sharing/hooks/useContactsByEmail', () => {

const contacts = fakeComponent.find('button').invoke('onClick')({ emails: [MOCK_EMAIL] });

expect(getUsersInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ filter_term: MOCK_EMAIL },
);
expect(getUsersInEnterprise).toHaveBeenCalledWith(MOCK_ITEM_ID, expect.anything(), expect.anything(), {
filter_term: MOCK_EMAIL,
});
expect(handleSuccess).toHaveBeenCalledWith(MOCK_CONTACTS_API_RESPONSE);
expect(transformUsersSpy).toHaveBeenCalledWith(MOCK_CONTACTS_API_RESPONSE);
return expect(contacts).resolves.toEqual(MOCK_CONTACTS_BY_EMAIL_CONVERTED_RESPONSE);
Expand All @@ -83,12 +79,9 @@ describe('elements/content-sharing/hooks/useContactsByEmail', () => {

const contacts = fakeComponent.find('button').invoke('onClick')({ emails: [MOCK_EMAIL] });

expect(getUsersInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ filter_term: MOCK_EMAIL },
);
expect(getUsersInEnterprise).toHaveBeenCalledWith(MOCK_ITEM_ID, expect.anything(), expect.anything(), {
filter_term: MOCK_EMAIL,
});
expect(handleSuccess).toHaveBeenCalledWith(MOCK_CONTACTS_API_RESPONSE);
expect(transformUsersSpy).not.toHaveBeenCalled();
expect(contacts).resolves.toEqual(MOCK_CONTACTS_API_RESPONSE.entries);
Expand Down Expand Up @@ -154,12 +147,9 @@ describe('elements/content-sharing/hooks/useContactsByEmail', () => {

const contacts = fakeComponent.find('button').invoke('onClick')({ emails: [MOCK_EMAIL] });

expect(getUsersInEnterprise).toHaveBeenCalledWith(
MOCK_ITEM_ID,
expect.anything(Function),
expect.anything(Function),
{ filter_term: MOCK_EMAIL },
);
expect(getUsersInEnterprise).toHaveBeenCalledWith(MOCK_ITEM_ID, expect.anything(), expect.anything(), {
filter_term: MOCK_EMAIL,
});
expect(handleError).toHaveBeenCalled();
expect(contacts).resolves.toBeFalsy();
});
Expand Down
Loading