1
1
import React from 'react' ;
2
2
import { mount } from 'enzyme' ;
3
3
import gql from 'graphql-tag' ;
4
- import { ApolloClient } from 'apollo-client' ;
4
+ import { ApolloClient , ApolloError } from 'apollo-client' ;
5
5
import { InMemoryCache as Cache } from 'apollo-cache-inmemory' ;
6
6
import { DataProxy } from 'apollo-cache' ;
7
7
import { ExecutionResult , GraphQLError } from 'graphql' ;
@@ -380,8 +380,11 @@ it('renders an error state', done => {
380
380
) ;
381
381
} ) ;
382
382
383
- it ( 'renders an error state when encountering graphql errors' , done => {
383
+ it ( 'renders an error state and throws when encountering graphql errors' , done => {
384
384
let count = 0 ;
385
+
386
+ const expectedError = new ApolloError ( { graphQLErrors : [ new GraphQLError ( 'error occurred' ) ] } ) ;
387
+
385
388
const Component = ( ) => (
386
389
< Mutation mutation = { mutation } >
387
390
{ ( createTodo , result ) => {
@@ -392,13 +395,13 @@ it('renders an error state when encountering graphql errors', done => {
392
395
done . fail ( 'Did not expect a result' ) ;
393
396
} )
394
397
. catch ( e => {
395
- expect ( e ) . toEqual ( new Error ( 'GraphQL error: error occurred' ) ) ;
398
+ expect ( e ) . toEqual ( expectedError ) ;
396
399
} ) ,
397
400
) ;
398
401
} else if ( count === 1 ) {
399
402
expect ( result . loading ) . toBeTruthy ( ) ;
400
403
} else if ( count === 2 ) {
401
- expect ( result . error ) . toEqual ( new Error ( 'GraphQL error: error occurred' ) ) ;
404
+ expect ( result . error ) . toEqual ( expectedError ) ;
402
405
done ( ) ;
403
406
}
404
407
count ++ ;
@@ -423,7 +426,7 @@ it('renders an error state when encountering graphql errors', done => {
423
426
) ;
424
427
} ) ;
425
428
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 => {
427
430
let count = 0 ;
428
431
const Component = ( ) => (
429
432
< Mutation mutation = { mutation } >
@@ -446,7 +449,9 @@ it('renders an error state when encountering graphql errors when errorPolicy=all
446
449
} else if ( count === 1 ) {
447
450
expect ( result . loading ) . toBeTruthy ( ) ;
448
451
} 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
+ ) ;
450
455
done ( ) ;
451
456
}
452
457
count ++ ;
@@ -471,6 +476,48 @@ it('renders an error state when encountering graphql errors when errorPolicy=all
471
476
) ;
472
477
} ) ;
473
478
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
+
474
521
it ( 'calls the onError prop if the mutation encounters an error' , done => {
475
522
let onRenderCalled = false ;
476
523
0 commit comments