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 {
import { UPDATE_PERSON, updatePerson } from '../update'; GraphqlMutationPerson,
import { GraphqlQueryPerson } from '../../../interfaces/person.interface'; GraphqlQueryPerson,
} from '../../../interfaces/person.interface';
import { updatePerson } from '../update';
const mockClient = createMockClient(); jest.mock('../../../apollo', () => {
const personInterface = jest.requireActual(
const PERSON_RESULT = { '../../../interfaces/person.interface',
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 }),
); );
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 () => { it('updates a person', async () => {
const result = await updatePerson( const result = await updatePerson({
{ fullName: 'John Doe',
fullName: 'John Doe', id: 1,
id: 1, email: 'john@example.com',
email: 'john@example.com', company: {
company: { id: 2,
id: 2, name: 'ACME',
name: 'ACME', domain: 'example.com',
domain: 'example.com',
},
phone: '+1 (555) 123-4567',
pipe: {
id: 3,
name: 'Customer',
icon: '!',
},
creationDate: new Date(),
city: 'San Francisco',
countryCode: 'US',
}, },
mockClient, phone: '+1 (555) 123-4567',
); pipe: {
expect(result.data.email).toBe('john@example.com'); 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 { Person, mapGqlPerson } from '../../interfaces/person.interface';
import { apiClient } from '../../apollo'; import { apiClient } from '../../apollo';
@ -43,9 +43,8 @@ export const UPDATE_PERSON = gql`
export async function updatePerson( export async function updatePerson(
person: Person, person: Person,
client: ApolloClient<NormalizedCacheObject> = apiClient, ): Promise<FetchResult<Person>> {
) { const result = await apiClient.mutate({
const result = await client.mutate({
mutation: UPDATE_PERSON, mutation: UPDATE_PERSON,
variables: mapGqlPerson(person), variables: mapGqlPerson(person),
}); });