fix test case of Button loading

This commit is contained in:
afc163 2019-09-07 20:56:51 +08:00 committed by 偏右
parent 4338a5b7cf
commit 1903ae0c13
3 changed files with 32 additions and 89 deletions

View File

@ -283,66 +283,3 @@ exports[`Button should support link button 1`] = `
</span> </span>
</a> </a>
`; `;
exports[`Button should support to change loading 1`] = `
<button
class="ant-btn ant-btn-loading"
type="button"
>
<i
aria-label="icon: loading"
class="anticon anticon-loading"
>
<svg
aria-hidden="true"
class="anticon-spin"
data-icon="loading"
fill="currentColor"
focusable="false"
height="1em"
viewBox="0 0 1024 1024"
width="1em"
>
<path
d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 0 0-94.3-139.9 437.71 437.71 0 0 0-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"
/>
</svg>
</i>
<span>
Button
</span>
</button>
`;
exports[`Button should support to change loading 2`] = `
<button
class="ant-btn"
type="button"
>
<span>
Button
</span>
</button>
`;
exports[`Button should support to change loading 3`] = `
<button
class="ant-btn"
type="button"
>
<span>
Button
</span>
</button>
`;
exports[`Button should support to change loading 4`] = `
<button
class="ant-btn"
type="button"
>
<span>
Button
</span>
</button>
`;

View File

@ -4,6 +4,7 @@ import renderer from 'react-test-renderer';
import Button from '..'; import Button from '..';
import Icon from '../../icon'; import Icon from '../../icon';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
import { sleep } from '../../../tests/utils';
describe('Button', () => { describe('Button', () => {
mountTest(Button); mountTest(Button);
@ -54,7 +55,11 @@ describe('Button', () => {
expect(wrapper5).toMatchSnapshot(); expect(wrapper5).toMatchSnapshot();
// should insert space while only one nested element // should insert space while only one nested element
const wrapper6 = render(<Button><span>按钮</span></Button>); const wrapper6 = render(
<Button>
<span>按钮</span>
</Button>,
);
expect(wrapper6).toMatchSnapshot(); expect(wrapper6).toMatchSnapshot();
}); });
@ -85,7 +90,12 @@ describe('Button', () => {
}); });
it('should render empty button without errors', () => { it('should render empty button without errors', () => {
const wrapper = mount(<Button />); const wrapper = mount(
<Button>
{null}
{undefined}
</Button>,
);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
@ -203,20 +213,24 @@ describe('Button', () => {
expect(wrapper.render()).toMatchSnapshot(); expect(wrapper.render()).toMatchSnapshot();
}); });
it('should support to change loading', () => { it('should support to change loading', async () => {
const wrapper = mount( const wrapper = mount(<Button>Button</Button>);
<Button>
Button
</Button>,
);
wrapper.setProps({ loading: true }); wrapper.setProps({ loading: true });
expect(wrapper.render()).toMatchSnapshot(); wrapper.update();
expect(wrapper.find('.ant-btn-loading').length).toBe(1);
wrapper.setProps({ loading: false }); wrapper.setProps({ loading: false });
expect(wrapper.render()).toMatchSnapshot(); wrapper.update();
wrapper.setProps({ loading: { delay: 200 } }); expect(wrapper.find('.ant-btn-loading').length).toBe(0);
expect(wrapper.render()).toMatchSnapshot(); 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 }); wrapper.setProps({ loading: false });
expect(wrapper.render()).toMatchSnapshot(); await sleep(50);
wrapper.update();
expect(wrapper.find('.ant-btn-loading').length).toBe(0);
expect(() => { expect(() => {
wrapper.unmount(); wrapper.unmount();
}).not.toThrow(); }).not.toThrow();

View File

@ -134,16 +134,6 @@ class Button extends React.Component<ButtonProps, ButtonState> {
title: PropTypes.string, title: PropTypes.string,
}; };
static getDerivedStateFromProps(nextProps: ButtonProps, prevState: ButtonState) {
if (nextProps.loading instanceof Boolean) {
return {
...prevState,
loading: nextProps.loading,
};
}
return null;
}
private delayTimeout: number; private delayTimeout: number;
private buttonNode: HTMLElement | null; private buttonNode: HTMLElement | null;
@ -169,8 +159,10 @@ class Button extends React.Component<ButtonProps, ButtonState> {
const { loading } = this.props; const { loading } = this.props;
if (loading && typeof loading !== 'boolean' && loading.delay) { if (loading && typeof loading !== 'boolean' && loading.delay) {
this.delayTimeout = window.setTimeout(() => this.setState({ loading }), loading.delay); this.delayTimeout = window.setTimeout(() => {
} else if (prevProps.loading !== this.props.loading) { this.setState({ loading });
}, loading.delay);
} else if (prevProps.loading !== loading) {
// eslint-disable-next-line react/no-did-update-set-state // eslint-disable-next-line react/no-did-update-set-state
this.setState({ loading }); this.setState({ loading });
} }
@ -260,7 +252,7 @@ class Button extends React.Component<ButtonProps, ButtonState> {
[`${prefixCls}-${shape}`]: shape, [`${prefixCls}-${shape}`]: shape,
[`${prefixCls}-${sizeCls}`]: sizeCls, [`${prefixCls}-${sizeCls}`]: sizeCls,
[`${prefixCls}-icon-only`]: !children && children !== 0 && iconType, [`${prefixCls}-icon-only`]: !children && children !== 0 && iconType,
[`${prefixCls}-loading`]: loading, [`${prefixCls}-loading`]: !!loading,
[`${prefixCls}-background-ghost`]: ghost, [`${prefixCls}-background-ghost`]: ghost,
[`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && autoInsertSpace, [`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && autoInsertSpace,
[`${prefixCls}-block`]: block, [`${prefixCls}-block`]: block,