import supertest from 'supertest' ; import { registerGetRoute } from '../get' ; import { ContextService } from '../../../context' ; import { savedObjectsClientMock } from '../../service/saved_objects_client.mock' ; import { HttpService , InternalHttpServiceSetup } from '../../../http' ; import { createHttpServer , createCoreContext } from '../../../http/test_utils' ; import { coreMock } from '../../../mocks' ; const coreId = Symbol ( 'core' ) ; describe ( 'GET /api/saved_objects/{type}/{id}' , ( ) => { let server ; let httpSetup ; let handlerContext ; let savedObjectsClient ; beforeEach ( async ( ) => { const coreContext = createCoreContext ( { coreId } ) ; server = createHttpServer ( coreContext ) ; const contextService = new ContextService ( coreContext ) ; httpSetup = await server . setup ( { context : contextService . setup ( { pluginDependencies : new Map ( ) } ) , } ) ; handlerContext = coreMock . createRequestHandlerContext ( ) ; savedObjectsClient = handlerContext . savedObjects . client ; httpSetup . registerRouteHandlerContext ( coreId , 'core' , async ( ctx , req , res ) => { return handlerContext ; } ) ; const router = httpSetup . createRouter ( '/api/saved_objects/' ) ; registerGetRoute ( router ) ; await server . start ( ) ; } ) ; afterEach ( async ( ) => { await server . stop ( ) ; } ) ; it ( 'formats successful response' , async ( ) => { const clientResponse = { id : 'logstash-*' , title : 'logstash-*' , type : 'logstash-type' , attributes : { } , timeFieldName : '@timestamp' , notExpandable : true , references : [ ] , } ; savedObjectsClient . get . mockResolvedValue ( clientResponse ) ; const result = await supertest ( httpSetup . server . listener ) . get ( '/api/saved_objects/index-pattern/logstash-*' ) . expect ( 200 ) ; expect ( result . body ) . toEqual ( clientResponse ) ; } ) ; it ( 'calls upon savedObjectClient.get' , async ( ) => { await supertest ( httpSetup . server . listener ) . get ( '/api/saved_objects/index-pattern/logstash-*' ) . expect ( 200 ) ; expect ( savedObjectsClient . get ) . toHaveBeenCalled ( ) ; const args = savedObjectsClient . get . mock . calls [ 0 ] ; expect ( args ) . toEqual ( [ 'index-pattern' , 'logstash-*' ] ) ; } ) ; } ) ;