@@ -2606,114 +2606,97 @@ describe('useQuery Hook', () => {
2606
2606
switch ( operation . operationName ) {
2607
2607
case "A" :
2608
2608
observer . next ( { data : aData } ) ;
2609
+ observer . complete ( ) ;
2609
2610
break ;
2610
2611
case "B" :
2611
- observer . next ( { data : bData } ) ;
2612
+ setTimeout ( ( ) => {
2613
+ observer . next ( { data : bData } ) ;
2614
+ observer . complete ( ) ;
2615
+ } , 10 ) ;
2612
2616
break ;
2613
2617
}
2614
- observer . complete ( ) ;
2615
2618
} ) ) ,
2616
2619
} ) ;
2617
2620
}
2618
2621
2619
- function check (
2622
+ async function check (
2620
2623
aFetchPolicy : WatchQueryFetchPolicy ,
2621
2624
bFetchPolicy : WatchQueryFetchPolicy ,
2622
2625
) {
2623
- return (
2624
- resolve : ( result : any ) => any ,
2625
- reject : ( reason : any ) => any ,
2626
- ) => {
2627
- let renderCount = 0 ;
2628
-
2629
- function App ( ) {
2630
- const a = useQuery ( aQuery , {
2631
- fetchPolicy : aFetchPolicy ,
2632
- } ) ;
2626
+ const client = makeClient ( ) ;
2627
+ const { result, waitForNextUpdate } = renderHook (
2628
+ ( ) => ( {
2629
+ a : useQuery ( aQuery , { fetchPolicy : aFetchPolicy } ) ,
2630
+ b : useQuery ( bQuery , { fetchPolicy : bFetchPolicy } ) ,
2631
+ } ) ,
2632
+ {
2633
+ wrapper : ( { children } ) => (
2634
+ < ApolloProvider client = { client } > { children } </ ApolloProvider >
2635
+ ) ,
2636
+ } ,
2637
+ ) ;
2633
2638
2634
- const b = useQuery ( bQuery , {
2635
- fetchPolicy : bFetchPolicy ,
2636
- } ) ;
2639
+ expect ( result . current . a . loading ) . toBe ( true ) ;
2640
+ expect ( result . current . b . loading ) . toBe ( true ) ;
2641
+ expect ( result . current . a . data ) . toBe ( undefined ) ;
2642
+ expect ( result . current . b . data ) . toBe ( undefined ) ;
2637
2643
2638
- switch ( ++ renderCount ) {
2639
- case 1 :
2640
- expect ( a . loading ) . toBe ( true ) ;
2641
- expect ( b . loading ) . toBe ( true ) ;
2642
- expect ( a . data ) . toBeUndefined ( ) ;
2643
- expect ( b . data ) . toBeUndefined ( ) ;
2644
- break ;
2645
- case 2 :
2646
- expect ( a . loading ) . toBe ( false ) ;
2647
- expect ( b . loading ) . toBe ( true ) ;
2648
- expect ( a . data ) . toEqual ( aData ) ;
2649
- expect ( b . data ) . toBeUndefined ( ) ;
2650
- break ;
2651
- case 3 :
2652
- expect ( a . loading ) . toBe ( false ) ;
2653
- expect ( b . loading ) . toBe ( false ) ;
2654
- expect ( a . data ) . toEqual ( aData ) ;
2655
- expect ( b . data ) . toEqual ( bData ) ;
2656
- break ;
2657
- default :
2658
- reject ( "too many renders: " + renderCount ) ;
2659
- }
2660
-
2661
- return null ;
2662
- }
2644
+ await waitForNextUpdate ( ) ;
2645
+ expect ( result . current . a . loading ) . toBe ( false ) ;
2646
+ expect ( result . current . b . loading ) . toBe ( true ) ;
2647
+ expect ( result . current . a . data ) . toEqual ( aData ) ;
2648
+ expect ( result . current . b . data ) . toBe ( undefined ) ;
2663
2649
2664
- render (
2665
- < ApolloProvider client = { makeClient ( ) } >
2666
- < App />
2667
- </ ApolloProvider >
2668
- ) ;
2650
+ await waitForNextUpdate ( ) ;
2669
2651
2670
- return wait ( ( ) => {
2671
- expect ( renderCount ) . toBe ( 3 ) ;
2672
- } ) . then ( resolve , reject ) ;
2673
- } ;
2652
+ expect ( result . current . a . loading ) . toBe ( false ) ;
2653
+ expect ( result . current . b . loading ) . toBe ( false ) ;
2654
+ expect ( result . current . a . data ) . toEqual ( aData ) ;
2655
+ expect ( result . current . b . data ) . toEqual ( bData ) ;
2656
+ await expect ( waitForNextUpdate ( { timeout : 20 } ) ) . rejects . toThrow ( 'Timed out' ) ;
2674
2657
}
2675
2658
2676
- itAsync ( "cache-first for both" , check (
2659
+ it ( "cache-first for both" , ( ) => check (
2677
2660
"cache-first" ,
2678
2661
"cache-first" ,
2679
2662
) ) ;
2680
2663
2681
- itAsync ( "cache-first first, cache-and-network second" , check (
2664
+ it ( "cache-first first, cache-and-network second" , ( ) => check (
2682
2665
"cache-first" ,
2683
2666
"cache-and-network" ,
2684
2667
) ) ;
2685
2668
2686
- itAsync ( "cache-first first, network-only second" , check (
2669
+ it ( "cache-first first, network-only second" , ( ) => check (
2687
2670
"cache-first" ,
2688
2671
"network-only" ,
2689
2672
) ) ;
2690
2673
2691
- itAsync ( "cache-and-network for both" , check (
2674
+ it ( "cache-and-network for both" , ( ) => check (
2692
2675
"cache-and-network" ,
2693
2676
"cache-and-network" ,
2694
2677
) ) ;
2695
2678
2696
- itAsync ( "cache-and-network first, cache-first second" , check (
2679
+ it ( "cache-and-network first, cache-first second" , ( ) => check (
2697
2680
"cache-and-network" ,
2698
2681
"cache-first" ,
2699
2682
) ) ;
2700
2683
2701
- itAsync ( "cache-and-network first, network-only second" , check (
2684
+ it ( "cache-and-network first, network-only second" , ( ) => check (
2702
2685
"cache-and-network" ,
2703
2686
"network-only" ,
2704
2687
) ) ;
2705
2688
2706
- itAsync ( "network-only for both" , check (
2689
+ it ( "network-only for both" , ( ) => check (
2707
2690
"network-only" ,
2708
2691
"network-only" ,
2709
2692
) ) ;
2710
2693
2711
- itAsync ( "network-only first, cache-first second" , check (
2694
+ it ( "network-only first, cache-first second" , ( ) => check (
2712
2695
"network-only" ,
2713
2696
"cache-first" ,
2714
2697
) ) ;
2715
2698
2716
- itAsync ( "network-only first, cache-and-network second" , check (
2699
+ it ( "network-only first, cache-and-network second" , ( ) => check (
2717
2700
"network-only" ,
2718
2701
"cache-and-network" ,
2719
2702
) ) ;
0 commit comments