@@ -12,11 +12,14 @@ import {Observable, of, Subject} from 'rxjs';
1212import { catchError , retry , scan , skip , take , toArray } from 'rxjs/operators' ;
1313
1414import {
15+ HttpClient ,
1516 HttpDownloadProgressEvent ,
1617 HttpErrorResponse ,
1718 HttpHeaderResponse ,
1819 HttpParams ,
1920 HttpStatusCode ,
21+ provideHttpClient ,
22+ withFetch ,
2023} from '../public_api' ;
2124import { FetchBackend , FetchFactory } from '../src/fetch' ;
2225
@@ -416,6 +419,36 @@ describe('FetchBackend', async () => {
416419 fetchMock . mockFlush ( 0 , 'CORS 0 status' ) ;
417420 } ) ;
418421 } ) ;
422+
423+ describe ( 'dynamic global fetch' , ( ) => {
424+ beforeEach ( ( ) => {
425+ TestBed . resetTestingModule ( ) ;
426+ TestBed . configureTestingModule ( {
427+ providers : [ provideHttpClient ( withFetch ( ) ) ] ,
428+ } ) ;
429+ } ) ;
430+
431+ it ( 'should use the current implementation of the global fetch' , async ( ) => {
432+ const fakeFetch = jasmine
433+ . createSpy ( '' , ( ) => Promise . resolve ( new Response ( JSON . stringify ( { foo : 'bar' } ) ) ) )
434+ . and . callThrough ( ) ;
435+ globalThis . fetch = fakeFetch ;
436+
437+ const client = TestBed . inject ( HttpClient ) ;
438+ expect ( fakeFetch ) . not . toHaveBeenCalled ( ) ;
439+ let response = await client . get < unknown > ( '' ) . toPromise ( ) ;
440+ expect ( fakeFetch ) . toHaveBeenCalled ( ) ;
441+ expect ( response ) . toEqual ( { foo : 'bar' } ) ;
442+
443+ // We dynamicaly change the implementation of fetch
444+ const fakeFetch2 = jasmine
445+ . createSpy ( '' , ( ) => Promise . resolve ( new Response ( JSON . stringify ( { foo : 'baz' } ) ) ) )
446+ . and . callThrough ( ) ;
447+ globalThis . fetch = fakeFetch2 ;
448+ response = await client . get < unknown > ( '' ) . toPromise ( ) ;
449+ expect ( response ) . toEqual ( { foo : 'baz' } ) ;
450+ } ) ;
451+ } ) ;
419452} ) ;
420453
421454export class MockFetchFactory extends FetchFactory {
0 commit comments