Skip to content

Commit 1a1548d

Browse files
authored
chore(shared components): Migrate enzyme to RTL (#26258)
1 parent 8e2f818 commit 1a1548d

File tree

15 files changed

+459
-590
lines changed

15 files changed

+459
-590
lines changed

superset-frontend/src/components/Button/Button.test.tsx

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,35 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
20-
import { isValidElement } from 'react';
21-
import { ReactWrapper } from 'enzyme';
22-
import { styledMount as mount } from 'spec/helpers/theming';
19+
import { fireEvent, render } from 'spec/helpers/testing-library';
2320
import Button from '.';
2421
import {
2522
ButtonGallery,
2623
SIZES as buttonSizes,
2724
STYLES as buttonStyles,
2825
} from './Button.stories';
2926

30-
describe('Button', () => {
31-
let wrapper: ReactWrapper;
32-
33-
// test the basic component
34-
it('renders the base component', () => {
35-
expect(isValidElement(<Button />)).toBe(true);
36-
});
37-
38-
it('works with an onClick handler', () => {
39-
const mockAction = jest.fn();
40-
wrapper = mount(<Button onClick={mockAction} />);
41-
wrapper.find('Button').first().simulate('click');
42-
expect(mockAction).toHaveBeenCalled();
43-
});
27+
test('works with an onClick handler', () => {
28+
const mockAction = jest.fn();
29+
const { getByRole } = render(<Button onClick={mockAction} />);
30+
fireEvent.click(getByRole('button'));
31+
expect(mockAction).toHaveBeenCalled();
32+
});
4433

45-
it('does not handle onClicks when disabled', () => {
46-
const mockAction = jest.fn();
47-
wrapper = mount(<Button onClick={mockAction} disabled />);
48-
wrapper.find('Button').first().simulate('click');
49-
expect(mockAction).toHaveBeenCalledTimes(0);
50-
});
34+
test('does not handle onClicks when disabled', () => {
35+
const mockAction = jest.fn();
36+
const { getByRole } = render(<Button onClick={mockAction} disabled />);
37+
fireEvent.click(getByRole('button'));
38+
expect(mockAction).toHaveBeenCalledTimes(0);
39+
});
5140

52-
// test stories from the storybook!
53-
it('All the sorybook gallery variants mount', () => {
54-
wrapper = mount(<ButtonGallery />);
41+
// test stories from the storybook!
42+
test('All the sorybook gallery variants mount', () => {
43+
const { getAllByRole } = render(<ButtonGallery />);
5544

56-
const permutationCount =
57-
Object.values(buttonStyles.options).filter(o => o).length *
58-
Object.values(buttonSizes.options).length;
45+
const permutationCount =
46+
Object.values(buttonStyles.options).filter(o => o).length *
47+
Object.values(buttonSizes.options).length;
5948

60-
expect(wrapper.find(Button).length).toEqual(permutationCount);
61-
});
49+
expect(getAllByRole('button')).toHaveLength(permutationCount);
6250
});

superset-frontend/src/components/Chart/ChartRenderer.test.jsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
import { shallow } from 'enzyme';
20-
import { SuperChart } from '@superset-ui/core';
19+
import { render } from 'spec/helpers/testing-library';
2120

2221
import ChartRenderer from 'src/components/Chart/ChartRenderer';
2322

23+
jest.mock('@superset-ui/core', () => ({
24+
...jest.requireActual('@superset-ui/core'),
25+
SuperChart: ({ formData }) => (
26+
<div data-test="mock-super-chart">{JSON.stringify(formData)}</div>
27+
),
28+
}));
29+
30+
jest.mock(
31+
'src/components/Chart/ChartContextMenu/ChartContextMenu',
32+
() => () => <div data-test="mock-chart-context-menu" />,
33+
);
34+
2435
const requiredProps = {
2536
chartId: 1,
2637
datasource: {},
@@ -31,18 +42,18 @@ const requiredProps = {
3142
vizType: 'table',
3243
};
3344

34-
describe('ChartRenderer', () => {
35-
it('should render SuperChart', () => {
36-
const wrapper = shallow(
37-
<ChartRenderer {...requiredProps} chartIsStale={false} />,
38-
);
39-
expect(wrapper.find(SuperChart)).toExist();
40-
});
45+
test('should render SuperChart', () => {
46+
const { getByTestId } = render(
47+
<ChartRenderer {...requiredProps} chartIsStale={false} />,
48+
);
49+
expect(getByTestId('mock-super-chart')).toBeInTheDocument();
50+
});
4151

42-
it('should use latestQueryFormData instead of formData when chartIsStale is true', () => {
43-
const wrapper = shallow(<ChartRenderer {...requiredProps} chartIsStale />);
44-
expect(wrapper.find(SuperChart).prop('formData')).toEqual({
45-
testControl: 'bar',
46-
});
47-
});
52+
test('should use latestQueryFormData instead of formData when chartIsStale is true', () => {
53+
const { getByTestId } = render(
54+
<ChartRenderer {...requiredProps} chartIsStale />,
55+
);
56+
expect(getByTestId('mock-super-chart')).toHaveTextContent(
57+
JSON.stringify({ testControl: 'bar' }),
58+
);
4859
});

superset-frontend/src/components/Checkbox/Checkbox.test.tsx

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,62 +16,46 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
import { fireEvent, render } from 'spec/helpers/testing-library';
1920

20-
import { isValidElement } from 'react';
21-
import { ReactWrapper } from 'enzyme';
22-
import {
23-
styledMount as mount,
24-
styledShallow as shallow,
25-
} from 'spec/helpers/theming';
21+
import Checkbox from 'src/components/Checkbox';
2622

27-
import Checkbox, {
28-
CheckboxChecked,
29-
CheckboxUnchecked,
30-
} from 'src/components/Checkbox';
23+
jest.mock('src/components/Checkbox/CheckboxIcons', () => ({
24+
CheckboxChecked: () => <div data-test="mock-CheckboxChecked" />,
25+
CheckboxUnchecked: () => <div data-test="mock-CheckboxUnchecked" />,
26+
}));
3127

32-
describe('Checkbox', () => {
33-
let wrapper: ReactWrapper;
34-
35-
it('renders the base component', () => {
36-
expect(
37-
isValidElement(
38-
<Checkbox style={{}} checked={false} onChange={() => true} />,
39-
),
40-
).toBe(true);
41-
});
42-
43-
describe('when unchecked', () => {
44-
it('renders the unchecked component', () => {
45-
const shallowWrapper = shallow(
46-
<Checkbox style={{}} checked={false} onChange={() => true} />,
47-
);
48-
expect(shallowWrapper.dive().find(CheckboxUnchecked)).toExist();
49-
});
50-
});
51-
52-
describe('when checked', () => {
53-
it('renders the checked component', () => {
54-
const shallowWrapper = shallow(
55-
<Checkbox style={{}} checked onChange={() => true} />,
56-
);
57-
expect(shallowWrapper.dive().find(CheckboxChecked)).toExist();
58-
});
59-
});
60-
61-
it('works with an onChange handler', () => {
62-
const mockAction = jest.fn();
63-
wrapper = mount(
64-
<Checkbox style={{}} checked={false} onChange={mockAction} />,
28+
describe('when unchecked', () => {
29+
test('renders the unchecked component', () => {
30+
const { getByTestId } = render(
31+
<Checkbox style={{}} checked={false} onChange={() => true} />,
6532
);
66-
wrapper.find('Checkbox').first().simulate('click');
67-
expect(mockAction).toHaveBeenCalled();
33+
expect(getByTestId('mock-CheckboxUnchecked')).toBeInTheDocument();
6834
});
35+
});
6936

70-
it('renders custom Checkbox styles without melting', () => {
71-
wrapper = mount(
72-
<Checkbox onChange={() => true} checked={false} style={{ opacity: 1 }} />,
37+
describe('when checked', () => {
38+
test('renders the checked component', () => {
39+
const { getByTestId } = render(
40+
<Checkbox style={{}} checked onChange={() => true} />,
7341
);
74-
expect(wrapper.find('Checkbox')).toExist();
75-
expect(wrapper.find('Checkbox')).toHaveStyle({ opacity: 1 });
42+
expect(getByTestId('mock-CheckboxChecked')).toBeInTheDocument();
7643
});
7744
});
45+
46+
test('works with an onChange handler', () => {
47+
const mockAction = jest.fn();
48+
const { getByRole } = render(
49+
<Checkbox style={{}} checked={false} onChange={mockAction} />,
50+
);
51+
fireEvent.click(getByRole('checkbox'));
52+
expect(mockAction).toHaveBeenCalled();
53+
});
54+
55+
test('renders custom Checkbox styles without melting', () => {
56+
const { getByRole } = render(
57+
<Checkbox onChange={() => true} checked={false} style={{ opacity: 1 }} />,
58+
);
59+
expect(getByRole('checkbox')).toBeInTheDocument();
60+
expect(getByRole('checkbox')).toHaveStyle({ opacity: 1 });
61+
});

superset-frontend/src/components/ConfirmStatusChange/ConfirmStatusChange.test.jsx

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,51 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
import { mount } from 'enzyme';
19+
import { fireEvent, render, waitFor } from 'spec/helpers/testing-library';
2020
import Button from 'src/components/Button';
21-
import { act } from 'react-dom/test-utils';
22-
import { supersetTheme, ThemeProvider } from '@superset-ui/core';
2321
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
24-
import Modal from 'src/components/Modal';
25-
26-
describe('ConfirmStatusChange', () => {
27-
const mockedProps = {
28-
title: 'please confirm',
29-
description: 'are you sure?',
30-
onConfirm: jest.fn(),
31-
};
32-
const wrapper = mount(
22+
23+
const mockedProps = {
24+
title: 'please confirm',
25+
description: 'are you sure?',
26+
onConfirm: jest.fn(),
27+
};
28+
29+
test('opens a confirm modal', () => {
30+
const { getByTestId } = render(
3331
<ConfirmStatusChange {...mockedProps}>
3432
{confirm => (
3533
<>
36-
<Button id="btn1" onClick={confirm} />
34+
<Button data-test="btn1" onClick={confirm} />
3735
</>
3836
)}
3937
</ConfirmStatusChange>,
40-
{
41-
wrappingComponent: ThemeProvider,
42-
wrappingComponentProps: { theme: supersetTheme },
43-
},
4438
);
4539

46-
it('opens a confirm modal', () => {
47-
act(() => {
48-
wrapper.find('#btn1').first().props().onClick('foo');
49-
});
40+
fireEvent.click(getByTestId('btn1'));
41+
42+
expect(getByTestId(`${mockedProps.title}-modal`)).toBeInTheDocument();
43+
});
44+
45+
test('calls the function on confirm', async () => {
46+
const { getByTestId, getByRole } = render(
47+
<ConfirmStatusChange {...mockedProps}>
48+
{confirm => (
49+
<>
50+
<Button data-test="btn1" onClick={() => confirm('foo')} />
51+
</>
52+
)}
53+
</ConfirmStatusChange>,
54+
);
5055

51-
wrapper.update();
56+
fireEvent.click(getByTestId('btn1'));
5257

53-
expect(wrapper.find(Modal)).toExist();
54-
});
58+
const confirmInput = getByTestId('delete-modal-input');
59+
fireEvent.change(confirmInput, { target: { value: 'DELETE' } });
5560

56-
it('calls the function on confirm', () => {
57-
act(() => {
58-
wrapper.find(Button).last().props().onClick();
59-
});
61+
const confirmButton = getByRole('button', { name: 'delete' });
62+
fireEvent.click(confirmButton);
6063

61-
expect(mockedProps.onConfirm).toHaveBeenCalledWith('foo');
62-
});
64+
await waitFor(() => expect(mockedProps.onConfirm).toHaveBeenCalledTimes(1));
65+
expect(mockedProps.onConfirm).toHaveBeenCalledWith('foo');
6366
});

0 commit comments

Comments
 (0)