Mock apollo client.
Remove unit test argument.
This commit is contained in:
@ -1,54 +1,46 @@
|
||||
import { createMockClient, RequestHandlerResponse } from 'mock-apollo-client';
|
||||
import { UPDATE_PERSON, updatePerson } from '../update';
|
||||
import { GraphqlQueryPerson } from '../../../interfaces/person.interface';
|
||||
import {
|
||||
GraphqlMutationPerson,
|
||||
GraphqlQueryPerson,
|
||||
} from '../../../interfaces/person.interface';
|
||||
import { updatePerson } from '../update';
|
||||
|
||||
const mockClient = createMockClient();
|
||||
|
||||
const PERSON_RESULT = {
|
||||
city: 'San Francisco',
|
||||
company: {
|
||||
company_domain: 'example.com',
|
||||
company_name: 'ACME',
|
||||
id: 1,
|
||||
__typename: 'Company',
|
||||
},
|
||||
email: 'john@example.com',
|
||||
firstname: 'John',
|
||||
id: 1,
|
||||
lastname: 'Doe',
|
||||
phone: '+1 (555) 123-4567',
|
||||
__typename: 'Person',
|
||||
created_at: 'today',
|
||||
};
|
||||
|
||||
mockClient.setRequestHandler(UPDATE_PERSON, () => {
|
||||
return new Promise<RequestHandlerResponse<GraphqlQueryPerson>>((resolve) =>
|
||||
resolve({ data: PERSON_RESULT }),
|
||||
jest.mock('../../../apollo', () => {
|
||||
const personInterface = jest.requireActual(
|
||||
'../../../interfaces/person.interface',
|
||||
);
|
||||
return {
|
||||
apiClient: {
|
||||
mutate: (arg: {
|
||||
mutation: unknown;
|
||||
variables: GraphqlMutationPerson;
|
||||
}) => {
|
||||
const gqlPerson = arg.variables as unknown as GraphqlQueryPerson;
|
||||
return { data: personInterface.mapPerson(gqlPerson) };
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
it('updates a person', async () => {
|
||||
const result = await updatePerson(
|
||||
{
|
||||
fullName: 'John Doe',
|
||||
id: 1,
|
||||
email: 'john@example.com',
|
||||
company: {
|
||||
id: 2,
|
||||
name: 'ACME',
|
||||
domain: 'example.com',
|
||||
},
|
||||
phone: '+1 (555) 123-4567',
|
||||
pipe: {
|
||||
id: 3,
|
||||
name: 'Customer',
|
||||
icon: '!',
|
||||
},
|
||||
creationDate: new Date(),
|
||||
city: 'San Francisco',
|
||||
countryCode: 'US',
|
||||
const result = await updatePerson({
|
||||
fullName: 'John Doe',
|
||||
id: 1,
|
||||
email: 'john@example.com',
|
||||
company: {
|
||||
id: 2,
|
||||
name: 'ACME',
|
||||
domain: 'example.com',
|
||||
},
|
||||
mockClient,
|
||||
);
|
||||
expect(result.data.email).toBe('john@example.com');
|
||||
phone: '+1 (555) 123-4567',
|
||||
pipe: {
|
||||
id: 3,
|
||||
name: 'Customer',
|
||||
icon: '!',
|
||||
},
|
||||
creationDate: new Date(),
|
||||
city: 'San Francisco',
|
||||
countryCode: 'US',
|
||||
});
|
||||
expect(result.data).toBeDefined();
|
||||
result.data && expect(result.data.email).toBe('john@example.com');
|
||||
});
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { ApolloClient, NormalizedCacheObject, gql } from '@apollo/client';
|
||||
import { FetchResult, gql } from '@apollo/client';
|
||||
import { Person, mapGqlPerson } from '../../interfaces/person.interface';
|
||||
import { apiClient } from '../../apollo';
|
||||
|
||||
@ -43,9 +43,8 @@ export const UPDATE_PERSON = gql`
|
||||
|
||||
export async function updatePerson(
|
||||
person: Person,
|
||||
client: ApolloClient<NormalizedCacheObject> = apiClient,
|
||||
) {
|
||||
const result = await client.mutate({
|
||||
): Promise<FetchResult<Person>> {
|
||||
const result = await apiClient.mutate({
|
||||
mutation: UPDATE_PERSON,
|
||||
variables: mapGqlPerson(person),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user