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 { 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';
@ -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();
});

View File

@ -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';
@ -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();

View File

@ -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';
@ -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();
});
@ -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();
});

View File

@ -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';
@ -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', () => {

View File

@ -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 {

View File

@ -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';
@ -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),
// }),
);
});

View File

@ -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';
@ -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();
});

View File

@ -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';
@ -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%' }} />
@ -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);

View File

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