Mock apollo client.

Remove unit test argument.
This commit is contained in:
Anders Borch
2023-04-25 12:51:42 +02:00
parent a6254197b1
commit 463b5f4ec9
2 changed files with 42 additions and 51 deletions

View File

@ -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');
});

View File

@ -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),
});