Added update test

This commit is contained in:
Anders Borch
2023-04-24 22:55:34 +02:00
parent 8ed09d61ef
commit 443f8ed663
4 changed files with 72 additions and 4 deletions

View File

@ -55,6 +55,7 @@
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-react": "^7.31.11",
"eslint-plugin-storybook": "^0.6.11",
"mock-apollo-client": "^1.2.1",
"prettier": "^2.8.0",
"prop-types": "^15.8.1",
"react-scripts": "5.0.1",
@ -19710,6 +19711,15 @@
"integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==",
"dev": true
},
"node_modules/mock-apollo-client": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/mock-apollo-client/-/mock-apollo-client-1.2.1.tgz",
"integrity": "sha512-QYQ6Hxo+t7hard1bcHHbsHxlNQYTQsaMNsm2Psh/NbwLMi2R4tGzplJKt97MUWuARHMq3GHB4PTLj/gxej4Caw==",
"dev": true,
"peerDependencies": {
"@apollo/client": "^3.0.0"
}
},
"node_modules/mri": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/mri/-/mri-1.2.0.tgz",

View File

@ -105,6 +105,7 @@
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-react": "^7.31.11",
"eslint-plugin-storybook": "^0.6.11",
"mock-apollo-client": "^1.2.1",
"prettier": "^2.8.0",
"prop-types": "^15.8.1",
"react-scripts": "5.0.1",

View File

@ -0,0 +1,54 @@
import { createMockClient, RequestHandlerResponse } from 'mock-apollo-client';
import { UPDATE_PERSON, updatePerson } from '../update';
import { GraphqlQueryPerson } from '../../../interfaces/person.interface';
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 }),
);
});
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',
},
mockClient,
);
expect(result.data.email).toBe('john@example.com');
});

View File

@ -1,8 +1,8 @@
import { gql } from '@apollo/client';
import { ApolloClient, NormalizedCacheObject, gql } from '@apollo/client';
import { Person, mapGqlPerson } from '../../interfaces/person.interface';
import { apiClient } from '../../apollo';
const UPDATE_PERSON = gql`
export const UPDATE_PERSON = gql`
mutation UpdatePeople(
$id: Int
$firstname: String
@ -41,8 +41,11 @@ const UPDATE_PERSON = gql`
}
`;
export async function updatePerson(person: Person) {
const result = await apiClient.mutate({
export async function updatePerson(
person: Person,
client: ApolloClient<NormalizedCacheObject> = apiClient,
) {
const result = await client.mutate({
mutation: UPDATE_PERSON,
variables: mapGqlPerson(person),
});