77 * License v3.0 only", or the "Server Side Public License, v 1".
88 */
99
10- import React from 'react' ;
11- import { EuiProgress , EuiButtonIcon } from '@elastic/eui' ;
12- import { mountWithIntl } from '@kbn/test-jest-helpers' ;
13- import type { DataView } from '@kbn/data-views-plugin/common' ;
1410import type { FieldTopValuesProps } from './field_top_values' ;
15- import { FieldTopValues } from './field_top_values' ;
16- import type { ReactWrapper } from '@kbn/test-jest-helpers/src/testbed/types' ;
17- import { fieldFormatsServiceMock } from '@kbn/field-formats-plugin/public/mocks' ;
18- import { ES_FIELD_TYPES , KBN_FIELD_TYPES } from '@kbn/field-types' ;
11+ import React from 'react' ;
12+ import userEvent from '@testing-library/user-event' ;
13+ import { dataViewMock } from '@kbn/discover-utils/src/__mocks__' ;
1914import { EMPTY_LABEL } from '@kbn/field-formats-common' ;
20-
21- // Similar to wrapper.text() but filtered by a selector
22- const getChildrenTextBySelector = ( wrapper : ReactWrapper , selector : string ) => {
23- let text = '' ;
24- const children = wrapper . find ( selector ) ;
25-
26- children . forEach ( ( element ) => {
27- text += element . text ( ) ;
28- } ) ;
29-
30- return text ;
15+ import { FieldTopValues } from './field_top_values' ;
16+ import { renderWithI18n } from '@kbn/test-jest-helpers' ;
17+ import { screen } from '@testing-library/react' ;
18+
19+ const expectTopValueProgress = ( label : string , percentage : string ) => {
20+ expect ( screen . getByRole ( 'progressbar' , { name : label } ) ) . toHaveAttribute (
21+ 'aria-valuetext' ,
22+ percentage
23+ ) ;
3124} ;
3225
3326describe ( 'UnifiedFieldList <FieldTopValues />' , ( ) => {
3427 let defaultProps : FieldTopValuesProps ;
35- let dataView : DataView ;
3628
3729 beforeEach ( ( ) => {
38- dataView = {
39- id : '1' ,
40- title : 'my-fake-index-pattern' ,
41- timeFieldName : 'timestamp' ,
42- fields : [
43- {
44- name : 'source' ,
45- displayName : 'source' ,
46- type : 'string' ,
47- aggregatable : true ,
48- searchable : true ,
49- filterable : true ,
50- } ,
51- ] ,
52- getFormatterForField : jest . fn ( ( ) => ( {
53- convertToText : jest . fn ( ( s : unknown ) =>
54- fieldFormatsServiceMock
55- . createStartContract ( )
56- . getDefaultInstance ( KBN_FIELD_TYPES . STRING , [ ES_FIELD_TYPES . STRING ] )
57- . convertToText ( s )
58- ) ,
59- } ) ) ,
60- } as unknown as DataView ;
61-
6230 defaultProps = {
6331 areExamples : false ,
64- dataView,
65- field : dataView . fields . find ( ( f ) => f . name === 'source' ) ! ,
66- sampledValuesCount : 5000 ,
6732 buckets : [
6833 {
6934 count : 500 ,
@@ -76,34 +41,42 @@ describe('UnifiedFieldList <FieldTopValues />', () => {
7641 ] ,
7742 color : '#000' ,
7843 'data-test-subj' : 'testing' ,
44+ dataView : dataViewMock ,
45+ field : dataViewMock . getFieldByName ( 'extension' ) ! ,
46+ sampledValuesCount : 5000 ,
7947 } ;
8048 } ) ;
8149
82- it ( 'should render correctly without filter actions' , async ( ) => {
83- const wrapper = mountWithIntl ( < FieldTopValues { ...defaultProps } /> ) ;
84- const text = getChildrenTextBySelector ( wrapper , 'div.euiProgress__data' ) ;
50+ it ( 'should render correctly without filter actions' , ( ) => {
51+ renderWithI18n ( < FieldTopValues { ...defaultProps } /> ) ;
8552
86- expect ( text ) . toBe ( 'sourceA10.0%sourceB0.0%Other90.0%' ) ;
87- expect ( wrapper . find ( EuiProgress ) ) . toHaveLength ( 3 ) ;
88- expect ( wrapper . find ( EuiButtonIcon ) ) . toHaveLength ( 0 ) ;
53+ expectTopValueProgress ( 'sourceA' , '10.0%' ) ;
54+ expectTopValueProgress ( 'sourceB' , '0.0%' ) ;
55+ expectTopValueProgress ( 'Other' , '90.0%' ) ;
56+
57+ expect ( screen . getAllByRole ( 'progressbar' ) ) . toHaveLength ( 3 ) ;
58+ expect ( screen . queryAllByRole ( 'button' ) ) . toHaveLength ( 0 ) ;
8959 } ) ;
9060
9161 it ( 'should render correctly with filter actions' , async ( ) => {
9262 const mockAddFilter = jest . fn ( ) ;
93- const wrapper = mountWithIntl ( < FieldTopValues { ...defaultProps } onAddFilter = { mockAddFilter } /> ) ;
94- const text = getChildrenTextBySelector ( wrapper , 'div.euiProgress__data' ) ;
9563
96- expect ( text ) . toBe ( 'sourceA10.0%sourceB0.0%Other90.0%' ) ;
97- expect ( wrapper . find ( EuiProgress ) ) . toHaveLength ( 3 ) ;
98- expect ( wrapper . find ( EuiButtonIcon ) ) . toHaveLength ( 4 ) ;
64+ renderWithI18n ( < FieldTopValues { ...defaultProps } onAddFilter = { mockAddFilter } /> ) ;
65+
66+ expectTopValueProgress ( 'sourceA' , '10.0%' ) ;
67+ expectTopValueProgress ( 'sourceB' , '0.0%' ) ;
68+ expectTopValueProgress ( 'Other' , '90.0%' ) ;
9969
100- wrapper . find ( EuiButtonIcon ) . first ( ) . simulate ( 'click' ) ;
70+ expect ( screen . getAllByRole ( 'progressbar' ) ) . toHaveLength ( 3 ) ;
71+ expect ( screen . getAllByRole ( 'button' ) ) . toHaveLength ( 4 ) ;
72+
73+ await userEvent . click ( screen . getByRole ( 'button' , { name : 'Filter for extension: "sourceA"' } ) ) ;
10174
10275 expect ( mockAddFilter ) . toHaveBeenCalledWith ( defaultProps . field , 'sourceA' , '+' ) ;
10376 } ) ;
10477
105- it ( 'should render correctly without Other section' , async ( ) => {
106- const wrapper = mountWithIntl (
78+ it ( 'should render correctly without Other section' , ( ) => {
79+ renderWithI18n (
10780 < FieldTopValues
10881 { ...defaultProps }
10982 buckets = { [
@@ -122,13 +95,16 @@ describe('UnifiedFieldList <FieldTopValues />', () => {
12295 ] }
12396 />
12497 ) ;
125- const text = getChildrenTextBySelector ( wrapper , 'div.euiProgress__data' ) ;
12698
127- expect ( text ) . toBe ( 'sourceA60.0%sourceB30.0%sourceC10.0%' ) ;
99+ expectTopValueProgress ( 'sourceA' , '60.0%' ) ;
100+ expectTopValueProgress ( 'sourceB' , '30.0%' ) ;
101+ expectTopValueProgress ( 'sourceC' , '10.0%' ) ;
102+
103+ expect ( screen . queryByText ( 'Other' ) ) . not . toBeInTheDocument ( ) ;
128104 } ) ;
129105
130- it ( 'should render correctly with empty strings' , async ( ) => {
131- const wrapper = mountWithIntl (
106+ it ( 'should render correctly with empty strings' , ( ) => {
107+ renderWithI18n (
132108 < FieldTopValues
133109 { ...defaultProps }
134110 buckets = { [
@@ -147,13 +123,15 @@ describe('UnifiedFieldList <FieldTopValues />', () => {
147123 ] }
148124 />
149125 ) ;
150- const text = getChildrenTextBySelector ( wrapper , 'div.euiProgress__data' ) ;
151126
152- expect ( text ) . toBe ( `${ EMPTY_LABEL } 60.0%sourceA30.0%sourceB0.4%Other9.6%` ) ;
127+ expectTopValueProgress ( EMPTY_LABEL , '60.0%' ) ;
128+ expectTopValueProgress ( 'sourceA' , '30.0%' ) ;
129+ expectTopValueProgress ( 'sourceB' , '0.4%' ) ;
130+ expectTopValueProgress ( 'Other' , '9.6%' ) ;
153131 } ) ;
154132
155- it ( 'should render correctly without floating point' , async ( ) => {
156- const wrapper = mountWithIntl (
133+ it ( 'should render correctly without floating point' , ( ) => {
134+ renderWithI18n (
157135 < FieldTopValues
158136 { ...defaultProps }
159137 buckets = { [
@@ -164,8 +142,7 @@ describe('UnifiedFieldList <FieldTopValues />', () => {
164142 ] }
165143 />
166144 ) ;
167- const text = getChildrenTextBySelector ( wrapper , 'div.euiProgress__data' ) ;
168145
169- expect ( text ) . toBe ( 'sourceA100 %') ;
146+ expectTopValueProgress ( 'sourceA' , '100 %') ;
170147 } ) ;
171148} ) ;
0 commit comments