ant-design/components/menu/__tests__/index.test.js

226 lines
9.1 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { mount } from 'enzyme';
import Menu from '..';
2017-07-01 14:04:26 +08:00
import Icon from '../../icon';
2017-10-09 13:23:20 +08:00
const { SubMenu } = Menu;
2017-07-07 17:15:29 +08:00
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
describe('Menu', () => {
it('should accept defaultOpenKeys in mode horizontal', () => {
const wrapper = mount(
<Menu defaultOpenKeys={['1']} mode="horizontal">
<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>
);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
});
it('should accept defaultOpenKeys in mode inline', () => {
const wrapper = mount(
<Menu defaultOpenKeys={['1']} 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>
);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
});
it('should accept defaultOpenKeys in mode vertical', () => {
const wrapper = mount(
<Menu defaultOpenKeys={['1']} mode="vertical">
<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>
);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
});
it('should accept openKeys in mode horizontal', () => {
const wrapper = mount(
2017-02-15 18:00:20 +08:00
<Menu openKeys={['1']} mode="horizontal" openTransitionName="">
<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>
);
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
2017-02-15 18:00:20 +08:00
wrapper.setProps({ openKeys: [] });
2017-09-20 16:26:18 +08:00
wrapper.update();
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).toBe(true);
2017-02-15 18:00:20 +08:00
wrapper.setProps({ openKeys: ['1'] });
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
});
2017-03-28 17:16:15 +08:00
it('should accept openKeys in mode inline', () => {
const wrapper = mount(
2017-02-15 18:00:20 +08:00
<Menu openKeys={['1']} mode="inline" openAnimation="">
<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>
);
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
2017-02-15 18:00:20 +08:00
wrapper.setProps({ openKeys: [] });
2017-09-20 16:26:18 +08:00
wrapper.update();
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).toBe(true);
2017-02-15 18:00:20 +08:00
wrapper.setProps({ openKeys: ['1'] });
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
});
it('should accept openKeys in mode vertical', () => {
const wrapper = mount(
2017-02-15 18:00:20 +08:00
<Menu openKeys={['1']} mode="vertical" openTransitionName="">
<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>
);
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
2017-02-15 18:00:20 +08:00
wrapper.setProps({ openKeys: [] });
2017-09-20 16:26:18 +08:00
wrapper.update();
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).toBe(true);
2017-02-15 18:00:20 +08:00
wrapper.setProps({ openKeys: ['1'] });
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
});
// https://github.com/ant-design/ant-design/pulls/4677
// https://github.com/ant-design/ant-design/issues/4692
// TypeError: Cannot read property 'indexOf' of undefined
it('pr #4677 and issue #4692', () => {
const wrapper = mount(
<Menu mode="horizontal">
<SubMenu title="submenu">
<Menu.Item key="1">menu1</Menu.Item>
<Menu.Item key="2">menu2</Menu.Item>
</SubMenu>
</Menu>
);
wrapper.update();
// just expect no error emit
});
it('should always follow openKeys when mode is switched', () => {
const wrapper = mount(
<Menu openKeys={['1']} 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>
);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
wrapper.setProps({ mode: 'vertical' });
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
wrapper.setProps({ mode: 'inline' });
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).not.toBe(true);
});
2017-06-30 23:08:11 +08:00
it('should always follow openKeys when mode is switched', () => {
const wrapper = mount(
<Menu defaultOpenKeys={['1']} mode="inline">
<Menu.Item key="menu1">
<Icon type="inbox" />
<span>Option</span>
</Menu.Item>
<SubMenu key="1" title="submenu1">
<Menu.Item key="submenu1">
Option
</Menu.Item>
<Menu.Item key="submenu2">
Option
</Menu.Item>
</SubMenu>
</Menu>
);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-inline')).toBe(true);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).toBe(false);
wrapper.setProps({ inlineCollapsed: true });
setTimeout(() => {
// 动画结束后套样式;
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-vertical')).toBe(true);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).toBe(true);
wrapper.setProps({ inlineCollapsed: false });
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-inline')).toBe(true);
expect(wrapper.find('.ant-menu-sub').at(0).hasClass('ant-menu-hidden')).toBe(false);
}, 300);
2017-06-30 23:08:11 +08:00
});
2017-07-07 19:45:59 +08:00
2017-07-07 17:15:29 +08:00
it('should open submenu when click submenu title (inline)', async () => {
2017-07-07 16:51:06 +08:00
const wrapper = mount(
<Menu mode="inline">
2017-07-07 17:15:29 +08:00
<SubMenu key="1" title="submenu1">
2017-07-07 16:51:06 +08:00
<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>
);
expect(wrapper.find('.ant-menu-sub').length).toBe(0);
2017-07-07 17:15:29 +08:00
wrapper.find('.ant-menu-submenu-title').simulate('click');
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().length).toBe(1);
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
2017-07-07 17:15:29 +08:00
wrapper.find('.ant-menu-submenu-title').simulate('click');
await delay(300);
2017-09-20 16:26:18 +08:00
wrapper.update();
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).toBe(true);
2017-07-07 16:51:06 +08:00
});
it('should open submenu when hover submenu title (vertical)', async () => {
const wrapper = mount(
<Menu mode="vertical">
2017-07-07 17:15:29 +08:00
<SubMenu key="1" title="submenu1">
2017-07-07 16:51:06 +08:00
<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>
);
expect(wrapper.find('.ant-menu-sub').length).toBe(0);
wrapper.find('.ant-menu-submenu-title').simulate('mouseenter');
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().length).toBe(1);
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
2017-07-07 17:15:29 +08:00
wrapper.find('.ant-menu-submenu').simulate('mouseleave');
await delay(300);
2017-09-20 16:26:18 +08:00
wrapper.update();
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).toBe(true);
2017-07-07 16:51:06 +08:00
});
it('should open submenu when hover submenu title (horizontal)', async () => {
const wrapper = mount(
<Menu mode="horizontal">
2017-07-07 17:15:29 +08:00
<SubMenu key="1" title="submenu1">
2017-07-07 16:51:06 +08:00
<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>
);
expect(wrapper.find('.ant-menu-sub').length).toBe(0);
wrapper.find('.ant-menu-submenu-title').simulate('mouseenter');
2017-09-20 16:26:18 +08:00
expect(wrapper.find('.ant-menu-sub').hostNodes().length).toBe(1);
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).not.toBe(true);
2017-07-07 17:15:29 +08:00
wrapper.find('.ant-menu-submenu').simulate('mouseleave');
await delay(300);
2017-09-20 16:26:18 +08:00
wrapper.update();
expect(wrapper.find('.ant-menu-sub').hostNodes().at(0).hasClass('ant-menu-hidden')).toBe(true);
2017-07-07 16:51:06 +08:00
});
});