Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 2e27efd

Browse files
committed
Add more Mutation error handling tests
1 parent 92cba32 commit 2e27efd

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

test/client/Mutation.test.tsx

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
33
import gql from 'graphql-tag';
4-
import { ApolloClient } from 'apollo-client';
4+
import { ApolloClient, ApolloError } from 'apollo-client';
55
import { InMemoryCache as Cache } from 'apollo-cache-inmemory';
66
import { DataProxy } from 'apollo-cache';
77
import { ExecutionResult, GraphQLError } from 'graphql';
@@ -380,8 +380,11 @@ it('renders an error state', done => {
380380
);
381381
});
382382

383-
it('renders an error state when encountering graphql errors', done => {
383+
it('renders an error state and throws when encountering graphql errors', done => {
384384
let count = 0;
385+
386+
const expectedError = new ApolloError({ graphQLErrors: [new GraphQLError('error occurred')] });
387+
385388
const Component = () => (
386389
<Mutation mutation={mutation}>
387390
{(createTodo, result) => {
@@ -392,13 +395,13 @@ it('renders an error state when encountering graphql errors', done => {
392395
done.fail('Did not expect a result');
393396
})
394397
.catch(e => {
395-
expect(e).toEqual(new Error('GraphQL error: error occurred'));
398+
expect(e).toEqual(expectedError);
396399
}),
397400
);
398401
} else if (count === 1) {
399402
expect(result.loading).toBeTruthy();
400403
} else if (count === 2) {
401-
expect(result.error).toEqual(new Error('GraphQL error: error occurred'));
404+
expect(result.error).toEqual(expectedError);
402405
done();
403406
}
404407
count++;
@@ -423,7 +426,7 @@ it('renders an error state when encountering graphql errors', done => {
423426
);
424427
});
425428

426-
it('renders an error state when encountering graphql errors when errorPolicy=all', done => {
429+
it('renders an error state and does not throw when encountering graphql errors when errorPolicy=all', done => {
427430
let count = 0;
428431
const Component = () => (
429432
<Mutation mutation={mutation}>
@@ -446,7 +449,9 @@ it('renders an error state when encountering graphql errors when errorPolicy=all
446449
} else if (count === 1) {
447450
expect(result.loading).toBeTruthy();
448451
} else if (count === 2) {
449-
expect(result.error).toEqual(new Error('GraphQL error: error occurred'));
452+
expect(result.error).toEqual(
453+
new ApolloError({ graphQLErrors: [new GraphQLError('error occurred')] }),
454+
);
450455
done();
451456
}
452457
count++;
@@ -471,6 +476,48 @@ it('renders an error state when encountering graphql errors when errorPolicy=all
471476
);
472477
});
473478

479+
it('renders an error state and throws when encountering network errors when errorPolicy=all', done => {
480+
let count = 0;
481+
const expectedError = new ApolloError({ networkError: new Error('network error') });
482+
const Component = () => (
483+
<Mutation mutation={mutation}>
484+
{(createTodo, result) => {
485+
if (count === 0) {
486+
setTimeout(() =>
487+
createTodo()
488+
.then(() => {
489+
done.fail('Did not expect a result');
490+
})
491+
.catch(e => {
492+
expect(e).toEqual(expectedError);
493+
}),
494+
);
495+
} else if (count === 1) {
496+
expect(result.loading).toBeTruthy();
497+
} else if (count === 2) {
498+
expect(result.error).toEqual(expectedError);
499+
done();
500+
}
501+
count++;
502+
return <div />;
503+
}}
504+
</Mutation>
505+
);
506+
507+
const mockError = [
508+
{
509+
request: { query: mutation },
510+
error: new Error('network error'),
511+
},
512+
];
513+
514+
mount(
515+
<MockedProvider defaultOptions={{ mutate: { errorPolicy: 'all' } }} mocks={mockError}>
516+
<Component />
517+
</MockedProvider>,
518+
);
519+
});
520+
474521
it('calls the onError prop if the mutation encounters an error', done => {
475522
let onRenderCalled = false;
476523

0 commit comments

Comments
 (0)