import supertest from 'supertest' ; import { UnwrapPromise } from '@osd/utility-types' ; import { registerBulkUpdateRoute } from '../bulk_update' ; import { savedObjectsClientMock } from '../../../../../core/server/mocks' ; import { setupServer } from '../test_utils' ; type SetupServerReturn = UnwrapPromise < ReturnType < typeof setupServer > > ; describe ( 'PUT /api/saved_objects/_bulk_update' , ( ) => { let server ; let httpSetup ; let handlerContext ; let savedObjectsClient ; beforeEach ( async ( ) => { ( { server , httpSetup , handlerContext } = await setupServer ( ) ) ; savedObjectsClient = handlerContext . savedObjects . client ; const router = httpSetup . createRouter ( '/api/saved_objects/' ) ; registerBulkUpdateRoute ( router ) ; await server . start ( ) ; } ) ; afterEach ( async ( ) => { await server . stop ( ) ; } ) ; it ( 'formats successful response' , async ( ) => { const time = Date . now ( ) . toLocaleString ( ) ; const clientResponse = [ { type : 'visualization' , id : 'dd7caf20-9efd-11e7-acb3-3dab96693fab' , updated_at : time , version : 'version' , references : undefined , attributes : { title : 'An existing visualization' , } , } , { type : 'dashboard' , id : 'be3733a0-9efe-11e7-acb3-3dab96693fab' , updated_at : time , version : 'version' , references : undefined , attributes : { title : 'An existing dashboard' , } , } , ] ; savedObjectsClient . bulkUpdate . mockResolvedValue ( { saved_objects : clientResponse } ) ; const result = await supertest ( httpSetup . server . listener ) . put ( '/api/saved_objects/_bulk_update' ) . send ( [ { type : 'visualization' , id : 'dd7caf20-9efd-11e7-acb3-3dab96693fab' , attributes : { title : 'An existing visualization' , } , } , { type : 'dashboard' , id : 'be3733a0-9efe-11e7-acb3-3dab96693fab' , attributes : { title : 'An existing dashboard' , } , } , ] ) . expect ( 200 ) ; expect ( result . body ) . toEqual ( { saved_objects : clientResponse } ) ; } ) ; it ( 'calls upon savedObjectClient.bulkUpdate' , async ( ) => { savedObjectsClient . bulkUpdate . mockResolvedValue ( { saved_objects : [ ] } ) ; await supertest ( httpSetup . server . listener ) . put ( '/api/saved_objects/_bulk_update' ) . send ( [ { type : 'visualization' , id : 'dd7caf20-9efd-11e7-acb3-3dab96693fab' , attributes : { title : 'An existing visualization' , } , } , { type : 'dashboard' , id : 'be3733a0-9efe-11e7-acb3-3dab96693fab' , attributes : { title : 'An existing dashboard' , } , } , ] ) . expect ( 200 ) ; expect ( savedObjectsClient . bulkUpdate ) . toHaveBeenCalledTimes ( 1 ) ; expect ( savedObjectsClient . bulkUpdate ) . toHaveBeenCalledWith ( [ { type : 'visualization' , id : 'dd7caf20-9efd-11e7-acb3-3dab96693fab' , attributes : { title : 'An existing visualization' , } , } , { type : 'dashboard' , id : 'be3733a0-9efe-11e7-acb3-3dab96693fab' , attributes : { title : 'An existing dashboard' , } , } , ] ) ; } ) ; } ) ;