@@ -175,7 +175,7 @@ describe('General use', () => {
175
175
} ;
176
176
177
177
render (
178
- < MockedProvider mocks = { mocks } >
178
+ < MockedProvider showWarnings = { false } mocks = { mocks } >
179
179
< Component { ...variables2 } />
180
180
</ MockedProvider >
181
181
) ;
@@ -215,7 +215,7 @@ describe('General use', () => {
215
215
} ;
216
216
217
217
render (
218
- < MockedProvider mocks = { mocks2 } >
218
+ < MockedProvider showWarnings = { false } mocks = { mocks2 } >
219
219
< Component { ...variables2 } />
220
220
</ MockedProvider >
221
221
) ;
@@ -325,7 +325,7 @@ describe('General use', () => {
325
325
] ;
326
326
327
327
render (
328
- < MockedProvider mocks = { mocksDifferentQuery } >
328
+ < MockedProvider showWarnings = { false } mocks = { mocksDifferentQuery } >
329
329
< Component { ...variables } />
330
330
</ MockedProvider >
331
331
) ;
@@ -435,7 +435,10 @@ describe('General use', () => {
435
435
return null ;
436
436
}
437
437
438
- const link = ApolloLink . from ( [ errorLink , new MockLink ( [ ] ) ] ) ;
438
+ const link = ApolloLink . from ( [
439
+ errorLink ,
440
+ new MockLink ( [ ] , true , { showWarnings : false } )
441
+ ] ) ;
439
442
440
443
render (
441
444
< MockedProvider link = { link } >
@@ -485,14 +488,149 @@ describe('General use', () => {
485
488
expect ( errorThrown ) . toBeFalsy ( ) ;
486
489
} ) ;
487
490
491
+ it ( 'shows a warning in the console when there is no matched mock' , async ( ) => {
492
+ const consoleSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
493
+ let finished = false ;
494
+ function Component ( { ...variables } : Variables ) {
495
+ const { loading } = useQuery < Data , Variables > ( query , { variables } ) ;
496
+ if ( ! loading ) {
497
+ finished = true ;
498
+ }
499
+ return null ;
500
+ }
501
+
502
+ const mocksDifferentQuery = [
503
+ {
504
+ request : {
505
+ query : gql `
506
+ query OtherQuery {
507
+ otherQuery {
508
+ id
509
+ }
510
+ }
511
+ ` ,
512
+ variables
513
+ } ,
514
+ result : { data : { user } }
515
+ }
516
+ ] ;
517
+
518
+ render (
519
+ < MockedProvider mocks = { mocksDifferentQuery } >
520
+ < Component { ...variables } />
521
+ </ MockedProvider >
522
+ ) ;
523
+
524
+ await waitFor ( ( ) => {
525
+ expect ( finished ) . toBe ( true ) ;
526
+ } ) ;
527
+
528
+ expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
529
+ expect ( console . warn ) . toHaveBeenCalledWith (
530
+ expect . stringContaining ( 'No more mocked responses for the query' )
531
+ ) ;
532
+
533
+ consoleSpy . mockRestore ( ) ;
534
+ } ) ;
535
+
536
+ it ( 'silences console warning for unmatched mocks when `showWarnings` is `false`' , async ( ) => {
537
+ const consoleSpy = jest . spyOn ( console , 'warn' ) ;
538
+ let finished = false ;
539
+ function Component ( { ...variables } : Variables ) {
540
+ const { loading } = useQuery < Data , Variables > ( query , { variables } ) ;
541
+ if ( ! loading ) {
542
+ finished = true ;
543
+ }
544
+ return null ;
545
+ }
546
+
547
+ const mocksDifferentQuery = [
548
+ {
549
+ request : {
550
+ query : gql `
551
+ query OtherQuery {
552
+ otherQuery {
553
+ id
554
+ }
555
+ }
556
+ ` ,
557
+ variables
558
+ } ,
559
+ result : { data : { user } }
560
+ }
561
+ ] ;
562
+
563
+ render (
564
+ < MockedProvider mocks = { mocksDifferentQuery } showWarnings = { false } >
565
+ < Component { ...variables } />
566
+ </ MockedProvider >
567
+ ) ;
568
+
569
+ await waitFor ( ( ) => {
570
+ expect ( finished ) . toBe ( true ) ;
571
+ } ) ;
572
+
573
+ expect ( console . warn ) . not . toHaveBeenCalled ( ) ;
574
+
575
+ consoleSpy . mockRestore ( ) ;
576
+ } ) ;
577
+
578
+ it ( 'silences console warning for unmatched mocks when passing `showWarnings` to `MockLink` directly' , async ( ) => {
579
+ const consoleSpy = jest . spyOn ( console , 'warn' ) ;
580
+ let finished = false ;
581
+ function Component ( { ...variables } : Variables ) {
582
+ const { loading } = useQuery < Data , Variables > ( query , { variables } ) ;
583
+ if ( ! loading ) {
584
+ finished = true ;
585
+ }
586
+ return null ;
587
+ }
588
+
589
+ const mocksDifferentQuery = [
590
+ {
591
+ request : {
592
+ query : gql `
593
+ query OtherQuery {
594
+ otherQuery {
595
+ id
596
+ }
597
+ }
598
+ ` ,
599
+ variables
600
+ } ,
601
+ result : { data : { user } }
602
+ }
603
+ ] ;
604
+
605
+ const link = new MockLink (
606
+ mocksDifferentQuery ,
607
+ false ,
608
+ { showWarnings : false }
609
+ ) ;
610
+
611
+ render (
612
+ < MockedProvider link = { link } >
613
+ < Component { ...variables } />
614
+ </ MockedProvider >
615
+ ) ;
616
+
617
+ await waitFor ( ( ) => {
618
+ expect ( finished ) . toBe ( true ) ;
619
+ } ) ;
620
+
621
+ expect ( console . warn ) . not . toHaveBeenCalled ( ) ;
622
+
623
+ consoleSpy . mockRestore ( ) ;
624
+ } ) ;
625
+
488
626
itAsync ( 'should support custom error handling using setOnError' , ( resolve , reject ) => {
489
627
let finished = false ;
490
628
function Component ( { ...variables } : Variables ) {
491
629
useQuery < Data , Variables > ( query , { variables } ) ;
492
630
return null ;
493
631
}
494
632
495
- const mockLink = new MockLink ( [ ] ) ;
633
+ const mockLink = new MockLink ( [ ] , true , { showWarnings : false } ) ;
496
634
mockLink . setOnError ( error => {
497
635
expect ( error ) . toMatchSnapshot ( ) ;
498
636
finished = true ;
@@ -521,7 +659,7 @@ describe('General use', () => {
521
659
return null ;
522
660
}
523
661
524
- const mockLink = new MockLink ( [ ] ) ;
662
+ const mockLink = new MockLink ( [ ] , true , { showWarnings : false } ) ;
525
663
mockLink . setOnError ( ( ) => {
526
664
throw new Error ( 'oh no!' ) ;
527
665
} ) ;
0 commit comments