import { StartServicesAccessor } from '../../../../core/public' ; import { createStartServicesGetter } from './create_start_service_getter' ; import { Defer } from '../../common/defer' ; describe ( 'createStartServicesGetter' , ( ) => { test ( 'throws if services are accessed before accessor resolves' , async ( ) => { const future = new Defer < any > ( ) ; const accessor = async ( ) => await future . promise ; const start = createStartServicesGetter ( accessor ) ; await new Promise ( ( r ) => setTimeout ( r , 1 ) ) ; expect ( ( ) => start ( ) ) . toThrowErrorMatchingInlineSnapshot ( `"Trying to access start services before start."` ) ; } ) ; test ( 'returns services after accessor resolves even if first time called before it resolved' , async ( ) => { const future = new Defer < any > ( ) ; const core = { } ; const plugins = { } ; const self = { } ; const accessor = async ( ) => await future . promise ; const start = createStartServicesGetter ( accessor ) ; await new Promise ( ( r ) => setTimeout ( r , 1 ) ) ; expect ( ( ) => start ( ) ) . toThrow ( ) ; await new Promise ( ( r ) => setTimeout ( r , 1 ) ) ; future . resolve ( [ core , plugins , self ] ) ; await future . promise ; await new Promise ( ( r ) => process . nextTick ( r ) ) ; expect ( start ( ) ) . toEqual ( { core , plugins , self , } ) ; } ) ; test ( 'returns services if called after accessor resolves' , async ( ) => { const future = new Defer < any > ( ) ; const core = { } ; const plugins = { } ; const self = { } ; const accessor = async ( ) => await future . promise ; const start = createStartServicesGetter ( accessor ) ; await new Promise ( ( r ) => setTimeout ( r , 1 ) ) ; future . resolve ( [ core , plugins , self ] ) ; await future . promise ; await new Promise ( ( r ) => process . nextTick ( r ) ) ; expect ( start ( ) ) . toEqual ( { core , plugins , self , } ) ; } ) ; } ) ;