perf: replace all test suite delay func to common test util sleep func

This commit is contained in:
shao 2019-07-31 18:06:31 +08:00
parent d45dc1b347
commit b4a228c4f4
4 changed files with 20 additions and 24 deletions

View File

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

View File

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

View File

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

View File

@ -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', () => {
</Upload>,
);
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', () => {
</Upload>,
);
wrapper.setState({});
await delay(0);
await sleep();
expect(previewFile).toHaveBeenCalledWith(file.originFileObj);
wrapper.update();