test: Motion for Collpase and Menu (#26828)

* test:  test menu and collapse motion

* chore: move openAnimation into collapse

* add test case

* rm extra openAnimation.tsx

* remove unused code
This commit is contained in:
偏右 2020-09-20 21:13:43 +08:00 committed by GitHub
parent 2aef2dbe26
commit c979850a5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 23 deletions

View File

@ -7,7 +7,6 @@ import throttleByAnimationFrame from '../throttleByAnimationFrame';
import getDataOrAriaProps from '../getDataOrAriaProps'; import getDataOrAriaProps from '../getDataOrAriaProps';
import Wave from '../wave'; import Wave from '../wave';
import TransButton from '../transButton'; import TransButton from '../transButton';
import openAnimation from '../openAnimation';
import { sleep } from '../../../tests/utils'; import { sleep } from '../../../tests/utils';
import focusTest from '../../../tests/shared/focusTest'; import focusTest from '../../../tests/shared/focusTest';
@ -186,21 +185,4 @@ describe('Test utils function', () => {
expect(preventDefault).toHaveBeenCalled(); expect(preventDefault).toHaveBeenCalled();
}); });
}); });
describe('openAnimation', () => {
it('should support openAnimation', () => {
const done = jest.fn();
const domNode = document.createElement('div');
expect(typeof openAnimation.enter).toBe('function');
expect(typeof openAnimation.leave).toBe('function');
expect(typeof openAnimation.appear).toBe('function');
const appear = openAnimation.appear(domNode, done);
const enter = openAnimation.enter(domNode, done);
const leave = openAnimation.leave(domNode, done);
expect(typeof appear.stop).toBe('function');
expect(typeof enter.stop).toBe('function');
expect(typeof leave.stop).toBe('function');
expect(done).toHaveBeenCalled();
});
});
}); });

View File

@ -5,7 +5,7 @@ import RightOutlined from '@ant-design/icons/RightOutlined';
import CollapsePanel from './CollapsePanel'; import CollapsePanel from './CollapsePanel';
import { ConfigContext } from '../config-provider'; import { ConfigContext } from '../config-provider';
import animation from '../_util/openAnimation'; import animation from './openAnimation';
import { cloneElement } from '../_util/reactNode'; import { cloneElement } from '../_util/reactNode';
export type ExpandIconPosition = 'left' | 'right' | undefined; export type ExpandIconPosition = 'left' | 'right' | undefined;

View File

@ -1,8 +1,26 @@
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
import Collapse from '..'; import Collapse from '..';
import openAnimation from '../openAnimation';
describe('Collapse', () => { describe('Collapse', () => {
mountTest(Collapse); mountTest(Collapse);
rtlTest(Collapse); rtlTest(Collapse);
}); });
describe('openAnimation', () => {
it('should support openAnimation', () => {
const done = jest.fn();
const domNode = document.createElement('div');
expect(typeof openAnimation.enter).toBe('function');
expect(typeof openAnimation.leave).toBe('function');
expect(typeof openAnimation.appear).toBe('function');
const appear = openAnimation.appear(domNode, done);
const enter = openAnimation.enter(domNode, done);
const leave = openAnimation.leave(domNode, done);
expect(typeof appear.stop).toBe('function');
expect(typeof enter.stop).toBe('function');
expect(typeof leave.stop).toBe('function');
expect(done).toHaveBeenCalled();
});
});

View File

@ -20,9 +20,6 @@ function animate(node: HTMLElement, show: boolean, done: () => void) {
} }
}, },
active() { active() {
if (requestAnimationFrameId) {
raf.cancel(requestAnimationFrameId);
}
requestAnimationFrameId = raf(() => { requestAnimationFrameId = raf(() => {
node.style.height = `${show ? height : 0}px`; node.style.height = `${show ? height : 0}px`;
node.style.opacity = show ? '1' : '0'; node.style.opacity = show ? '1' : '0';

View File

@ -12,6 +12,8 @@ import Layout from '../../layout';
import Tooltip from '../../tooltip'; import Tooltip from '../../tooltip';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
import collapseMotion from '../../_util/motion';
import { sleep } from '../../../tests/utils';
const { SubMenu } = Menu; const { SubMenu } = Menu;
@ -393,6 +395,25 @@ describe('Menu', () => {
); );
}); });
it('inline menu collapseMotion should be triggered', async () => {
jest.useRealTimers();
const onAppearEnd = jest.spyOn(collapseMotion, 'onAppearEnd');
collapseMotion.motionDeadline = 1;
const wrapper = mount(
<Menu mode="inline">
<SubMenu key="1" title="submenu1">
<Menu.Item key="submenu1">Option 1</Menu.Item>
<Menu.Item key="submenu2">Option 2</Menu.Item>
</SubMenu>
<Menu.Item key="2">menu2</Menu.Item>
</Menu>,
);
wrapper.find('.ant-menu-submenu-title').simulate('click');
await sleep(3000);
expect(onAppearEnd).toHaveBeenCalledTimes(1);
onAppearEnd.mockRestore();
});
it('vertical with hover(default)', () => { it('vertical with hover(default)', () => {
const wrapper = mount( const wrapper = mount(
<Menu mode="vertical"> <Menu mode="vertical">

View File

@ -1,4 +1,5 @@
import MockDate from 'mockdate'; import MockDate from 'mockdate';
import { act } from 'react-dom/test-utils';
export function setMockDate(dateString = '2017-09-18T03:30:07.795') { export function setMockDate(dateString = '2017-09-18T03:30:07.795') {
MockDate.set(dateString); MockDate.set(dateString);
@ -10,4 +11,8 @@ export function resetMockDate() {
const globalTimeout = global.setTimeout; const globalTimeout = global.setTimeout;
export const sleep = (timeout = 0) => new Promise(resolve => globalTimeout(resolve, timeout)); export const sleep = async (timeout = 0) => {
await act(async () => {
await new Promise(resolve => globalTimeout(resolve, timeout));
});
};