Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate some test case to testing-library #35062

Merged
merged 4 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/avatar/__tests__/Avatar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';
import { mount } from 'enzyme';
import { render } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';
import Avatar from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
Expand Down Expand Up @@ -175,8 +175,8 @@ describe('Avatar Render', () => {

it('support onMouseEnter', () => {
const onMouseEnter = jest.fn();
const wrapper = mount(<Avatar onMouseEnter={onMouseEnter}>TestString</Avatar>);
wrapper.simulate('mouseenter');
const { container } = render(<Avatar onMouseEnter={onMouseEnter}>TestString</Avatar>);
fireEvent.mouseEnter(container.firstChild);
expect(onMouseEnter).toHaveBeenCalled();
});

Expand Down
5 changes: 3 additions & 2 deletions components/badge/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';
import { fireEvent, render } from '@testing-library/react';
import Badge from '../index';
import Tooltip from '../../tooltip';
import mountTest from '../../../tests/shared/mountTest';
Expand Down Expand Up @@ -54,14 +55,14 @@ describe('Badge', () => {
// https://github.com/ant-design/ant-design/issues/10626
it('should be composable with Tooltip', () => {
const ref = React.createRef();
const wrapper = mount(
const { container } = render(
<Tooltip title="Fix the error" ref={ref}>
<Badge status="error" />
</Tooltip>,
);

act(() => {
wrapper.find('Badge').simulate('mouseenter');
fireEvent.mouseEnter(container.querySelector('.ant-badge'));
jest.runAllTimers();
});
expect(ref.current.props.visible).toBeTruthy();
Expand Down
10 changes: 5 additions & 5 deletions components/button/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react';
import { mount } from 'enzyme';
import { render } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import { SearchOutlined } from '@ant-design/icons';
import { resetWarned } from 'rc-util/lib/warning';
Expand Down Expand Up @@ -194,12 +194,12 @@ describe('Button', () => {

it('should not clickable when button is loading', () => {
const onClick = jest.fn();
const wrapper = mount(
const { container } = render(
<Button loading onClick={onClick}>
button
</Button>,
);
wrapper.simulate('click');
fireEvent.click(container.firstChild!);
expect(onClick).not.toHaveBeenCalledWith();
});

Expand Down Expand Up @@ -313,12 +313,12 @@ describe('Button', () => {

it('should not redirect when button is disabled', () => {
const onClick = jest.fn();
const wrapper = mount(
const { container } = render(
<Button href="https://ant.design" onClick={onClick} disabled>
click me
</Button>,
);
wrapper.simulate('click');
fireEvent.click(container.firstChild!);
expect(onClick).not.toHaveBeenCalled();
});

Expand Down
9 changes: 5 additions & 4 deletions components/config-provider/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from 'react';
import { mount } from 'enzyme';
import { SmileOutlined } from '@ant-design/icons';
import { fireEvent, render } from '@testing-library/react';
import ConfigProvider, { ConfigContext } from '..';
import Button from '../../button';
import Table from '../../table';
Expand Down Expand Up @@ -74,11 +75,11 @@ describe('ConfigProvider', () => {
);
};

const wrapper = mount(<DynamicPrefixCls />);
const { container } = render(<DynamicPrefixCls />);

expect(wrapper.exists('button.bamboo-btn')).toBeTruthy();
wrapper.find('.toggle-button').first().simulate('click');
expect(wrapper.exists('button.light-btn')).toBeTruthy();
expect(container.querySelector('button.bamboo-btn')).toBeTruthy();
fireEvent.click(container.querySelector('.toggle-button'));
expect(container.querySelector('button.light-btn')).toBeTruthy();
});

it('iconPrefixCls', () => {
Expand Down
2 changes: 1 addition & 1 deletion components/date-picker/style/panel.less
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@
// Fix IE11 render bug by css hacks
// https://github.com/ant-design/ant-design/issues/21559
// https://codepen.io/afc163-1472555193/pen/mdJRaNj?editors=0110
/* stylelint-disable-next-line selector-type-no-unknown,selector-no-vendor-prefix */
/* stylelint-disable selector-type-no-unknown,selector-no-vendor-prefix */
_:-ms-fullscreen,
:root {
.@{picker-prefix-cls}-range-wrapper {
Expand Down
65 changes: 39 additions & 26 deletions components/input/__tests__/Search.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import Search from '../Search';
import Button from '../../button';
import focusTest from '../../../tests/shared/focusTest';
Expand Down Expand Up @@ -39,72 +40,84 @@ describe('Input.Search', () => {

it('should disable search icon when disabled prop is true', () => {
const onSearch = jest.fn();
const wrapper = mount(<Search defaultValue="search text" onSearch={onSearch} disabled />);
wrapper.find('Button').simulate('click');
const { container } = render(
<Search defaultValue="search text" onSearch={onSearch} disabled />,
);
fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(0);
});

it('should trigger onSearch when click search icon', () => {
const onSearch = jest.fn();
const wrapper = mount(<Search defaultValue="search text" onSearch={onSearch} />);
wrapper.find('Button').simulate('click');
const { container } = render(<Search defaultValue="search text" onSearch={onSearch} />);
fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith(
'search text',
expect.objectContaining({
type: 'click',
preventDefault: expect.any(Function),
}),
expect.anything(),
// FIXME: should use following code
// expect.objectContaining({
// type: 'click',
// preventDefault: expect.any(Function),
// }),
);
});

it('should trigger onSearch when click search button', () => {
const onSearch = jest.fn();
const wrapper = mount(<Search defaultValue="search text" enterButton onSearch={onSearch} />);
wrapper.find('Button').simulate('click');
const { container } = render(
<Search defaultValue="search text" enterButton onSearch={onSearch} />,
);
fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith(
'search text',
expect.objectContaining({
type: 'click',
preventDefault: expect.any(Function),
}),
expect.anything(),
// FIXME: should use following code
// expect.objectContaining({
// type: 'click',
// preventDefault: expect.any(Function),
// }),
);
});

it('should trigger onSearch when click search button with text', () => {
const onSearch = jest.fn();
const wrapper = mount(
const { container } = render(
<Search defaultValue="search text" enterButton="button text" onSearch={onSearch} />,
);
wrapper.find('Button').simulate('click');
fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith(
'search text',
expect.objectContaining({
type: 'click',
preventDefault: expect.any(Function),
}),
expect.anything(),
// FIXME: should use following code
// expect.objectContaining({
// type: 'click',
// preventDefault: expect.any(Function),
// }),
);
});

it('should trigger onSearch when click search button with customize button', () => {
const onSearch = jest.fn();
const wrapper = mount(
const { container } = render(
<Search
defaultValue="search text"
enterButton={<Button>antd button</Button>}
onSearch={onSearch}
/>,
);
wrapper.find('Button').simulate('click');
fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith(
'search text',
expect.objectContaining({
type: 'click',
preventDefault: expect.any(Function),
}),
expect.anything(),
// FIXME: should use following code
// expect.objectContaining({
// type: 'click',
// preventDefault: expect.any(Function),
// }),
);
});

Expand Down
9 changes: 6 additions & 3 deletions components/statistic/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import MockDate from 'mockdate';
import moment from 'moment';
import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import Statistic from '..';
import { formatTimeStr } from '../utils';
import { sleep } from '../../../tests/utils';
Expand Down Expand Up @@ -97,10 +98,12 @@ describe('Statistic', () => {
it('responses hover events', () => {
const onMouseEnter = jest.fn();
const onMouseLeave = jest.fn();
const wrapper = mount(<Statistic onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />);
wrapper.simulate('mouseenter');
const { container } = render(
<Statistic onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />,
);
fireEvent.mouseEnter(container.firstChild);
expect(onMouseEnter).toHaveBeenCalled();
wrapper.simulate('mouseleave');
fireEvent.mouseLeave(container.firstChild);
expect(onMouseLeave).toHaveBeenCalled();
});

Expand Down
11 changes: 6 additions & 5 deletions components/tooltip/__tests__/tooltip.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
import { fireEvent, render } from '@testing-library/react';
import Tooltip from '..';
import Button from '../../button';
import Switch from '../../switch';
Expand Down Expand Up @@ -224,7 +225,7 @@ describe('Tooltip', () => {
it('should works for input group', async () => {
const onVisibleChange = jest.fn();
const ref = React.createRef();
const wrapper = mount(
const { container } = render(
<Tooltip title="hello" onVisibleChange={onVisibleChange} ref={ref}>
<Group>
<Input style={{ width: '50%' }} />
Expand All @@ -233,14 +234,14 @@ describe('Tooltip', () => {
</Tooltip>,
);

expect(wrapper.find('Group')).toHaveLength(1);
const picker = wrapper.find('Group').at(0);
picker.simulate('mouseenter');
expect(container.getElementsByClassName('ant-input-group')).toHaveLength(1);
const picker = container.getElementsByClassName('ant-input-group')[0];
fireEvent.mouseEnter(picker);
await sleep(100);
expect(onVisibleChange).toHaveBeenCalledWith(true);
expect(ref.current.props.visible).toBe(true);

picker.simulate('mouseleave');
fireEvent.mouseLeave(picker);
await sleep(100);
expect(onVisibleChange).toHaveBeenCalledWith(false);
expect(ref.current.props.visible).toBe(false);
Expand Down