From 1903ae0c1348bedc13e522cdf2e46f0caa9f574e Mon Sep 17 00:00:00 2001 From: afc163 Date: Sat, 7 Sep 2019 20:56:51 +0800 Subject: [PATCH] :white_check_mark: fix test case of Button loading --- .../__snapshots__/index.test.js.snap | 63 ------------------- components/button/__tests__/index.test.js | 40 ++++++++---- components/button/button.tsx | 18 ++---- 3 files changed, 32 insertions(+), 89 deletions(-) diff --git a/components/button/__tests__/__snapshots__/index.test.js.snap b/components/button/__tests__/__snapshots__/index.test.js.snap index 82434c952a..82864ee0af 100644 --- a/components/button/__tests__/__snapshots__/index.test.js.snap +++ b/components/button/__tests__/__snapshots__/index.test.js.snap @@ -283,66 +283,3 @@ exports[`Button should support link button 1`] = ` `; - -exports[`Button should support to change loading 1`] = ` - -`; - -exports[`Button should support to change loading 2`] = ` - -`; - -exports[`Button should support to change loading 3`] = ` - -`; - -exports[`Button should support to change loading 4`] = ` - -`; diff --git a/components/button/__tests__/index.test.js b/components/button/__tests__/index.test.js index 81dddb0f14..a5ef8080c2 100644 --- a/components/button/__tests__/index.test.js +++ b/components/button/__tests__/index.test.js @@ -4,6 +4,7 @@ import renderer from 'react-test-renderer'; import Button from '..'; import Icon from '../../icon'; import mountTest from '../../../tests/shared/mountTest'; +import { sleep } from '../../../tests/utils'; describe('Button', () => { mountTest(Button); @@ -54,7 +55,11 @@ describe('Button', () => { expect(wrapper5).toMatchSnapshot(); // should insert space while only one nested element - const wrapper6 = render(); + const wrapper6 = render( + , + ); expect(wrapper6).toMatchSnapshot(); }); @@ -85,7 +90,12 @@ describe('Button', () => { }); it('should render empty button without errors', () => { - const wrapper = mount(, + ); expect(wrapper).toMatchSnapshot(); }); @@ -203,20 +213,24 @@ describe('Button', () => { expect(wrapper.render()).toMatchSnapshot(); }); - it('should support to change loading', () => { - const wrapper = mount( - , - ); + it('should support to change loading', async () => { + const wrapper = mount(); wrapper.setProps({ loading: true }); - expect(wrapper.render()).toMatchSnapshot(); + wrapper.update(); + expect(wrapper.find('.ant-btn-loading').length).toBe(1); wrapper.setProps({ loading: false }); - expect(wrapper.render()).toMatchSnapshot(); - wrapper.setProps({ loading: { delay: 200 } }); - expect(wrapper.render()).toMatchSnapshot(); + 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 }); - expect(wrapper.render()).toMatchSnapshot(); + await sleep(50); + wrapper.update(); + expect(wrapper.find('.ant-btn-loading').length).toBe(0); expect(() => { wrapper.unmount(); }).not.toThrow(); diff --git a/components/button/button.tsx b/components/button/button.tsx index be9c1b46b4..e73982df22 100644 --- a/components/button/button.tsx +++ b/components/button/button.tsx @@ -134,16 +134,6 @@ class Button extends React.Component { 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 buttonNode: HTMLElement | null; @@ -169,8 +159,10 @@ class Button extends React.Component { const { loading } = this.props; if (loading && typeof loading !== 'boolean' && loading.delay) { - this.delayTimeout = window.setTimeout(() => this.setState({ loading }), loading.delay); - } else if (prevProps.loading !== this.props.loading) { + this.delayTimeout = window.setTimeout(() => { + this.setState({ loading }); + }, loading.delay); + } else if (prevProps.loading !== loading) { // eslint-disable-next-line react/no-did-update-set-state this.setState({ loading }); } @@ -260,7 +252,7 @@ class Button extends React.Component { [`${prefixCls}-${shape}`]: shape, [`${prefixCls}-${sizeCls}`]: sizeCls, [`${prefixCls}-icon-only`]: !children && children !== 0 && iconType, - [`${prefixCls}-loading`]: loading, + [`${prefixCls}-loading`]: !!loading, [`${prefixCls}-background-ghost`]: ghost, [`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && autoInsertSpace, [`${prefixCls}-block`]: block,