Fix async test cases

This commit is contained in:
afc163 2017-11-16 17:12:36 +08:00
parent 9a9196ca10
commit 46d1296045
5 changed files with 64 additions and 31 deletions

View File

@ -2,17 +2,23 @@ import React from 'react';
import { mount } from 'enzyme';
import BackTop from '..';
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
describe('BackTop', () => {
it('should scroll to top after click it', async () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('should scroll to top after click it', () => {
const wrapper = mount(<BackTop visibilityHeight={-1} />);
document.documentElement.scrollTop = 400;
// trigger scroll manually
wrapper.node.handleScroll();
await delay(500);
jest.runAllTimers();
wrapper.find('.ant-back-top').simulate('click');
await delay(500);
jest.runAllTimers();
expect(Math.round(document.documentElement.scrollTop)).toBe(0);
});
});

View File

@ -2,10 +2,17 @@ import React from 'react';
import { mount } from 'enzyme';
import Card from '../index';
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
const testMethod = typeof window !== 'undefined' ? it : xit;
describe('Card', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
function fakeResizeWindowTo(wrapper, width) {
Object.defineProperties(wrapper.node.container, {
offsetWidth: {
@ -16,13 +23,13 @@ describe('Card', () => {
window.resizeTo(width);
}
testMethod('resize card will trigger different padding', async () => {
testMethod('resize card will trigger different padding', () => {
const wrapper = mount(<Card title="xxx">xxx</Card>);
fakeResizeWindowTo(wrapper, 1000);
await delay(0);
jest.runAllTimers();
expect(wrapper.hasClass('ant-card-wider-padding')).toBe(true);
fakeResizeWindowTo(wrapper, 800);
await delay(0);
jest.runAllTimers();
expect(wrapper.hasClass('ant-card-wider-padding')).toBe(false);
});
});

View File

@ -5,10 +5,8 @@ import Form from '../../form';
const { TextArea } = Input;
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
describe('Input', () => {
it('should support maxLength', async () => {
it('should support maxLength', () => {
const wrapper = mount(
<Input maxLength="3" />
);
@ -17,27 +15,35 @@ describe('Input', () => {
});
describe('TextArea', () => {
it('should auto calculate height according to content length', async () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('should auto calculate height according to content length', () => {
const wrapper = mount(
<TextArea value="" readOnly autosize />
);
const mockFunc = jest.spyOn(wrapper.node, 'resizeTextarea');
wrapper.setProps({ value: '1111\n2222\n3333' });
await delay(0);
jest.runAllTimers();
expect(mockFunc).toHaveBeenCalledTimes(1);
wrapper.setProps({ value: '1111' });
await delay(0);
jest.runAllTimers();
expect(mockFunc).toHaveBeenCalledTimes(2);
});
it('should support disabled', async () => {
it('should support disabled', () => {
const wrapper = mount(
<TextArea disabled />
);
expect(wrapper).toMatchSnapshot();
});
it('should support maxLength', async () => {
it('should support maxLength', () => {
const wrapper = mount(
<TextArea maxLength="10" />
);
@ -46,7 +52,7 @@ describe('TextArea', () => {
});
describe('As Form Control', () => {
it('should be reset when wrapped in form.getFieldDecorator without initialValue', async () => {
it('should be reset when wrapped in form.getFieldDecorator without initialValue', () => {
class Demo extends React.Component {
reset = () => {
this.props.form.resetFields();
@ -79,7 +85,7 @@ describe('As Form Control', () => {
});
describe('Input.Search', () => {
it('should support suffix', async () => {
it('should support suffix', () => {
const wrapper = mount(
<Input.Search suffix="suffix" />
);

View File

@ -4,9 +4,16 @@ import Menu from '..';
import Icon from '../../icon';
const { SubMenu } = Menu;
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
describe('Menu', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('should accept defaultOpenKeys in mode horizontal', () => {
const wrapper = mount(
<Menu defaultOpenKeys={['1']} mode="horizontal">
@ -131,6 +138,7 @@ describe('Menu', () => {
});
it('should always follow openKeys when mode is switched', () => {
jest.useRealTimers();
const wrapper = mount(
<Menu defaultOpenKeys={['1']} mode="inline">
<Menu.Item key="menu1">
@ -160,7 +168,7 @@ describe('Menu', () => {
}, 300);
});
it('should open submenu when click submenu title (inline)', async () => {
it('should open submenu when click submenu title (inline)', () => {
const wrapper = mount(
<Menu mode="inline">
<SubMenu key="1" title="submenu1">
@ -175,11 +183,11 @@ describe('Menu', () => {
expect(wrapper.find('.ant-menu-sub').length).toBe(1);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
wrapper.find('.ant-menu-submenu-title').simulate('click');
await delay(300);
jest.runAllTimers();
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).toBe(true);
});
it('should open submenu when hover submenu title (vertical)', async () => {
it('should open submenu when hover submenu title (vertical)', () => {
const wrapper = mount(
<Menu mode="vertical">
<SubMenu key="1" title="submenu1">
@ -194,11 +202,11 @@ describe('Menu', () => {
expect(wrapper.find('.ant-menu-sub').length).toBe(1);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
wrapper.find('.ant-menu-submenu').simulate('mouseleave');
await delay(300);
jest.runAllTimers();
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).toBe(true);
});
it('should open submenu when hover submenu title (horizontal)', async () => {
it('should open submenu when hover submenu title (horizontal)', () => {
const wrapper = mount(
<Menu mode="horizontal">
<SubMenu key="1" title="submenu1">
@ -213,7 +221,7 @@ describe('Menu', () => {
expect(wrapper.find('.ant-menu-sub').length).toBe(1);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
wrapper.find('.ant-menu-submenu').simulate('mouseleave');
await delay(300);
jest.runAllTimers();
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).toBe(true);
});
});

View File

@ -1,8 +1,14 @@
import message from '..';
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
describe('message', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
afterEach(() => {
message.destroy();
});
@ -28,15 +34,15 @@ describe('message', () => {
expect(document.querySelectorAll('.custom-container').length).toBe(1);
});
it('should be able to hide manually', async () => {
it('should be able to hide manually', () => {
const hide1 = message.info('whatever', 0);
const hide2 = message.info('whatever', 0);
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
hide1();
await delay(100);
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
hide2();
await delay(100);
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
});