import supertest from 'supertest' ; import { UnwrapPromise } from '@osd/utility-types' ; import { registerUpdateRoute } from '../update' ; import { savedObjectsClientMock } from '../../../../../core/server/mocks' ; import { setupServer } from '../test_utils' ; type SetupServerReturn = UnwrapPromise < ReturnType < typeof setupServer > > ; describe ( 'PUT /api/saved_objects/{type}/{id?}' , ( ) => { let server ; let httpSetup ; let handlerContext ; let savedObjectsClient ; beforeEach ( async ( ) => { const clientResponse = { id : 'logstash-*' , title : 'logstash-*' , type : 'logstash-type' , attributes : { } , timeFieldName : '@timestamp' , notExpandable : true , references : [ ] , } ; ( { server , httpSetup , handlerContext } = await setupServer ( ) ) ; savedObjectsClient = handlerContext . savedObjects . client ; savedObjectsClient . update . mockResolvedValue ( clientResponse ) ; const router = httpSetup . createRouter ( '/api/saved_objects/' ) ; registerUpdateRoute ( router ) ; await server . start ( ) ; } ) ; afterEach ( async ( ) => { await server . stop ( ) ; } ) ; it ( 'formats successful response' , async ( ) => { const clientResponse = { id : 'logstash-*' , title : 'logstash-*' , type : 'logstash-type' , timeFieldName : '@timestamp' , notExpandable : true , attributes : { } , references : [ ] , } ; savedObjectsClient . update . mockResolvedValue ( clientResponse ) ; const result = await supertest ( httpSetup . server . listener ) . put ( '/api/saved_objects/index-pattern/logstash-*' ) . send ( { attributes : { title : 'Testing' , } , references : [ ] , } ) . expect ( 200 ) ; expect ( result . body ) . toEqual ( clientResponse ) ; } ) ; it ( 'calls upon savedObjectClient.update' , async ( ) => { await supertest ( httpSetup . server . listener ) . put ( '/api/saved_objects/index-pattern/logstash-*' ) . send ( { attributes : { title : 'Testing' } , version : 'foo' , } ) . expect ( 200 ) ; expect ( savedObjectsClient . update ) . toHaveBeenCalledWith ( 'index-pattern' , 'logstash-*' , { title : 'Testing' } , { version : 'foo' } ) ; } ) ; } ) ;