import { TestScheduler } from 'rxjs/testing' ; import { httpServiceMock } from '../../../../../src/core/public/mocks' ; import { GlobalSearchResult } from '../../common/types' ; import { fetchServerResults } from './fetch_server_results' ; const getTestScheduler = ( ) => new TestScheduler ( ( actual , expected ) => { expect ( actual ) . toEqual ( expected ) ; } ) ; const createResult = ( id , parts = { } ) : GlobalSearchResult => ( { id , title : id , type : 'type' , url : `/path/to/${ id }` , score : 100 , ... parts , } ) ; describe ( 'fetchServerResults' , ( ) => { let http ; beforeEach ( ( ) => { http = httpServiceMock . createStartContract ( ) ; } ) ; it ( 'perform a POST request to the endpoint with valid options' , ( ) => { http . post . mockResolvedValue ( { results : [ ] } ) ; fetchServerResults ( http , { term : 'some term' , types : [ 'dashboard' , 'map' ] } , { preference : 'pref' } ) ; expect ( http . post ) . toHaveBeenCalledTimes ( 1 ) ; expect ( http . post ) . toHaveBeenCalledWith ( '/internal/global_search/find' , { body : JSON . stringify ( { params : { term : 'some term' , types : [ 'dashboard' , 'map' ] } , options : { preference : 'pref' } , } ) , } ) ; } ) ; it ( 'returns the results from the server' , async ( ) => { const resultA = createResult ( 'A' ) ; const resultB = createResult ( 'B' ) ; http . post . mockResolvedValue ( { results : [ resultA , resultB ] } ) ; const results = await fetchServerResults ( http , { term : 'some term' } , { preference : 'pref' } ) . toPromise ( ) ; expect ( http . post ) . toHaveBeenCalledTimes ( 1 ) ; expect ( results ) . toHaveLength ( 2 ) ; expect ( results [ 0 ] ) . toEqual ( resultA ) ; expect ( results [ 1 ] ) . toEqual ( resultB ) ; } ) ; describe ( 'returns an observable that' , ( ) => { it ( 'emits when the response is received' , ( ) => { getTestScheduler ( ) . run ( ( { expectObservable , hot } ) => { http . post . mockReturnValue ( hot ( '---(a|)' , { a : { results : [ ] } } ) as any ) ; const results = fetchServerResults ( http , { term : 'term' } , { } ) ; expectObservable ( results ) . toBe ( '---(a|)' , { a : [ ] , } ) ; } ) ; } ) ; it ( 'completes without returning results if aborted$ emits before the response' , ( ) => { getTestScheduler ( ) . run ( ( { expectObservable , hot } ) => { http . post . mockReturnValue ( hot ( '---(a|)' , { a : { results : [ ] } } ) as any ) ; const aborted$ = hot ( '-(a|)' , { a : undefined } ) ; const results = fetchServerResults ( http , { term : 'term' } , { aborted$ } ) ; expectObservable ( results ) . toBe ( '-|' , { a : [ ] , } ) ; } ) ; } ) ; } ) ; } ) ;