Skip to content

Commit a823128

Browse files
Convert 'unified_field_list_item' tests
1 parent 57368f5 commit a823128

4 files changed

Lines changed: 236 additions & 252 deletions

File tree

src/platform/packages/shared/kbn-unified-field-list/src/components/field_list/field_list.test.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,47 @@
88
*/
99

1010
import React from 'react';
11-
import { EuiText, EuiProgress } from '@elastic/eui';
12-
import { mountWithIntl } from '@kbn/test-jest-helpers';
11+
import { EuiText } from '@elastic/eui';
1312
import { FieldList } from './field_list';
13+
import { renderWithI18n } from '@kbn/test-jest-helpers';
14+
import { screen } from '@testing-library/react';
1415

1516
describe('UnifiedFieldList <FieldList />', () => {
16-
it('should render correctly when processing', async () => {
17-
expect(mountWithIntl(<FieldList isProcessing={true} />).find(EuiProgress)?.length).toBe(1);
18-
expect(mountWithIntl(<FieldList isProcessing={false} />).find(EuiProgress)?.length).toBe(0);
17+
it('should render correctly when processing', () => {
18+
const { unmount } = renderWithI18n(<FieldList isProcessing={true} />);
19+
20+
expect(screen.getByTestId('fieldListLoading')).toBeVisible();
21+
22+
unmount();
23+
24+
renderWithI18n(<FieldList isProcessing={false} />);
25+
26+
expect(screen.queryByTestId('fieldListLoading')).not.toBeInTheDocument();
1927
});
2028

21-
it('should render correctly with content', async () => {
22-
const wrapper = mountWithIntl(
29+
it('should render correctly with content', () => {
30+
renderWithI18n(
2331
<FieldList isProcessing={false}>
2432
<EuiText>{'content'}</EuiText>
2533
</FieldList>
2634
);
2735

28-
expect(wrapper.find(EuiText).first().text()).toBe('content');
36+
expect(screen.getByText('content')).toBeVisible();
2937
});
3038

31-
it('should render correctly with additional elements', async () => {
32-
const wrapper = mountWithIntl(
39+
it('should render correctly with additional elements', () => {
40+
renderWithI18n(
3341
<FieldList
42+
append={<EuiText>{'append'}</EuiText>}
3443
isProcessing={false}
3544
prepend={<EuiText>{'prepend'}</EuiText>}
36-
append={<EuiText>{'append'}</EuiText>}
3745
>
3846
<EuiText>{'content'}</EuiText>
3947
</FieldList>
4048
);
4149

42-
expect(wrapper.find(EuiText).first().text()).toBe('prepend');
43-
expect(wrapper.find(EuiText).at(1).text()).toBe('content');
44-
expect(wrapper.find(EuiText).at(2).text()).toBe('append');
50+
expect(screen.getByText('prepend')).toBeVisible();
51+
expect(screen.getByText('content')).toBeVisible();
52+
expect(screen.getByText('append')).toBeVisible();
4553
});
4654
});

src/platform/packages/shared/kbn-unified-field-list/src/components/field_stats/field_number_summary.test.tsx

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,62 @@
99

1010
import React from 'react';
1111
import { createStubDataView } from '@kbn/data-views-plugin/common/data_views/data_view.stub';
12-
import { mountWithIntl } from '@kbn/test-jest-helpers';
1312
import { FieldNumberSummary } from './field_number_summary';
13+
import { renderWithI18n } from '@kbn/test-jest-helpers';
14+
import { screen, within } from '@testing-library/react';
1415

1516
const dataView = createStubDataView({
1617
spec: {
17-
id: 'test',
18-
title: 'test',
1918
fields: {
2019
bytes_counter: {
21-
timeSeriesMetric: 'counter',
22-
name: 'bytes_counter',
23-
type: 'number',
24-
esTypes: ['long'],
2520
aggregatable: true,
26-
searchable: true,
2721
count: 10,
22+
esTypes: ['long'],
23+
isMapped: true,
24+
name: 'bytes_counter',
2825
readFromDocValues: true,
2926
scripted: false,
30-
isMapped: true,
27+
searchable: true,
28+
timeSeriesMetric: 'counter',
29+
type: 'number',
3130
},
3231
},
32+
id: 'test',
33+
title: 'test',
3334
},
3435
});
3536

3637
describe('UnifiedFieldList <FieldNumberSummary />', () => {
37-
it('should render min and max correctly', async () => {
38-
const wrapper = mountWithIntl(
38+
it('should render min and max correctly', () => {
39+
renderWithI18n(
3940
<FieldNumberSummary
41+
data-test-subj="test-subj"
4042
dataView={dataView}
4143
field={dataView.getFieldByName('bytes_counter')!}
4244
numberSummary={{
43-
minValue: 45,
4445
maxValue: 12345,
46+
minValue: 45,
4547
}}
46-
data-test-subj="test-subj"
4748
/>
4849
);
4950

50-
expect(wrapper.text()).toBe('min45max12345');
51+
const summary = screen.getByTestId('test-subj-numberSummary');
52+
expect(within(summary).getByText('min')).toBeVisible();
53+
expect(within(summary).getByText('45')).toBeVisible();
54+
expect(within(summary).getByText('max')).toBeVisible();
55+
expect(within(summary).getByText('12345')).toBeVisible();
5156
});
5257

53-
it('should not fail if data is invalid', async () => {
54-
const wrapper = mountWithIntl(
58+
it('should not fail if data is invalid', () => {
59+
renderWithI18n(
5560
<FieldNumberSummary
61+
data-test-subj="test-subj"
5662
dataView={dataView}
5763
field={dataView.getFieldByName('bytes_counter')!}
5864
numberSummary={undefined}
59-
data-test-subj="test-subj"
6065
/>
6166
);
6267

63-
expect(wrapper.isEmptyRender()).toBe(true);
68+
expect(screen.queryByTestId('test-subj-numberSummary')).not.toBeInTheDocument();
6469
});
6570
});

src/platform/packages/shared/kbn-unified-field-list/src/components/field_stats/field_top_values.test.tsx

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,28 @@
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';
1410
import 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__';
1914
import { 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

3326
describe('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

Comments
 (0)