Skip to content

Commit 3e3d79b

Browse files
kibanamachinerbrtj
andauthored
[8.19] [ML] Migrate Enzyme tests to RTL (#225423) (#226102)
# Backport This will backport the following commits from `main` to `8.19`: - [[ML] Migrate `Enzyme` tests to `RTL` (#225423)](#225423) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Robert Jaszczurek","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-06-30T12:10:03Z","message":"[ML] Migrate `Enzyme` tests to `RTL` (#225423)\n\nResolves https://github.com/elastic/kibana/issues/223197","sha":"a01759c410312b65134c9daf505d94f8e55b9c44","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":[":ml","release_note:skip","Team:ML","backport:version","v9.1.0","v8.19.0","v9.2.0"],"title":"[ML] Migrate `Enzyme` tests to `RTL`","number":225423,"url":"https://github.com/elastic/kibana/pull/225423","mergeCommit":{"message":"[ML] Migrate `Enzyme` tests to `RTL` (#225423)\n\nResolves https://github.com/elastic/kibana/issues/223197","sha":"a01759c410312b65134c9daf505d94f8e55b9c44"}},"sourceBranch":"main","suggestedTargetBranches":["8.19"],"targetPullRequestStates":[{"branch":"9.1","label":"v9.1.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/225828","number":225828,"state":"MERGED","mergeCommit":{"sha":"7776cc0162e38bed303dd06b6819667a0e725f64","message":"[9.1] [ML] Migrate `Enzyme` tests to `RTL` (#225423) (#225828)\n\n# Backport\n\nThis will backport the following commits from `main` to `9.1`:\n- [[ML] Migrate `Enzyme` tests to `RTL`\n(#225423)](https://github.com/elastic/kibana/pull/225423)\n\n\n\n### Questions ?\nPlease refer to the [Backport tool\ndocumentation](https://github.com/sorenlouv/backport)\n\n\n\nCo-authored-by: Robert Jaszczurek <[email protected]>"}},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/225423","number":225423,"mergeCommit":{"message":"[ML] Migrate `Enzyme` tests to `RTL` (#225423)\n\nResolves https://github.com/elastic/kibana/issues/223197","sha":"a01759c410312b65134c9daf505d94f8e55b9c44"}}]}] BACKPORT--> Co-authored-by: Robert Jaszczurek <[email protected]>
1 parent 2b5339c commit 3e3d79b

File tree

16 files changed

+793
-664
lines changed

16 files changed

+793
-664
lines changed

x-pack/platform/plugins/shared/ml/public/application/components/custom_urls/custom_url_editor/__snapshots__/list.test.tsx.snap

Lines changed: 540 additions & 339 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/platform/plugins/shared/ml/public/application/components/custom_urls/custom_url_editor/list.test.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
*/
77

88
import React from 'react';
9-
import { shallow } from 'enzyme';
9+
import { render } from '@testing-library/react';
10+
import userEvent from '@testing-library/user-event';
1011
import type { Job } from '../../../../../common/types/anomaly_detection_jobs';
1112

1213
import type { CustomUrlListProps } from './list';
1314
import { CustomUrlList } from './list';
15+
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react';
1416

1517
jest.mock('../../../contexts/kibana');
1618

@@ -53,27 +55,32 @@ function prepareTest(setCustomUrlsFn: jest.Mock) {
5355
dataViewListItems: [],
5456
};
5557

56-
return shallow(<CustomUrlList {...props} />);
58+
return render(
59+
<IntlProvider>
60+
<CustomUrlList {...props} />
61+
</IntlProvider>
62+
);
5763
}
5864

5965
describe('CustomUrlList', () => {
60-
const setCustomUrls = jest.fn(() => {});
66+
const setCustomUrls = jest.fn();
6167

6268
test('renders a list of custom URLs', () => {
63-
const wrapper = prepareTest(setCustomUrls);
64-
expect(wrapper).toMatchSnapshot();
69+
const { container } = prepareTest(setCustomUrls);
70+
expect(container.firstChild).toMatchSnapshot();
6571
});
6672

67-
test('switches custom URL field to textarea and calls setCustomUrls on change', () => {
68-
const wrapper = prepareTest(setCustomUrls);
69-
wrapper.update();
70-
const url1LabelInput = wrapper.find('[data-test-subj="mlJobEditCustomUrlInput_0"]');
71-
url1LabelInput.simulate('focus');
72-
wrapper.update();
73-
const url1LabelTextarea = wrapper.find('[data-test-subj="mlJobEditCustomUrlTextarea_0"]');
74-
expect(url1LabelTextarea).toBeDefined();
75-
url1LabelTextarea.simulate('change', { target: { value: 'Edit' } });
76-
wrapper.update();
73+
test('switches custom URL field to textarea and calls setCustomUrls on change', async () => {
74+
const { getByTestId } = prepareTest(setCustomUrls);
75+
const user = userEvent.setup();
76+
77+
const input = getByTestId('mlJobEditCustomUrlInput_0');
78+
await user.click(input);
79+
80+
const textarea = getByTestId('mlJobEditCustomUrlTextarea_0');
81+
expect(textarea).toBeInTheDocument();
82+
83+
await user.type(textarea, 'Edit');
7784
expect(setCustomUrls).toHaveBeenCalled();
7885
});
7986
});

x-pack/platform/plugins/shared/ml/public/application/components/custom_urls/custom_url_editor/list.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ export const CustomUrlList: FC<CustomUrlListProps> = ({
211211
: [];
212212

213213
return (
214-
<>
215-
<EuiFlexGroup key={`url_${index}`} data-test-subj={`mlJobEditCustomUrlItem_${index}`}>
214+
<React.Fragment key={`url_${index}`}>
215+
<EuiFlexGroup data-test-subj={`mlJobEditCustomUrlItem_${index}`}>
216216
<EuiFlexItem grow={false}>
217217
<EuiFormRow
218218
label={
@@ -341,7 +341,7 @@ export const CustomUrlList: FC<CustomUrlListProps> = ({
341341
</EuiFlexItem>
342342
</EuiFlexGroup>
343343
<EuiSpacer size="m" />
344-
</>
344+
</React.Fragment>
345345
);
346346
});
347347

x-pack/platform/plugins/shared/ml/public/application/components/job_selector/timerange_bar/timerange_bar.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* 2.0.
66
*/
77

8-
import { mount } from 'enzyme';
98
import React from 'react';
9+
import { render } from '@testing-library/react';
1010
import { TimeRangeBar } from './timerange_bar';
1111

1212
describe('TimeRangeBar', () => {
@@ -17,16 +17,16 @@ describe('TimeRangeBar', () => {
1717
};
1818

1919
test('Renders gantt bar when isRunning is false', () => {
20-
const wrapper = mount(<TimeRangeBar timerange={timeRange} />);
21-
const ganttBar = wrapper.find('div[data-test-subj="mlJobSelectorGanttBar"]');
20+
const { getByTestId } = render(<TimeRangeBar timerange={timeRange} />);
21+
const ganttBar = getByTestId('mlJobSelectorGanttBar');
2222

23-
expect(ganttBar).toHaveLength(1);
23+
expect(ganttBar).toBeInTheDocument();
2424
});
2525

2626
test('Renders running animation bar when isRunning is true', () => {
27-
const wrapper = mount(<TimeRangeBar timerange={timeRange} isRunning={true} />);
28-
const runningBar = wrapper.find('div[data-test-subj="mlJobSelectorGanttBarRunning"]');
27+
const { getByTestId } = render(<TimeRangeBar timerange={timeRange} isRunning={true} />);
28+
const runningBar = getByTestId('mlJobSelectorGanttBarRunning');
2929

30-
expect(runningBar).toHaveLength(1);
30+
expect(runningBar).toBeInTheDocument();
3131
});
3232
});

x-pack/platform/plugins/shared/ml/public/application/data_frame_analytics/pages/analytics_exploration/components/outlier_exploration/outlier_exploration.test.tsx

Lines changed: 0 additions & 31 deletions
This file was deleted.

x-pack/platform/plugins/shared/ml/public/application/data_frame_analytics/pages/analytics_management/components/create_analytics_button/create_analytics_button.test.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@
55
* 2.0.
66
*/
77

8-
import { mount } from 'enzyme';
98
import React from 'react';
9+
import { render, screen } from '@testing-library/react';
10+
import userEvent from '@testing-library/user-event';
1011

1112
import { CreateAnalyticsButton } from './create_analytics_button';
1213

1314
describe('Data Frame Analytics: <CreateAnalyticsButton />', () => {
14-
test('Minimal initialization', () => {
15-
const wrapper = mount(
16-
<CreateAnalyticsButton isDisabled={false} navigateToSourceSelection={jest.fn()} />
15+
test('renders button with correct text', () => {
16+
render(<CreateAnalyticsButton isDisabled={false} navigateToSourceSelection={jest.fn()} />);
17+
18+
expect(screen.getByText('Create job')).toBeInTheDocument();
19+
});
20+
21+
test('calls navigateToSourceSelection when clicked', async () => {
22+
const navigateToSourceSelection = jest.fn();
23+
const user = userEvent.setup();
24+
25+
render(
26+
<CreateAnalyticsButton
27+
isDisabled={false}
28+
navigateToSourceSelection={navigateToSourceSelection}
29+
/>
1730
);
1831

19-
expect(wrapper.find('EuiButton').text()).toBe('Create job');
32+
await user.click(screen.getByText('Create job'));
33+
expect(navigateToSourceSelection).toHaveBeenCalledTimes(1);
2034
});
2135
});

x-pack/platform/plugins/shared/ml/public/application/explorer/components/explorer_no_influencers_found/__snapshots__/explorer_no_influencers_found.test.js.snap

Lines changed: 3 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/platform/plugins/shared/ml/public/application/explorer/components/explorer_no_influencers_found/explorer_no_influencers_found.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
*/
77

88
import React from 'react';
9-
import { shallow } from 'enzyme';
9+
import { render } from '@testing-library/react';
10+
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react';
1011
import { ExplorerNoInfluencersFound } from './explorer_no_influencers_found';
1112

1213
describe('ExplorerNoInfluencersFound', () => {
1314
test('snapshot', () => {
14-
const wrapper = shallow(<ExplorerNoInfluencersFound viewBySwimlaneFieldName="field_name" />);
15-
expect(wrapper).toMatchSnapshot();
15+
const { container } = render(
16+
<IntlProvider>
17+
<ExplorerNoInfluencersFound viewBySwimlaneFieldName="field_name" />
18+
</IntlProvider>
19+
);
20+
expect(container).toMatchSnapshot();
1621
});
1722
});

x-pack/platform/plugins/shared/ml/public/application/explorer/components/explorer_no_results_found/__snapshots__/explorer_no_results_found.test.js.snap

Lines changed: 36 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/platform/plugins/shared/ml/public/application/explorer/components/explorer_no_results_found/explorer_no_results_found.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
*/
77

88
import React from 'react';
9-
import { shallow } from 'enzyme';
9+
import { render } from '@testing-library/react';
10+
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react';
1011
import { ExplorerNoResultsFound } from './explorer_no_results_found';
1112

12-
describe('ExplorerNoInfluencersFound', () => {
13+
describe('ExplorerNoResultsFound', () => {
1314
test('snapshot', () => {
14-
const wrapper = shallow(<ExplorerNoResultsFound />);
15-
expect(wrapper).toMatchSnapshot();
15+
const { container } = render(
16+
<IntlProvider>
17+
<ExplorerNoResultsFound hasResults={false} selectedJobsRunning={false} />
18+
</IntlProvider>
19+
);
20+
expect(container.firstChild).toMatchSnapshot();
1621
});
1722
});

0 commit comments

Comments
 (0)