@@ -6,6 +6,7 @@ import { ApolloLink } from "../../core/ApolloLink";
6
6
import { execute } from "../../core/execute" ;
7
7
import {
8
8
Observable ,
9
+ ObservableSubscription ,
9
10
Observer ,
10
11
} from "../../../utilities/observables/Observable" ;
11
12
import { BatchHttpLink } from "../batchHttpLink" ;
@@ -271,6 +272,8 @@ const createHttpLink = (httpArgs?: any) => {
271
272
return new BatchHttpLink ( args ) ;
272
273
} ;
273
274
275
+ const subscriptions = new Set < ObservableSubscription > ( ) ;
276
+
274
277
describe ( "SharedHttpTest" , ( ) => {
275
278
const data = { data : { hello : "world" } } ;
276
279
const data2 = { data : { hello : "everyone" } } ;
@@ -300,10 +303,16 @@ describe("SharedHttpTest", () => {
300
303
error,
301
304
complete,
302
305
} ;
306
+ subscriptions . clear ( ) ;
303
307
} ) ;
304
308
305
309
afterEach ( ( ) => {
306
310
fetchMock . restore ( ) ;
311
+ if ( subscriptions . size ) {
312
+ // Tests within this describe block can add subscriptions to this Set
313
+ // that they want to be canceled/unsubscribed after the test finishes.
314
+ subscriptions . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
315
+ }
307
316
} ) ;
308
317
309
318
it ( "raises warning if called with concat" , ( ) => {
@@ -624,6 +633,61 @@ describe("SharedHttpTest", () => {
624
633
) ;
625
634
} ) ;
626
635
636
+ it ( "uses the latest window.fetch function if options.fetch not configured" , ( done ) => {
637
+ const httpLink = createHttpLink ( { uri : "data" } ) ;
638
+
639
+ const fetch = window . fetch ;
640
+ expect ( typeof fetch ) . toBe ( "function" ) ;
641
+
642
+ const fetchSpy = jest . spyOn ( window , "fetch" ) ;
643
+ fetchSpy . mockImplementation ( ( ) =>
644
+ Promise . resolve < Response > ( {
645
+ text ( ) {
646
+ return Promise . resolve (
647
+ JSON . stringify ( {
648
+ data : { hello : "from spy" } ,
649
+ } )
650
+ ) ;
651
+ } ,
652
+ } as Response )
653
+ ) ;
654
+
655
+ const spyFn = window . fetch ;
656
+ expect ( spyFn ) . not . toBe ( fetch ) ;
657
+
658
+ subscriptions . add (
659
+ execute ( httpLink , {
660
+ query : sampleQuery ,
661
+ } ) . subscribe ( {
662
+ error : done . fail ,
663
+
664
+ next ( result ) {
665
+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
666
+ expect ( result ) . toEqual ( {
667
+ data : { hello : "from spy" } ,
668
+ } ) ;
669
+
670
+ fetchSpy . mockRestore ( ) ;
671
+ expect ( window . fetch ) . toBe ( fetch ) ;
672
+
673
+ subscriptions . add (
674
+ execute ( httpLink , {
675
+ query : sampleQuery ,
676
+ } ) . subscribe ( {
677
+ error : done . fail ,
678
+ next ( result ) {
679
+ expect ( result ) . toEqual ( {
680
+ data : { hello : "world" } ,
681
+ } ) ;
682
+ done ( ) ;
683
+ } ,
684
+ } )
685
+ ) ;
686
+ } ,
687
+ } )
688
+ ) ;
689
+ } ) ;
690
+
627
691
itAsync (
628
692
"prioritizes context headers over setup headers" ,
629
693
( resolve , reject ) => {
0 commit comments