From b4a228c4f44c9a1e1a97b4437d914550cfde019b Mon Sep 17 00:00:00 2001 From: shao Date: Wed, 31 Jul 2019 18:06:31 +0800 Subject: [PATCH] perf: replace all test suite delay func to common test util sleep func --- components/anchor/__tests__/Anchor.test.js | 17 ++++++++--------- components/statistic/__tests__/index.test.js | 5 ++--- components/tooltip/__tests__/tooltip.test.js | 11 +++++------ components/upload/__tests__/uploadlist.test.js | 11 +++++------ 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/components/anchor/__tests__/Anchor.test.js b/components/anchor/__tests__/Anchor.test.js index 9bcf4a1d96..e5b16b8ece 100644 --- a/components/anchor/__tests__/Anchor.test.js +++ b/components/anchor/__tests__/Anchor.test.js @@ -2,11 +2,10 @@ import React from 'react'; import { mount } from 'enzyme'; import Anchor from '..'; import { spyElementPrototypes } from '../../__tests__/util/domHook'; +import { sleep } from '../../../tests/utils'; const { Link } = Anchor; -const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout)); - describe('Anchor Render', () => { const getBoundingClientRectMock = jest.fn(() => ({ width: 100, @@ -82,7 +81,7 @@ describe('Anchor Render', () => { wrapper.instance().handleScrollTo('##API'); expect(wrapper.instance().state.activeLink).toBe('##API'); expect(scrollToSpy).not.toHaveBeenCalled(); - await delay(1000); + await sleep(1000); expect(scrollToSpy).toHaveBeenCalled(); }); @@ -172,7 +171,7 @@ describe('Anchor Render', () => { , ); const removeListenerSpy = jest.spyOn(wrapper.instance().scrollEvent, 'remove'); - await delay(1000); + await sleep(1000); wrapper.setProps({ getContainer: getContainerB }); expect(removeListenerSpy).not.toHaveBeenCalled(); }); @@ -205,7 +204,7 @@ describe('Anchor Render', () => { ); const removeListenerSpy = jest.spyOn(wrapper.instance().scrollEvent, 'remove'); expect(removeListenerSpy).not.toHaveBeenCalled(); - await delay(1000); + await sleep(1000); wrapper.setProps({ getContainer: getContainerB }); expect(removeListenerSpy).toHaveBeenCalled(); }); @@ -257,7 +256,7 @@ describe('Anchor Render', () => { ); const removeListenerSpy = jest.spyOn(wrapper.instance().scrollEvent, 'remove'); expect(removeListenerSpy).not.toHaveBeenCalled(); - await delay(1000); + await sleep(1000); holdContainer.container = document.getElementById('API2'); wrapper.setProps({ 'data-only-trigger-re-render': true }); expect(removeListenerSpy).toHaveBeenCalled(); @@ -289,17 +288,17 @@ describe('Anchor Render', () => { , ); wrapper.instance().handleScrollTo('#API'); - await delay(1000); + await sleep(1000); expect(scrollToSpy).toHaveBeenLastCalledWith(0, 1000); wrapper.setProps({ offsetTop: 100 }); wrapper.instance().handleScrollTo('#API'); - await delay(1000); + await sleep(1000); expect(scrollToSpy).toHaveBeenLastCalledWith(0, 900); wrapper.setProps({ targetOffset: 200 }); wrapper.instance().handleScrollTo('#API'); - await delay(1000); + await sleep(1000); expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800); }); }); diff --git a/components/statistic/__tests__/index.test.js b/components/statistic/__tests__/index.test.js index bfa2024597..affb9003fc 100644 --- a/components/statistic/__tests__/index.test.js +++ b/components/statistic/__tests__/index.test.js @@ -4,8 +4,7 @@ import moment from 'moment'; import { mount } from 'enzyme'; import Statistic from '..'; import { formatTimeStr } from '../utils'; - -const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout)); +import { sleep } from '../../../tests/utils'; describe('Statistic', () => { beforeAll(() => { @@ -70,7 +69,7 @@ describe('Statistic', () => { const instance = wrapper.instance(); expect(instance.countdownId).not.toBe(undefined); - await delay(10); + await sleep(10); wrapper.unmount(); expect(instance.countdownId).toBe(undefined); diff --git a/components/tooltip/__tests__/tooltip.test.js b/components/tooltip/__tests__/tooltip.test.js index c5d5d7a25d..8004ba805a 100644 --- a/components/tooltip/__tests__/tooltip.test.js +++ b/components/tooltip/__tests__/tooltip.test.js @@ -7,8 +7,7 @@ import Checkbox from '../../checkbox'; import DatePicker from '../../date-picker'; import Input from '../../input'; import Group from '../../input/Group'; - -const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout)); +import { sleep } from '../../../tests/utils'; describe('Tooltip', () => { it('check `onVisibleChange` arguments', () => { @@ -205,12 +204,12 @@ describe('Tooltip', () => { expect(wrapper.find('span.ant-calendar-picker')).toHaveLength(1); const picker = wrapper.find('span.ant-calendar-picker').at(0); picker.simulate('mouseenter'); - await delay(100); + await sleep(100); expect(onVisibleChange).toHaveBeenCalledWith(true); expect(wrapper.instance().tooltip.props.visible).toBe(true); picker.simulate('mouseleave'); - await delay(100); + await sleep(100); expect(onVisibleChange).toHaveBeenCalledWith(false); expect(wrapper.instance().tooltip.props.visible).toBe(false); }); @@ -230,12 +229,12 @@ describe('Tooltip', () => { expect(wrapper.find('Group')).toHaveLength(1); const picker = wrapper.find('Group').at(0); picker.simulate('mouseenter'); - await delay(100); + await sleep(100); expect(onVisibleChange).toHaveBeenCalledWith(true); expect(wrapper.instance().tooltip.props.visible).toBe(true); picker.simulate('mouseleave'); - await delay(100); + await sleep(100); expect(onVisibleChange).toHaveBeenCalledWith(false); expect(wrapper.instance().tooltip.props.visible).toBe(false); }); diff --git a/components/upload/__tests__/uploadlist.test.js b/components/upload/__tests__/uploadlist.test.js index 6f42e20168..8690ba6d27 100644 --- a/components/upload/__tests__/uploadlist.test.js +++ b/components/upload/__tests__/uploadlist.test.js @@ -6,8 +6,7 @@ import Form from '../../form'; import { spyElementPrototypes } from '../../__tests__/util/domHook'; import { errorRequest, successRequest } from './requests'; import { setup, teardown } from './mock'; - -const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout)); +import { sleep } from '../../../tests/utils'; const fileList = [ { @@ -124,7 +123,7 @@ describe('Upload List', () => { .at(0) .find('.anticon-close') .simulate('click'); - await delay(400); + await sleep(400); wrapper.update(); expect(wrapper.find('.ant-upload-list-item').hostNodes().length).toBe(1); }); @@ -245,7 +244,7 @@ describe('Upload List', () => { .at(1) .simulate('click'); expect(handleRemove).toHaveBeenCalledWith(fileList[1]); - await delay(0); + await sleep(); expect(handleChange.mock.calls.length).toBe(2); }); @@ -274,7 +273,7 @@ describe('Upload List', () => { , ); wrapper.setState({}); - await delay(0); + await sleep(); expect(wrapper.state().fileList[2].thumbUrl).not.toBe(undefined); expect(onDrawImage).toHaveBeenCalled(); @@ -525,7 +524,7 @@ describe('Upload List', () => { , ); wrapper.setState({}); - await delay(0); + await sleep(); expect(previewFile).toHaveBeenCalledWith(file.originFileObj); wrapper.update();