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

* test: migrate some test case to testing-library

* chore: code clean

* test: fix stylelint

* test: test case
This commit is contained in:
MadCcc 2022-04-17 22:36:36 +08:00 committed by GitHub
parent e4a87750ac
commit 1c8e499f9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 105 additions and 83 deletions

View File

@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { render } from '@testing-library/react'; import { fireEvent, render } from '@testing-library/react';
import Avatar from '..'; import Avatar from '..';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
@ -175,8 +175,8 @@ describe('Avatar Render', () => {
it('support onMouseEnter', () => { it('support onMouseEnter', () => {
const onMouseEnter = jest.fn(); const onMouseEnter = jest.fn();
const wrapper = mount(<Avatar onMouseEnter={onMouseEnter}>TestString</Avatar>); const { container } = render(<Avatar onMouseEnter={onMouseEnter}>TestString</Avatar>);
wrapper.simulate('mouseenter'); fireEvent.mouseEnter(container.firstChild);
expect(onMouseEnter).toHaveBeenCalled(); expect(onMouseEnter).toHaveBeenCalled();
}); });

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import { fireEvent, render } from '@testing-library/react';
import Badge from '../index'; import Badge from '../index';
import Tooltip from '../../tooltip'; import Tooltip from '../../tooltip';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
@ -54,14 +55,14 @@ describe('Badge', () => {
// https://github.com/ant-design/ant-design/issues/10626 // https://github.com/ant-design/ant-design/issues/10626
it('should be composable with Tooltip', () => { it('should be composable with Tooltip', () => {
const ref = React.createRef(); const ref = React.createRef();
const wrapper = mount( const { container } = render(
<Tooltip title="Fix the error" ref={ref}> <Tooltip title="Fix the error" ref={ref}>
<Badge status="error" /> <Badge status="error" />
</Tooltip>, </Tooltip>,
); );
act(() => { act(() => {
wrapper.find('Badge').simulate('mouseenter'); fireEvent.mouseEnter(container.querySelector('.ant-badge'));
jest.runAllTimers(); jest.runAllTimers();
}); });
expect(ref.current.props.visible).toBeTruthy(); expect(ref.current.props.visible).toBeTruthy();

View File

@ -1,6 +1,6 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { mount } from 'enzyme'; 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 { act } from 'react-dom/test-utils';
import { SearchOutlined } from '@ant-design/icons'; import { SearchOutlined } from '@ant-design/icons';
import { resetWarned } from 'rc-util/lib/warning'; import { resetWarned } from 'rc-util/lib/warning';
@ -194,12 +194,12 @@ describe('Button', () => {
it('should not clickable when button is loading', () => { it('should not clickable when button is loading', () => {
const onClick = jest.fn(); const onClick = jest.fn();
const wrapper = mount( const { container } = render(
<Button loading onClick={onClick}> <Button loading onClick={onClick}>
button button
</Button>, </Button>,
); );
wrapper.simulate('click'); fireEvent.click(container.firstChild!);
expect(onClick).not.toHaveBeenCalledWith(); expect(onClick).not.toHaveBeenCalledWith();
}); });
@ -313,12 +313,12 @@ describe('Button', () => {
it('should not redirect when button is disabled', () => { it('should not redirect when button is disabled', () => {
const onClick = jest.fn(); const onClick = jest.fn();
const wrapper = mount( const { container } = render(
<Button href="https://ant.design" onClick={onClick} disabled> <Button href="https://ant.design" onClick={onClick} disabled>
click me click me
</Button>, </Button>,
); );
wrapper.simulate('click'); fireEvent.click(container.firstChild!);
expect(onClick).not.toHaveBeenCalled(); expect(onClick).not.toHaveBeenCalled();
}); });

View File

@ -1,6 +1,7 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { SmileOutlined } from '@ant-design/icons'; import { SmileOutlined } from '@ant-design/icons';
import { fireEvent, render } from '@testing-library/react';
import ConfigProvider, { ConfigContext } from '..'; import ConfigProvider, { ConfigContext } from '..';
import Button from '../../button'; import Button from '../../button';
import Table from '../../table'; import Table from '../../table';
@ -74,11 +75,11 @@ describe('ConfigProvider', () => {
); );
}; };
const wrapper = mount(<DynamicPrefixCls />); const { container } = render(<DynamicPrefixCls />);
expect(wrapper.exists('button.bamboo-btn')).toBeTruthy(); expect(container.querySelector('button.bamboo-btn')).toBeTruthy();
wrapper.find('.toggle-button').first().simulate('click'); fireEvent.click(container.querySelector('.toggle-button'));
expect(wrapper.exists('button.light-btn')).toBeTruthy(); expect(container.querySelector('button.light-btn')).toBeTruthy();
}); });
it('iconPrefixCls', () => { it('iconPrefixCls', () => {

View File

@ -665,7 +665,7 @@
// Fix IE11 render bug by css hacks // Fix IE11 render bug by css hacks
// https://github.com/ant-design/ant-design/issues/21559 // https://github.com/ant-design/ant-design/issues/21559
// https://codepen.io/afc163-1472555193/pen/mdJRaNj?editors=0110 // 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, _:-ms-fullscreen,
:root { :root {
.@{picker-prefix-cls}-range-wrapper { .@{picker-prefix-cls}-range-wrapper {

View File

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import Search from '../Search'; import Search from '../Search';
import Button from '../../button'; import Button from '../../button';
import focusTest from '../../../tests/shared/focusTest'; import focusTest from '../../../tests/shared/focusTest';
@ -39,72 +40,84 @@ describe('Input.Search', () => {
it('should disable search icon when disabled prop is true', () => { it('should disable search icon when disabled prop is true', () => {
const onSearch = jest.fn(); const onSearch = jest.fn();
const wrapper = mount(<Search defaultValue="search text" onSearch={onSearch} disabled />); const { container } = render(
wrapper.find('Button').simulate('click'); <Search defaultValue="search text" onSearch={onSearch} disabled />,
);
fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(0); expect(onSearch).toHaveBeenCalledTimes(0);
}); });
it('should trigger onSearch when click search icon', () => { it('should trigger onSearch when click search icon', () => {
const onSearch = jest.fn(); const onSearch = jest.fn();
const wrapper = mount(<Search defaultValue="search text" onSearch={onSearch} />); const { container } = render(<Search defaultValue="search text" onSearch={onSearch} />);
wrapper.find('Button').simulate('click'); fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(1); expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith( expect(onSearch).toHaveBeenCalledWith(
'search text', 'search text',
expect.objectContaining({ expect.anything(),
type: 'click', // FIXME: should use following code
preventDefault: expect.any(Function), // expect.objectContaining({
}), // type: 'click',
// preventDefault: expect.any(Function),
// }),
); );
}); });
it('should trigger onSearch when click search button', () => { it('should trigger onSearch when click search button', () => {
const onSearch = jest.fn(); const onSearch = jest.fn();
const wrapper = mount(<Search defaultValue="search text" enterButton onSearch={onSearch} />); const { container } = render(
wrapper.find('Button').simulate('click'); <Search defaultValue="search text" enterButton onSearch={onSearch} />,
);
fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(1); expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith( expect(onSearch).toHaveBeenCalledWith(
'search text', 'search text',
expect.objectContaining({ expect.anything(),
type: 'click', // FIXME: should use following code
preventDefault: expect.any(Function), // expect.objectContaining({
}), // type: 'click',
// preventDefault: expect.any(Function),
// }),
); );
}); });
it('should trigger onSearch when click search button with text', () => { it('should trigger onSearch when click search button with text', () => {
const onSearch = jest.fn(); const onSearch = jest.fn();
const wrapper = mount( const { container } = render(
<Search defaultValue="search text" enterButton="button text" onSearch={onSearch} />, <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).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith( expect(onSearch).toHaveBeenCalledWith(
'search text', 'search text',
expect.objectContaining({ expect.anything(),
type: 'click', // FIXME: should use following code
preventDefault: expect.any(Function), // expect.objectContaining({
}), // type: 'click',
// preventDefault: expect.any(Function),
// }),
); );
}); });
it('should trigger onSearch when click search button with customize button', () => { it('should trigger onSearch when click search button with customize button', () => {
const onSearch = jest.fn(); const onSearch = jest.fn();
const wrapper = mount( const { container } = render(
<Search <Search
defaultValue="search text" defaultValue="search text"
enterButton={<Button>antd button</Button>} enterButton={<Button>antd button</Button>}
onSearch={onSearch} onSearch={onSearch}
/>, />,
); );
wrapper.find('Button').simulate('click'); fireEvent.click(container.querySelector('button'));
expect(onSearch).toHaveBeenCalledTimes(1); expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith( expect(onSearch).toHaveBeenCalledWith(
'search text', 'search text',
expect.objectContaining({ expect.anything(),
type: 'click', // FIXME: should use following code
preventDefault: expect.any(Function), // expect.objectContaining({
}), // type: 'click',
// preventDefault: expect.any(Function),
// }),
); );
}); });

View File

@ -2,6 +2,7 @@ import React from 'react';
import MockDate from 'mockdate'; import MockDate from 'mockdate';
import moment from 'moment'; import moment from 'moment';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import Statistic from '..'; import Statistic from '..';
import { formatTimeStr } from '../utils'; import { formatTimeStr } from '../utils';
import { sleep } from '../../../tests/utils'; import { sleep } from '../../../tests/utils';
@ -97,10 +98,12 @@ describe('Statistic', () => {
it('responses hover events', () => { it('responses hover events', () => {
const onMouseEnter = jest.fn(); const onMouseEnter = jest.fn();
const onMouseLeave = jest.fn(); const onMouseLeave = jest.fn();
const wrapper = mount(<Statistic onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />); const { container } = render(
wrapper.simulate('mouseenter'); <Statistic onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />,
);
fireEvent.mouseEnter(container.firstChild);
expect(onMouseEnter).toHaveBeenCalled(); expect(onMouseEnter).toHaveBeenCalled();
wrapper.simulate('mouseleave'); fireEvent.mouseLeave(container.firstChild);
expect(onMouseLeave).toHaveBeenCalled(); expect(onMouseLeave).toHaveBeenCalled();
}); });

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { spyElementPrototype } from 'rc-util/lib/test/domHook'; import { spyElementPrototype } from 'rc-util/lib/test/domHook';
import { fireEvent, render } from '@testing-library/react';
import Tooltip from '..'; import Tooltip from '..';
import Button from '../../button'; import Button from '../../button';
import Switch from '../../switch'; import Switch from '../../switch';
@ -224,7 +225,7 @@ describe('Tooltip', () => {
it('should works for input group', async () => { it('should works for input group', async () => {
const onVisibleChange = jest.fn(); const onVisibleChange = jest.fn();
const ref = React.createRef(); const ref = React.createRef();
const wrapper = mount( const { container } = render(
<Tooltip title="hello" onVisibleChange={onVisibleChange} ref={ref}> <Tooltip title="hello" onVisibleChange={onVisibleChange} ref={ref}>
<Group> <Group>
<Input style={{ width: '50%' }} /> <Input style={{ width: '50%' }} />
@ -233,14 +234,14 @@ describe('Tooltip', () => {
</Tooltip>, </Tooltip>,
); );
expect(wrapper.find('Group')).toHaveLength(1); expect(container.getElementsByClassName('ant-input-group')).toHaveLength(1);
const picker = wrapper.find('Group').at(0); const picker = container.getElementsByClassName('ant-input-group')[0];
picker.simulate('mouseenter'); fireEvent.mouseEnter(picker);
await sleep(100); await sleep(100);
expect(onVisibleChange).toHaveBeenCalledWith(true); expect(onVisibleChange).toHaveBeenCalledWith(true);
expect(ref.current.props.visible).toBe(true); expect(ref.current.props.visible).toBe(true);
picker.simulate('mouseleave'); fireEvent.mouseLeave(picker);
await sleep(100); await sleep(100);
expect(onVisibleChange).toHaveBeenCalledWith(false); expect(onVisibleChange).toHaveBeenCalledWith(false);
expect(ref.current.props.visible).toBe(false); expect(ref.current.props.visible).toBe(false);

View File

@ -1,12 +1,11 @@
/* eslint @typescript-eslint/no-use-before-define: "off" */ /* eslint @typescript-eslint/no-use-before-define: "off" */
import React from 'react'; import React, { useState } from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import Transfer from '..'; import Transfer from '..';
import TransferList from '../list'; import TransferList from '../list';
import TransferOperation from '../operation';
import TransferSearch from '../search'; import TransferSearch from '../search';
import TransferItem from '../ListItem'; import TransferItem from '../ListItem';
import Button from '../../button';
import Checkbox from '../../checkbox'; import Checkbox from '../../checkbox';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
@ -103,14 +102,14 @@ describe('Transfer', () => {
it('should move selected keys to corresponding list', () => { it('should move selected keys to corresponding list', () => {
const handleChange = jest.fn(); const handleChange = jest.fn();
const wrapper = mount(<Transfer {...listCommonProps} onChange={handleChange} />); const { container } = render(<Transfer {...listCommonProps} onChange={handleChange} />);
wrapper.find(TransferOperation).find(Button).at(0).simulate('click'); // move selected keys to right list fireEvent.click(container.querySelector('.ant-transfer-operation').querySelector('button')); // move selected keys to right list
expect(handleChange).toHaveBeenCalledWith(['a', 'b'], 'right', ['a']); expect(handleChange).toHaveBeenCalledWith(['a', 'b'], 'right', ['a']);
}); });
it('should move selected keys to left list', () => { it('should move selected keys to left list', () => {
const handleChange = jest.fn(); const handleChange = jest.fn();
const wrapper = mount( const { container } = render(
<Transfer <Transfer
{...listCommonProps} {...listCommonProps}
selectedKeys={['a']} selectedKeys={['a']}
@ -118,14 +117,16 @@ describe('Transfer', () => {
onChange={handleChange} onChange={handleChange}
/>, />,
); );
wrapper.find(TransferOperation).find(Button).at(1).simulate('click'); // move selected keys to left list fireEvent.click(
container.querySelector('.ant-transfer-operation').querySelectorAll('button')[1],
); // move selected keys to left list
expect(handleChange).toHaveBeenCalledWith([], 'left', ['a']); expect(handleChange).toHaveBeenCalledWith([], 'left', ['a']);
}); });
it('should move selected keys expect disabled to corresponding list', () => { it('should move selected keys expect disabled to corresponding list', () => {
const handleChange = jest.fn(); const handleChange = jest.fn();
const wrapper = mount(<Transfer {...listDisabledProps} onChange={handleChange} />); const { container } = render(<Transfer {...listDisabledProps} onChange={handleChange} />);
wrapper.find(TransferOperation).find(Button).at(0).simulate('click'); // move selected keys to right list fireEvent.click(container.querySelector('.ant-transfer-operation').querySelector('button')); // move selected keys to right list
expect(handleChange).toHaveBeenCalledWith(['b'], 'right', ['b']); expect(handleChange).toHaveBeenCalledWith(['b'], 'right', ['b']);
}); });
@ -327,12 +328,14 @@ describe('Transfer', () => {
const filterOption = (inputValue, option) => option.description.indexOf(inputValue) > -1; const filterOption = (inputValue, option) => option.description.indexOf(inputValue) > -1;
const renderFunc = item => item.title; const renderFunc = item => item.title;
const handleChange = jest.fn(); const handleChange = jest.fn();
const TransferDemo = () => {
const [selectedKeys, setSelectedKeys] = useState(searchTransferProps.selectedKeys);
const handleSelectChange = (sourceSelectedKeys, targetSelectedKeys) => { const handleSelectChange = (sourceSelectedKeys, targetSelectedKeys) => {
wrapper.setProps({ setSelectedKeys([...sourceSelectedKeys, ...targetSelectedKeys]);
selectedKeys: [...sourceSelectedKeys, ...targetSelectedKeys],
});
}; };
const wrapper = mount(
return (
<Transfer <Transfer
{...searchTransferProps} {...searchTransferProps}
showSearch showSearch
@ -340,20 +343,20 @@ describe('Transfer', () => {
render={renderFunc} render={renderFunc}
onSelectChange={handleSelectChange} onSelectChange={handleSelectChange}
onChange={handleChange} onChange={handleChange}
/>, selectedKeys={selectedKeys}
/>
); );
wrapper };
.find(TransferSearch) const { container } = render(<TransferDemo />);
.at(0) fireEvent.change(container.querySelector('.ant-transfer-list-search').querySelector('input'), {
.find('input') target: { value: 'content2' },
.simulate('change', { target: { value: 'content2' } }); });
wrapper fireEvent.click(
.find(TransferList) container
.at(0) .querySelector('.ant-transfer-list')
.find('.ant-transfer-list-header input[type="checkbox"]') .querySelector('.ant-transfer-list-header input[type="checkbox"]'),
.filterWhere(n => !n.prop('checked')) );
.simulate('change'); fireEvent.click(container.querySelector('.ant-transfer-operation').querySelector('button'));
wrapper.find(TransferOperation).find(Button).at(0).simulate('click');
expect(handleChange).toHaveBeenCalledWith(['1', '3', '4'], 'right', ['1']); expect(handleChange).toHaveBeenCalledWith(['1', '3', '4'], 'right', ['1']);
}); });