import fetchMock from 'fetch-mock/es5/client' ; import { loadingServiceMock } from './http_service.test.mocks' ; import { fatalErrorsServiceMock } from '../fatal_errors/fatal_errors_service.mock' ; import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock' ; import { HttpService } from './http_service' ; import { Observable } from 'rxjs' ; describe ( 'interceptors' , ( ) => { afterEach ( ( ) => fetchMock . restore ( ) ) ; it ( 'shares interceptors across setup and start' , async ( ) => { fetchMock . get ( '*' , { } ) ; const injectedMetadata = injectedMetadataServiceMock . createSetupContract ( ) ; const fatalErrors = fatalErrorsServiceMock . createSetupContract ( ) ; const httpService = new HttpService ( ) ; const setup = httpService . setup ( { fatalErrors , injectedMetadata } ) ; const setupInterceptor = jest . fn ( ) ; setup . intercept ( { request : setupInterceptor } ) ; const start = httpService . start ( ) ; const startInterceptor = jest . fn ( ) ; start . intercept ( { request : startInterceptor } ) ; await setup . get ( '/blah' ) ; expect ( setupInterceptor ) . toHaveBeenCalledTimes ( 1 ) ; expect ( startInterceptor ) . toHaveBeenCalledTimes ( 1 ) ; await start . get ( '/other-blah' ) ; expect ( setupInterceptor ) . toHaveBeenCalledTimes ( 2 ) ; expect ( startInterceptor ) . toHaveBeenCalledTimes ( 2 ) ; } ) ; } ) ; describe ( '#setup()' , ( ) => { it ( 'registers Fetch#getLoadingCount$() with LoadingCountSetup#addLoadingCountSource()' , ( ) => { const injectedMetadata = injectedMetadataServiceMock . createSetupContract ( ) ; const fatalErrors = fatalErrorsServiceMock . createSetupContract ( ) ; const httpService = new HttpService ( ) ; httpService . setup ( { fatalErrors , injectedMetadata } ) ; const loadingServiceSetup = loadingServiceMock . setup . mock . results [ 0 ] . value ; expect ( loadingServiceSetup . addLoadingCountSource ) . toHaveBeenCalledWith ( expect . any ( Observable ) ) ; } ) ; } ) ; describe ( '#stop()' , ( ) => { it ( 'calls loadingCount.stop()' , ( ) => { const injectedMetadata = injectedMetadataServiceMock . createSetupContract ( ) ; const fatalErrors = fatalErrorsServiceMock . createSetupContract ( ) ; const httpService = new HttpService ( ) ; httpService . setup ( { fatalErrors , injectedMetadata } ) ; httpService . start ( ) ; httpService . stop ( ) ; expect ( loadingServiceMock . stop ) . toHaveBeenCalled ( ) ; } ) ; } ) ;