Skip to content

Commit 9b43118

Browse files
authored
test(enzyme): Migrate Enzyme to RTL (#2714)
1 parent 591459e commit 9b43118

30 files changed

+588
-1192
lines changed

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = {
1212
preset: 'ts-jest',
1313
testEnvironment: 'jsdom',
1414
setupFilesAfterEnv: [
15-
'<rootDir>/scripts/setup_enzyme.ts',
15+
'<rootDir>/scripts/setup_tests.ts',
16+
'@testing-library/jest-dom',
1617
'<rootDir>/scripts/custom_matchers.mock.ts',
1718
'jest-extended/all',
1819
],

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
"@semantic-release/npm": "^9.0.1",
8787
"@semantic-release/release-notes-generator": "^10.0.3",
8888
"@storybook/react": "^6.3.7",
89+
"@testing-library/jest-dom": "^6.4.8",
90+
"@testing-library/react": "^14.2.1",
8991
"@types/chroma-js": "^2.4.2",
9092
"@types/classnames": "^2.2.7",
9193
"@types/color": "^3.0.1",
@@ -95,7 +97,6 @@
9597
"@types/d3-interpolate": "^3.0.1",
9698
"@types/d3-scale": "^3.3.0",
9799
"@types/d3-shape": "^2.0.0",
98-
"@types/enzyme": "^3.9.0",
99100
"@types/jest": "^30.0.0",
100101
"@types/lodash": "^4.14.121",
101102
"@types/luxon": "^1.25.0",
@@ -116,8 +117,6 @@
116117
"commitizen": "^4.2.3",
117118
"cross-env": "^7.0.2",
118119
"cz-conventional-changelog": "^3.3.0",
119-
"enzyme": "^3.11.0",
120-
"@cfaester/enzyme-adapter-react-18": "^0.8.0",
121120
"eslint": "^8.57.0",
122121
"eslint-config-airbnb": "^19.0.4",
123122
"eslint-config-airbnb-typescript": "^18.0.0",

packages/charts/src/chart_types/xy_chart/annotations/line/tooltip.test.tsx

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
import { mount } from 'enzyme';
9+
import { render, fireEvent, screen } from '@testing-library/react';
1010
import React from 'react';
1111

1212
import type { AnnotationLineProps } from './types';
@@ -29,7 +29,7 @@ import type { AnnotationDimensions } from '../types';
2929
describe('Annotation tooltips', () => {
3030
describe('Line annotation tooltips', () => {
3131
test('should show tooltip on mouseenter', () => {
32-
const wrapper = mount(
32+
render(
3333
<Chart size={[100, 100]}>
3434
<Settings
3535
theme={{
@@ -56,20 +56,23 @@ describe('Annotation tooltips', () => {
5656
/>
5757
</Chart>,
5858
);
59-
const annotation = wrapper.find('.echAnnotation__marker');
60-
expect(annotation).toHaveLength(1);
61-
expect(wrapper.find('.echAnnotation')).toHaveLength(0);
62-
annotation.simulate('mouseenter');
63-
const header = wrapper.find('.echTooltipHeader');
64-
expect(header).toHaveLength(1);
65-
expect(header.text()).toEqual('2');
66-
expect(wrapper.find('.echAnnotation__details').text()).toEqual('foo');
67-
annotation.simulate('mouseleave');
68-
expect(annotation.find('.echTooltipHeader')).toHaveLength(0);
59+
60+
expect(screen.queryByTestId('echAnnotationMarker')).not.toBeNull();
61+
const marker = screen.getByTestId('echAnnotationMarker');
62+
expect(screen.queryAllByTestId('echAnnotation')).toHaveLength(0);
63+
64+
if (marker) fireEvent.mouseEnter(marker);
65+
66+
expect(screen.queryByTestId('echTooltip')).not.toBeNull();
67+
expect(screen.getByTestId('echTooltipHeader').textContent).toBe('2');
68+
expect(screen.getByTestId('echAnnotationDetails').textContent).toBe('foo');
69+
70+
if (marker) fireEvent.mouseLeave(marker);
71+
expect(screen.queryByTestId('echTooltipHeader')).toBeNull();
6972
});
7073

71-
test('should now show tooltip if hidden', () => {
72-
const wrapper = mount(
74+
test('should not show tooltip when using hideTooltips', () => {
75+
render(
7376
<Chart size={[100, 100]}>
7477
<Settings
7578
theme={{
@@ -97,10 +100,12 @@ describe('Annotation tooltips', () => {
97100
/>
98101
</Chart>,
99102
);
100-
const annotation = wrapper.find('.echAnnotation__marker');
101-
expect(wrapper.find('.echAnnotation')).toHaveLength(0);
102-
annotation.simulate('mouseenter');
103-
expect(wrapper.find('.echTooltip__header')).toHaveLength(0);
103+
104+
expect(screen.queryAllByTestId('echAnnotation')).toHaveLength(0);
105+
const marker = screen.getByTestId('echAnnotationMarker');
106+
if (marker) fireEvent.mouseEnter(marker);
107+
108+
expect(screen.queryByTestId('echTooltip')).toBeNull();
104109
});
105110
});
106111

packages/charts/src/chart_types/xy_chart/renderer/dom/annotations/annotation_tooltip.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const AnnotationTooltip = ({ state, chartRef, chartId, onScroll, zIndex }
4040
actionsLoading=""
4141
noActionsLoaded=""
4242
className="echAnnotation"
43+
data-testid="echAnnotation"
4344
>
4445
<TooltipContent {...state} />
4546
</TooltipWrapper>

packages/charts/src/chart_types/xy_chart/renderer/dom/annotations/line_marker.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export function LineMarker({
127127
return clickable ? (
128128
<button
129129
className="echAnnotation__marker"
130+
data-testid="echAnnotationMarker"
130131
onMouseEnter={() => {
131132
onDOMElementEnter({
132133
createdBySpecId: specId,
@@ -153,6 +154,7 @@ export function LineMarker({
153154
) : (
154155
<div
155156
className="echAnnotation__marker"
157+
data-testid="echAnnotationMarker"
156158
onMouseEnter={() => {
157159
onDOMElementEnter({
158160
createdBySpecId: specId,

packages/charts/src/chart_types/xy_chart/renderer/dom/annotations/tooltip_content.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const TooltipContent = ({
2727
<>
2828
<TooltipHeader>{header}</TooltipHeader>
2929
<TooltipDivider />
30-
<div className="echAnnotation__details">
30+
<div className="echAnnotation__details" data-testid="echAnnotationDetails">
3131
{customTooltipDetails ? renderWithProps(customTooltipDetails, { details }) : details}
3232
</div>
3333
</>
@@ -41,7 +41,11 @@ export const TooltipContent = ({
4141
return null;
4242
}
4343

44-
return <div className="echAnnotation__details">{tooltipContent}</div>;
44+
return (
45+
<div className="echAnnotation__details" data-testid="echAnnotationDetails">
46+
{tooltipContent}
47+
</div>
48+
);
4549
}, [datum, customTooltipDetails]);
4650

4751
if (CustomTooltip) {

packages/charts/src/chart_types/xy_chart/specs/line_annotation.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
import { mount } from 'enzyme';
9+
import { render } from '@testing-library/react';
1010
import React from 'react';
1111
import { Provider } from 'react-redux';
1212
import type { Store } from 'redux';
@@ -45,9 +45,9 @@ function LineAnnotationChart(props: { chartStore: Store<GlobalChartState> }) {
4545
describe('Line annotation', () => {
4646
it('Should always be available on the on every render', () => {
4747
const chartStore = createChartStore('chart_id');
48-
const wrapper = mount(<LineAnnotationChart chartStore={chartStore} />);
48+
const { rerender } = render(<LineAnnotationChart chartStore={chartStore} />);
4949
expect(chartStore.getState().specs.threshold).toBeDefined();
50-
wrapper.setProps({});
50+
rerender(<LineAnnotationChart chartStore={chartStore} />);
5151
expect(chartStore.getState().specs.threshold).toBeDefined();
5252
});
5353
});

packages/charts/src/components/__snapshots__/chart.test.tsx.snap

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

0 commit comments

Comments
 (0)