@@ -35,12 +35,24 @@ const mockRollupResponse = {
3535
3636describe ( 'ES search strategy' , ( ) => {
3737 const mockApiCaller = jest . fn ( ) ;
38+ const mockGetCaller = jest . fn ( ) ;
39+ const mockSubmitCaller = jest . fn ( ) ;
3840 const mockLogger : any = {
3941 debug : ( ) => { } ,
4042 } ;
4143 const mockContext = {
4244 core : {
43- elasticsearch : { client : { asCurrentUser : { transport : { request : mockApiCaller } } } } ,
45+ elasticsearch : {
46+ client : {
47+ asCurrentUser : {
48+ asyncSearch : {
49+ get : mockGetCaller ,
50+ submit : mockSubmitCaller ,
51+ } ,
52+ transport : { request : mockApiCaller } ,
53+ } ,
54+ } ,
55+ } ,
4456 } ,
4557 } ;
4658 const mockConfig$ = pluginInitializerContextConfigMock < any > ( { } ) . legacy . globalConfig$ ;
@@ -56,47 +68,32 @@ describe('ES search strategy', () => {
5668 } ) ;
5769
5870 it ( 'makes a POST request to async search with params when no ID is provided' , async ( ) => {
59- mockApiCaller . mockResolvedValueOnce ( mockAsyncResponse ) ;
71+ mockSubmitCaller . mockResolvedValueOnce ( mockAsyncResponse ) ;
6072
6173 const params = { index : 'logstash-*' , body : { query : { } } } ;
6274 const esSearch = await enhancedEsSearchStrategyProvider ( mockConfig$ , mockLogger ) ;
6375
6476 await esSearch . search ( ( mockContext as unknown ) as RequestHandlerContext , { params } ) ;
6577
66- expect ( mockApiCaller ) . toBeCalled ( ) ;
67- const { method, path, body } = mockApiCaller . mock . calls [ 0 ] [ 0 ] ;
68- expect ( method ) . toBe ( 'POST' ) ;
69- expect ( path ) . toBe ( '/logstash-*/_async_search' ) ;
70- expect ( body ) . toEqual ( { query : { } } ) ;
78+ expect ( mockSubmitCaller ) . toBeCalled ( ) ;
79+ const request = mockSubmitCaller . mock . calls [ 0 ] [ 0 ] ;
80+ expect ( request . index ) . toEqual ( params . index ) ;
81+ expect ( request . body ) . toEqual ( params . body ) ;
7182 } ) ;
7283
7384 it ( 'makes a GET request to async search with ID when ID is provided' , async ( ) => {
74- mockApiCaller . mockResolvedValueOnce ( mockAsyncResponse ) ;
85+ mockGetCaller . mockResolvedValueOnce ( mockAsyncResponse ) ;
7586
7687 const params = { index : 'logstash-*' , body : { query : { } } } ;
7788 const esSearch = await enhancedEsSearchStrategyProvider ( mockConfig$ , mockLogger ) ;
7889
7990 await esSearch . search ( ( mockContext as unknown ) as RequestHandlerContext , { id : 'foo' , params } ) ;
8091
81- expect ( mockApiCaller ) . toBeCalled ( ) ;
82- const { method, path, body } = mockApiCaller . mock . calls [ 0 ] [ 0 ] ;
83- expect ( method ) . toBe ( 'GET' ) ;
84- expect ( path ) . toBe ( '/_async_search/foo' ) ;
85- expect ( body ) . toEqual ( undefined ) ;
86- } ) ;
87-
88- it ( 'encodes special characters in the path' , async ( ) => {
89- mockApiCaller . mockResolvedValueOnce ( mockAsyncResponse ) ;
90-
91- const params = { index : 'foo-程' , body : { } } ;
92- const esSearch = await enhancedEsSearchStrategyProvider ( mockConfig$ , mockLogger ) ;
93-
94- await esSearch . search ( ( mockContext as unknown ) as RequestHandlerContext , { params } ) ;
95-
96- expect ( mockApiCaller ) . toBeCalled ( ) ;
97- const { method, path } = mockApiCaller . mock . calls [ 0 ] [ 0 ] ;
98- expect ( method ) . toBe ( 'POST' ) ;
99- expect ( path ) . toBe ( '/foo-%E7%A8%8B/_async_search' ) ;
92+ expect ( mockGetCaller ) . toBeCalled ( ) ;
93+ const request = mockGetCaller . mock . calls [ 0 ] [ 0 ] ;
94+ expect ( request ) . toEqual ( {
95+ id : 'foo' ,
96+ } ) ;
10097 } ) ;
10198
10299 it ( 'calls the rollup API if the index is a rollup type' , async ( ) => {
@@ -117,16 +114,16 @@ describe('ES search strategy', () => {
117114 } ) ;
118115
119116 it ( 'sets wait_for_completion_timeout and keep_alive in the request' , async ( ) => {
120- mockApiCaller . mockResolvedValueOnce ( mockAsyncResponse ) ;
117+ mockSubmitCaller . mockResolvedValueOnce ( mockAsyncResponse ) ;
121118
122119 const params = { index : 'foo-*' , body : { } } ;
123120 const esSearch = await enhancedEsSearchStrategyProvider ( mockConfig$ , mockLogger ) ;
124121
125122 await esSearch . search ( ( mockContext as unknown ) as RequestHandlerContext , { params } ) ;
126123
127- expect ( mockApiCaller ) . toBeCalled ( ) ;
128- const { querystring } = mockApiCaller . mock . calls [ 0 ] [ 0 ] ;
129- expect ( querystring ) . toHaveProperty ( 'wait_for_completion_timeout' ) ;
130- expect ( querystring ) . toHaveProperty ( 'keep_alive' ) ;
124+ expect ( mockSubmitCaller ) . toBeCalled ( ) ;
125+ const request = mockSubmitCaller . mock . calls [ 0 ] [ 0 ] ;
126+ expect ( request ) . toHaveProperty ( 'wait_for_completion_timeout' ) ;
127+ expect ( request ) . toHaveProperty ( 'keep_alive' ) ;
131128 } ) ;
132129} ) ;
0 commit comments