2017-03-28 15:50:46 +08:00
|
|
|
import React, { Component } from 'react';
|
2020-04-06 12:15:28 +08:00
|
|
|
import { mount, render } from 'enzyme';
|
2020-06-02 12:54:22 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2019-11-28 12:34:33 +08:00
|
|
|
import { SearchOutlined } from '@ant-design/icons';
|
2020-09-02 18:30:27 +08:00
|
|
|
import { resetWarned } from 'rc-util/lib/warning';
|
2016-12-14 15:44:38 +08:00
|
|
|
import Button from '..';
|
2020-02-02 18:03:38 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2019-09-16 11:23:26 +08:00
|
|
|
import { sleep } from '../../../tests/utils';
|
2020-04-13 13:36:23 +08:00
|
|
|
import { SizeType } from '../../config-provider/SizeContext';
|
2016-12-14 15:44:38 +08:00
|
|
|
|
|
|
|
describe('Button', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Button);
|
2019-09-16 11:23:26 +08:00
|
|
|
mountTest(() => <Button size="large" />);
|
|
|
|
mountTest(() => <Button size="small" />);
|
2019-08-27 16:13:43 +08:00
|
|
|
mountTest(Button.Group);
|
2019-09-16 11:23:26 +08:00
|
|
|
mountTest(() => <Button.Group size="large" />);
|
|
|
|
mountTest(() => <Button.Group size="small" />);
|
2020-04-05 21:18:28 +08:00
|
|
|
mountTest(() => <Button.Group size="middle" />);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Button);
|
|
|
|
rtlTest(() => <Button size="large" />);
|
|
|
|
rtlTest(() => <Button size="small" />);
|
|
|
|
rtlTest(Button.Group);
|
|
|
|
rtlTest(() => <Button.Group size="large" />);
|
|
|
|
rtlTest(() => <Button.Group size="small" />);
|
2020-04-05 21:18:28 +08:00
|
|
|
rtlTest(() => <Button.Group size="middle" />);
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2016-12-14 15:44:38 +08:00
|
|
|
it('renders correctly', () => {
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(<Button>Follow</Button>).toMatchRenderedSnapshot();
|
2016-12-14 15:44:38 +08:00
|
|
|
});
|
|
|
|
|
2018-08-24 12:11:44 +08:00
|
|
|
it('mount correctly', () => {
|
2020-04-13 13:36:23 +08:00
|
|
|
expect(() => mount(<Button>Follow</Button>)).not.toThrow();
|
2018-08-24 12:11:44 +08:00
|
|
|
});
|
|
|
|
|
2020-04-05 21:18:28 +08:00
|
|
|
it('warns if size is wrong', () => {
|
|
|
|
const mockWarn = jest.fn();
|
|
|
|
jest.spyOn(console, 'warn').mockImplementation(mockWarn);
|
2021-06-11 16:32:46 +08:00
|
|
|
const size = 'who am I' as any as SizeType;
|
2020-04-13 13:36:23 +08:00
|
|
|
render(<Button.Group size={size} />);
|
2020-04-05 21:18:28 +08:00
|
|
|
expect(mockWarn).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockWarn.mock.calls[0][0]).toMatchObject({
|
|
|
|
message: 'unreachable case: "who am I"',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-12-14 15:44:38 +08:00
|
|
|
it('renders Chinese characters correctly', () => {
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(<Button>按钮</Button>).toMatchRenderedSnapshot();
|
2017-05-02 20:16:18 +08:00
|
|
|
// should not insert space when there is icon
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(<Button icon={<SearchOutlined />}>按钮</Button>).toMatchRenderedSnapshot();
|
2017-05-02 20:16:18 +08:00
|
|
|
// should not insert space when there is icon
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Button>
|
2019-11-28 12:34:33 +08:00
|
|
|
<SearchOutlined />
|
2018-12-07 16:17:45 +08:00
|
|
|
按钮
|
|
|
|
</Button>,
|
2020-04-06 12:05:38 +08:00
|
|
|
).toMatchRenderedSnapshot();
|
2018-05-01 19:36:11 +08:00
|
|
|
// should not insert space when there is icon
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(<Button icon={<SearchOutlined />}>按钮</Button>).toMatchRenderedSnapshot();
|
2018-05-01 19:36:11 +08:00
|
|
|
// should not insert space when there is icon while loading
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(
|
2019-11-28 12:34:33 +08:00
|
|
|
<Button icon={<SearchOutlined />} loading>
|
2018-12-07 16:17:45 +08:00
|
|
|
按钮
|
|
|
|
</Button>,
|
2020-04-06 12:05:38 +08:00
|
|
|
).toMatchRenderedSnapshot();
|
2018-05-01 19:36:11 +08:00
|
|
|
// should insert space while loading
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(<Button loading>按钮</Button>).toMatchRenderedSnapshot();
|
2019-09-16 11:23:26 +08:00
|
|
|
|
|
|
|
// should insert space while only one nested element
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(
|
2019-09-16 11:23:26 +08:00
|
|
|
<Button>
|
|
|
|
<span>按钮</span>
|
|
|
|
</Button>,
|
2020-04-06 12:05:38 +08:00
|
|
|
).toMatchRenderedSnapshot();
|
2016-12-14 15:44:38 +08:00
|
|
|
});
|
2017-02-16 14:03:05 +08:00
|
|
|
|
2018-03-23 22:23:03 +08:00
|
|
|
it('renders Chinese characters correctly in HOC', () => {
|
2020-04-13 13:36:23 +08:00
|
|
|
const Text = ({ children }: { children: React.ReactNode }) => <span>{children}</span>;
|
2018-03-23 22:23:03 +08:00
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Button>
|
|
|
|
<Text>按钮</Text>
|
|
|
|
</Button>,
|
2018-03-23 22:23:03 +08:00
|
|
|
);
|
|
|
|
expect(wrapper.find('.ant-btn').hasClass('ant-btn-two-chinese-chars')).toBe(true);
|
|
|
|
wrapper.setProps({
|
|
|
|
children: <Text>大按钮</Text>,
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-btn').hasClass('ant-btn-two-chinese-chars')).toBe(false);
|
|
|
|
wrapper.setProps({
|
|
|
|
children: <Text>按钮</Text>,
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-btn').hasClass('ant-btn-two-chinese-chars')).toBe(true);
|
|
|
|
});
|
|
|
|
|
2019-09-16 11:23:26 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/18118
|
2020-05-06 10:15:33 +08:00
|
|
|
it('should not insert space to link or text button', () => {
|
|
|
|
const wrapper1 = mount(<Button type="link">按钮</Button>);
|
|
|
|
expect(wrapper1.text()).toBe('按钮');
|
|
|
|
const wrapper2 = mount(<Button type="text">按钮</Button>);
|
|
|
|
expect(wrapper2.text()).toBe('按钮');
|
2019-09-16 11:23:26 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render empty button without errors', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Button>
|
|
|
|
{null}
|
|
|
|
{undefined}
|
|
|
|
</Button>,
|
|
|
|
);
|
2020-09-02 18:30:27 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2019-09-16 11:23:26 +08:00
|
|
|
});
|
|
|
|
|
2018-06-12 14:36:08 +08:00
|
|
|
it('have static property for type detecting', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Button>Button Text</Button>);
|
2020-04-13 13:36:23 +08:00
|
|
|
expect((wrapper.type() as any).__ANT_BUTTON).toBe(true);
|
2017-02-16 14:03:05 +08:00
|
|
|
});
|
2017-03-28 15:50:46 +08:00
|
|
|
|
|
|
|
it('should change loading state instantly by default', () => {
|
|
|
|
class DefaultButton extends Component {
|
|
|
|
state = {
|
|
|
|
loading: false,
|
|
|
|
};
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2017-03-28 15:50:46 +08:00
|
|
|
enterLoading = () => {
|
|
|
|
this.setState({ loading: true });
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2017-03-28 15:50:46 +08:00
|
|
|
render() {
|
2018-06-22 21:05:13 +08:00
|
|
|
const { loading } = this.state;
|
2018-12-07 16:17:45 +08:00
|
|
|
return (
|
|
|
|
<Button loading={loading} onClick={this.enterLoading}>
|
|
|
|
Button
|
|
|
|
</Button>
|
|
|
|
);
|
2017-03-28 15:50:46 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<DefaultButton />);
|
2017-03-28 15:50:46 +08:00
|
|
|
wrapper.simulate('click');
|
2017-09-20 16:26:18 +08:00
|
|
|
expect(wrapper.find('.ant-btn-loading').length).toBe(1);
|
2017-03-28 15:50:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should change loading state with delay', () => {
|
|
|
|
class DefaultButton extends Component {
|
|
|
|
state = {
|
|
|
|
loading: false,
|
|
|
|
};
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2017-03-28 15:50:46 +08:00
|
|
|
enterLoading = () => {
|
|
|
|
this.setState({ loading: { delay: 1000 } });
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2017-03-28 15:50:46 +08:00
|
|
|
render() {
|
2018-06-22 21:05:13 +08:00
|
|
|
const { loading } = this.state;
|
2018-12-07 16:17:45 +08:00
|
|
|
return (
|
|
|
|
<Button loading={loading} onClick={this.enterLoading}>
|
|
|
|
Button
|
|
|
|
</Button>
|
|
|
|
);
|
2017-03-28 15:50:46 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<DefaultButton />);
|
2017-03-28 15:50:46 +08:00
|
|
|
wrapper.simulate('click');
|
|
|
|
expect(wrapper.hasClass('ant-btn-loading')).toBe(false);
|
|
|
|
});
|
2017-11-29 11:20:22 +08:00
|
|
|
|
2020-06-02 12:54:22 +08:00
|
|
|
it('reset when loading back of delay', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const wrapper = mount(<Button loading={{ delay: 1000 }} />);
|
|
|
|
wrapper.update();
|
2020-06-03 21:59:54 +08:00
|
|
|
wrapper.setProps({ loading: { delay: 2000 } });
|
|
|
|
wrapper.update();
|
2020-06-02 12:54:22 +08:00
|
|
|
wrapper.setProps({ loading: false });
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find('.ant-btn-loading')).toHaveLength(0);
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2019-09-16 11:23:26 +08:00
|
|
|
it('should not clickable when button is loading', () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Button loading onClick={onClick}>
|
|
|
|
button
|
|
|
|
</Button>,
|
|
|
|
);
|
|
|
|
wrapper.simulate('click');
|
|
|
|
expect(onClick).not.toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
|
2017-11-29 11:20:22 +08:00
|
|
|
it('should support link button', () => {
|
|
|
|
const wrapper = mount(
|
2020-05-15 17:18:55 +08:00
|
|
|
<Button target="_blank" href="https://ant.design">
|
2018-12-07 16:17:45 +08:00
|
|
|
link button
|
|
|
|
</Button>,
|
2017-11-29 11:20:22 +08:00
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2018-01-09 16:18:20 +08:00
|
|
|
|
|
|
|
it('fixbug renders {0} , 0 and {false}', () => {
|
2020-04-06 12:05:38 +08:00
|
|
|
expect(<Button>{0}</Button>).toMatchRenderedSnapshot();
|
|
|
|
expect(<Button>0</Button>).toMatchRenderedSnapshot();
|
|
|
|
expect(<Button>{false}</Button>).toMatchRenderedSnapshot();
|
2018-01-09 16:18:20 +08:00
|
|
|
});
|
2018-11-12 12:38:02 +08:00
|
|
|
|
2018-12-03 10:39:00 +08:00
|
|
|
it('should not render as link button when href is undefined', async () => {
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Button type="primary" href={undefined}>
|
|
|
|
button
|
|
|
|
</Button>,
|
2018-12-03 10:39:00 +08:00
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2019-05-17 12:04:47 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/15342
|
|
|
|
it('should merge text if children using variable', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Button>
|
2019-10-05 17:35:49 +08:00
|
|
|
{/* eslint-disable-next-line react/jsx-curly-brace-presence */}
|
2019-05-17 12:04:47 +08:00
|
|
|
This {'is'} a test {1}
|
|
|
|
</Button>,
|
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2019-09-16 11:23:26 +08:00
|
|
|
|
|
|
|
it('should support to change loading', async () => {
|
|
|
|
const wrapper = mount(<Button>Button</Button>);
|
|
|
|
wrapper.setProps({ loading: true });
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-btn-loading').length).toBe(1);
|
|
|
|
wrapper.setProps({ loading: false });
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-btn-loading').length).toBe(0);
|
|
|
|
wrapper.setProps({ loading: { delay: 50 } });
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-btn-loading').length).toBe(0);
|
|
|
|
await sleep(50);
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-btn-loading').length).toBe(1);
|
|
|
|
wrapper.setProps({ loading: false });
|
|
|
|
await sleep(50);
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-btn-loading').length).toBe(0);
|
|
|
|
expect(() => {
|
|
|
|
wrapper.unmount();
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2019-12-13 10:18:12 +08:00
|
|
|
|
|
|
|
it('should warning when pass a string as icon props', () => {
|
2020-09-02 18:30:27 +08:00
|
|
|
resetWarned();
|
2019-12-13 10:18:12 +08:00
|
|
|
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
mount(<Button type="primary" icon="ab" />);
|
|
|
|
expect(warnSpy).not.toHaveBeenCalled();
|
|
|
|
mount(<Button type="primary" icon="search" />);
|
|
|
|
expect(warnSpy).toHaveBeenCalledWith(
|
|
|
|
`Warning: [antd: Button] \`icon\` is using ReactNode instead of string naming in v4. Please check \`search\` at https://ant.design/components/icon`,
|
|
|
|
);
|
|
|
|
warnSpy.mockRestore();
|
|
|
|
});
|
2020-02-02 18:03:38 +08:00
|
|
|
|
2020-06-19 17:46:15 +08:00
|
|
|
it('should warning when pass type=link and ghost=true', () => {
|
2020-09-02 18:30:27 +08:00
|
|
|
resetWarned();
|
2020-06-19 17:46:15 +08:00
|
|
|
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
mount(<Button type="link" ghost />);
|
|
|
|
expect(warnSpy).toHaveBeenCalledWith(
|
|
|
|
"Warning: [antd: Button] `link` or `text` button can't be a `ghost` button.",
|
|
|
|
);
|
|
|
|
warnSpy.mockRestore();
|
2020-09-02 18:30:27 +08:00
|
|
|
});
|
|
|
|
|
2020-06-19 17:46:15 +08:00
|
|
|
it('should warning when pass type=text and ghost=true', () => {
|
2020-09-02 18:30:27 +08:00
|
|
|
resetWarned();
|
2020-06-19 17:46:15 +08:00
|
|
|
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
mount(<Button type="text" ghost />);
|
|
|
|
expect(warnSpy).toHaveBeenCalledWith(
|
|
|
|
"Warning: [antd: Button] `link` or `text` button can't be a `ghost` button.",
|
|
|
|
);
|
|
|
|
warnSpy.mockRestore();
|
2020-09-02 18:30:27 +08:00
|
|
|
});
|
2020-06-19 17:46:15 +08:00
|
|
|
|
2020-02-02 18:03:38 +08:00
|
|
|
it('skip check 2 words when ConfigProvider disable this', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<ConfigProvider autoInsertSpaceInButton={false}>
|
|
|
|
<Button>test</Button>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const btn = wrapper.find('button').instance();
|
|
|
|
Object.defineProperty(btn, 'textContent', {
|
|
|
|
get() {
|
|
|
|
throw new Error('Should not called!!!');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2021-04-19 17:19:22 +08:00
|
|
|
|
|
|
|
it('should not redirect when button is disabled', () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Button href="https://ant.design" onClick={onClick} disabled>
|
|
|
|
click me
|
|
|
|
</Button>,
|
|
|
|
);
|
|
|
|
wrapper.simulate('click');
|
|
|
|
expect(onClick).not.toHaveBeenCalled();
|
|
|
|
});
|
2021-06-11 16:32:46 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/30953
|
|
|
|
it('should handle fragment as children', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Button>
|
|
|
|
<>text</>
|
|
|
|
</Button>,
|
|
|
|
);
|
|
|
|
expect(wrapper).toMatchRenderedSnapshot();
|
|
|
|
});
|
2016-12-14 15:44:38 +08:00
|
|
|
});
|