jest . mock ( '../../export' , ( ) => ( { exportSavedObjectsToStream : jest . fn ( ) , } ) ) ; import * as exportMock from '../../export' ; import { createListStream } from '../../../utils/streams' ; import supertest from 'supertest' ; import { UnwrapPromise } from '@osd/utility-types' ; import { SavedObjectConfig } from '../../saved_objects_config' ; import { registerExportRoute } from '../export' ; import { setupServer , createExportableType } from '../test_utils' ; type SetupServerReturn = UnwrapPromise < ReturnType < typeof setupServer > > ; const exportSavedObjectsToStream = exportMock . exportSavedObjectsToStream as jest . Mock ; const allowedTypes = [ 'index-pattern' , 'search' ] ; const config = { maxImportPayloadBytes : 26214400 , maxImportExportSize : 10000 , } as SavedObjectConfig ; describe ( 'POST /api/saved_objects/_export' , ( ) => { let server ; let httpSetup ; let handlerContext ; beforeEach ( async ( ) => { ( { server , httpSetup , handlerContext } = await setupServer ( ) ) ; handlerContext . savedObjects . typeRegistry . getImportableAndExportableTypes . mockReturnValue ( allowedTypes . map ( createExportableType ) ) ; const router = httpSetup . createRouter ( '/api/saved_objects/' ) ; registerExportRoute ( router , config ) ; await server . start ( ) ; } ) ; afterEach ( async ( ) => { jest . clearAllMocks ( ) ; await server . stop ( ) ; } ) ; it ( 'formats successful response' , async ( ) => { const sortedObjects = [ { id : '1' , type : 'index-pattern' , attributes : { } , references : [ ] , } , { id : '2' , type : 'search' , attributes : { } , references : [ { name : 'ref_0' , type : 'index-pattern' , id : '1' , } , ] , } , ] ; exportSavedObjectsToStream . mockResolvedValueOnce ( createListStream ( sortedObjects ) ) ; const result = await supertest ( httpSetup . server . listener ) . post ( '/api/saved_objects/_export' ) . send ( { type : 'search' , search : 'my search string' , includeReferencesDeep : true , } ) ; expect ( result . status ) . toBe ( 200 ) ; expect ( result . header ) . toEqual ( expect . objectContaining ( { 'content-disposition' : 'attachment; filename="export.ndjson"' , 'content-type' : 'application/ndjson' , } ) ) ; const objects = ( result . text as string ) . split ( '\n' ) . map ( ( row ) => JSON . parse ( row ) ) ; expect ( objects ) . toEqual ( sortedObjects ) ; expect ( exportSavedObjectsToStream . mock . calls [ 0 ] [ 0 ] ) . toEqual ( expect . objectContaining ( { excludeExportDetails : false , exportSizeLimit : 10000 , includeReferencesDeep : true , objects : undefined , search : 'my search string' , types : [ 'search' ] , } ) ) ; } ) ; } ) ;