@@ -9,11 +9,12 @@ import React, { ReactElement } from 'react';
9
9
import { buildDataTableRecord } from '../../../../utils/build_data_record' ;
10
10
import { esHits } from '../../../../__mocks__/es_hits' ;
11
11
import { act , renderHook , WrapperComponent } from '@testing-library/react-hooks' ;
12
- import { BehaviorSubject } from 'rxjs' ;
12
+ import { BehaviorSubject , Subject } from 'rxjs' ;
13
13
import { FetchStatus } from '../../../types' ;
14
14
import {
15
15
AvailableFields$ ,
16
16
DataDocuments$ ,
17
+ DataFetch$ ,
17
18
DataMain$ ,
18
19
DataTotalHits$ ,
19
20
RecordRawType ,
@@ -72,6 +73,15 @@ jest.mock('@kbn/unified-field-list-plugin/public', () => {
72
73
return {
73
74
...originalModule ,
74
75
getVisualizeInformation : jest . fn ( ( ) => Promise . resolve ( mockCanVisualize ) ) ,
76
+ useQuerySubscriber : jest . fn ( ( ) => ( {
77
+ query : {
78
+ query : 'query' ,
79
+ language : 'kuery' ,
80
+ } ,
81
+ filters : [ ] ,
82
+ fromDate : 'now-15m' ,
83
+ toDate : 'now' ,
84
+ } ) ) ,
75
85
} ;
76
86
} ) ;
77
87
@@ -120,6 +130,7 @@ describe('useDiscoverHistogram', () => {
120
130
recordRawType : isPlainRecord ? RecordRawType . PLAIN : RecordRawType . DOCUMENT ,
121
131
foundDocuments : true ,
122
132
} ) as DataMain$ ,
133
+ savedSearchFetch$ = new Subject ( ) as DataFetch$ ,
123
134
} : {
124
135
isPlainRecord ?: boolean ;
125
136
isTimeBased ?: boolean ;
@@ -131,6 +142,7 @@ describe('useDiscoverHistogram', () => {
131
142
inspectorAdapters ?: InspectorAdapters ;
132
143
totalHits$ ?: DataTotalHits$ ;
133
144
main$ ?: DataMain$ ;
145
+ savedSearchFetch$ ?: DataFetch$ ;
134
146
} = { } ) => {
135
147
mockStorage = storage ;
136
148
mockCanVisualize = canVisualize ;
@@ -161,6 +173,7 @@ describe('useDiscoverHistogram', () => {
161
173
const initialProps = {
162
174
stateContainer,
163
175
savedSearchData$,
176
+ savedSearchFetch$,
164
177
dataView : dataViewWithTimefieldMock ,
165
178
savedSearch : savedSearchMock ,
166
179
isTimeBased,
@@ -188,11 +201,20 @@ describe('useDiscoverHistogram', () => {
188
201
return { hook, initialProps } ;
189
202
} ;
190
203
191
- it ( 'should return undefined if there is no search session' , async ( ) => {
192
- const {
193
- hook : { result } ,
194
- } = await renderUseDiscoverHistogram ( { searchSessionId : null } ) ;
195
- expect ( result . current ) . toBeUndefined ( ) ;
204
+ describe ( 'result' , ( ) => {
205
+ it ( 'should return undefined if there is no search session' , async ( ) => {
206
+ const {
207
+ hook : { result } ,
208
+ } = await renderUseDiscoverHistogram ( { searchSessionId : null } ) ;
209
+ expect ( result . current ) . toBeUndefined ( ) ;
210
+ } ) ;
211
+
212
+ it ( 'it should not return undefined if there is no search session, but isPlainRecord is true' , async ( ) => {
213
+ const {
214
+ hook : { result } ,
215
+ } = await renderUseDiscoverHistogram ( { searchSessionId : null , isPlainRecord : true } ) ;
216
+ expect ( result . current ) . toBeDefined ( ) ;
217
+ } ) ;
196
218
} ) ;
197
219
198
220
describe ( 'contexts' , ( ) => {
@@ -263,6 +285,21 @@ describe('useDiscoverHistogram', () => {
263
285
} ) ;
264
286
} ) ;
265
287
288
+ describe ( 'search params' , ( ) => {
289
+ it ( 'should return the correct query, filters, and timeRange' , async ( ) => {
290
+ const { hook } = await renderUseDiscoverHistogram ( ) ;
291
+ expect ( hook . result . current ?. query ) . toEqual ( {
292
+ query : 'query' ,
293
+ language : 'kuery' ,
294
+ } ) ;
295
+ expect ( hook . result . current ?. filters ) . toEqual ( [ ] ) ;
296
+ expect ( hook . result . current ?. timeRange ) . toEqual ( {
297
+ from : 'now-15m' ,
298
+ to : 'now' ,
299
+ } ) ;
300
+ } ) ;
301
+ } ) ;
302
+
266
303
describe ( 'onEditVisualization' , ( ) => {
267
304
it ( 'returns a callback for onEditVisualization when the data view can be visualized' , async ( ) => {
268
305
const {
@@ -364,7 +401,7 @@ describe('useDiscoverHistogram', () => {
364
401
} = await renderUseDiscoverHistogram ( { inspectorAdapters } ) ;
365
402
expect ( inspectorAdapters . lensRequests ) . toBeUndefined ( ) ;
366
403
act ( ( ) => {
367
- result . current ?. onChartLoad ( { complete : true , adapters : { requests : lensRequests } } ) ;
404
+ result . current ?. onChartLoad ( { adapters : { requests : lensRequests } } ) ;
368
405
} ) ;
369
406
expect ( inspectorAdapters . lensRequests ) . toBeDefined ( ) ;
370
407
} ) ;
@@ -486,4 +523,63 @@ describe('useDiscoverHistogram', () => {
486
523
expect ( mockCheckHitCount ) . not . toHaveBeenCalled ( ) ;
487
524
} ) ;
488
525
} ) ;
526
+
527
+ describe ( 'refetching' , ( ) => {
528
+ it ( "should call input$.next({ type: 'refetch' }) when savedSearchFetch$ is triggered" , async ( ) => {
529
+ const savedSearchFetch$ = new BehaviorSubject ( { reset : false , searchSessionId : '1234' } ) ;
530
+ const { hook } = await renderUseDiscoverHistogram ( { savedSearchFetch$ } ) ;
531
+ const onRefetch = jest . fn ( ) ;
532
+ hook . result . current ?. input$ . subscribe ( onRefetch ) ;
533
+ act ( ( ) => {
534
+ savedSearchFetch$ . next ( { reset : false , searchSessionId : '1234' } ) ;
535
+ } ) ;
536
+ expect ( onRefetch ) . toHaveBeenCalledWith ( { type : 'refetch' } ) ;
537
+ } ) ;
538
+
539
+ it ( "should not call input$.next({ type: 'refetch' }) when searchSessionId is not set" , async ( ) => {
540
+ const savedSearchFetch$ = new BehaviorSubject ( { reset : false , searchSessionId : '1234' } ) ;
541
+ const { hook } = await renderUseDiscoverHistogram ( {
542
+ savedSearchFetch$,
543
+ searchSessionId : null ,
544
+ } ) ;
545
+ const onRefetch = jest . fn ( ) ;
546
+ hook . result . current ?. input$ . subscribe ( onRefetch ) ;
547
+ act ( ( ) => {
548
+ savedSearchFetch$ . next ( { reset : false , searchSessionId : '1234' } ) ;
549
+ } ) ;
550
+ expect ( onRefetch ) . not . toHaveBeenCalled ( ) ;
551
+ } ) ;
552
+
553
+ it ( "should call input$.next({ type: 'refetch' }) when searchSessionId is not set and isPlainRecord is true" , async ( ) => {
554
+ const savedSearchFetch$ = new BehaviorSubject ( { reset : false , searchSessionId : '1234' } ) ;
555
+ const { hook } = await renderUseDiscoverHistogram ( {
556
+ savedSearchFetch$,
557
+ searchSessionId : null ,
558
+ isPlainRecord : true ,
559
+ } ) ;
560
+ const onRefetch = jest . fn ( ) ;
561
+ hook . result . current ?. input$ . subscribe ( onRefetch ) ;
562
+ act ( ( ) => {
563
+ savedSearchFetch$ . next ( { reset : false , searchSessionId : '1234' } ) ;
564
+ } ) ;
565
+ expect ( onRefetch ) . toHaveBeenCalledWith ( { type : 'refetch' } ) ;
566
+ } ) ;
567
+
568
+ it ( 'should skip the next refetch when state.hideChart changes from true to false' , async ( ) => {
569
+ const savedSearchFetch$ = new BehaviorSubject ( { reset : false , searchSessionId : '1234' } ) ;
570
+ const { hook } = await renderUseDiscoverHistogram ( { savedSearchFetch$ } ) ;
571
+ const onRefetch = jest . fn ( ) ;
572
+ hook . result . current ?. input$ . subscribe ( onRefetch ) ;
573
+ act ( ( ) => {
574
+ hook . result . current ?. onChartHiddenChange ( true ) ;
575
+ } ) ;
576
+ act ( ( ) => {
577
+ hook . result . current ?. onChartHiddenChange ( false ) ;
578
+ } ) ;
579
+ act ( ( ) => {
580
+ savedSearchFetch$ . next ( { reset : false , searchSessionId : '1234' } ) ;
581
+ } ) ;
582
+ expect ( onRefetch ) . not . toHaveBeenCalled ( ) ;
583
+ } ) ;
584
+ } ) ;
489
585
} ) ;
0 commit comments