import React from 'react'; import { mount } from 'enzyme'; import Menu from '..'; import Icon from '../../icon'; import Layout from '../../layout'; jest.mock('mutationobserver-shim', () => { global.MutationObserver = function MutationObserver() { this.observe = () => {}; this.disconnect = () => {}; }; }); const { SubMenu } = Menu; describe('Menu', () => { beforeEach(() => { jest.useFakeTimers(); }); afterEach(() => { jest.useRealTimers(); }); it('If has select nested submenu item ,the menu items on the grandfather level should be highlight', () => { const wrapper = mount( Option 1 Option 2 Option 3 Option 4 menu2 , ); expect(wrapper.find('.ant-menu-submenu-selected').length).toBe(1); }); it('should accept defaultOpenKeys in mode horizontal', () => { const wrapper = mount( Option 1 Option 2 menu2 , ); 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( Option 1 Option 2 menu2 , ); 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( Option 1 Option 2 menu2 , ); expect( wrapper .find('.ant-menu-sub') .at(0) .hasClass('ant-menu-hidden'), ).not.toBe(true); }); it('horizontal', () => { const wrapper = mount( Option 1 Option 2 menu2 , ); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).not.toBe(true); wrapper.setProps({ openKeys: [] }); wrapper.update(); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).toBe(true); wrapper.setProps({ openKeys: ['1'] }); wrapper.update(); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).not.toBe(true); }); it('inline', () => { const wrapper = mount( Option 1 Option 2 menu2 , ); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).not.toBe(true); wrapper.setProps({ openKeys: [] }); wrapper.update(); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).toBe(true); wrapper.setProps({ openKeys: ['1'] }); wrapper.update(); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).not.toBe(true); }); it('vertical', () => { const wrapper = mount( Option 1 Option 2 menu2 , ); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).not.toBe(true); wrapper.setProps({ openKeys: [] }); wrapper.update(); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).toBe(true); wrapper.setProps({ openKeys: ['1'] }); wrapper.update(); 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( menu1 menu2 , ); wrapper.update(); // just expect no error emit }); it('should always follow openKeys when mode is switched', () => { const wrapper = mount( Option 1 Option 2 menu2 , ); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-hidden'), ).toBe(false); wrapper.setProps({ mode: 'vertical' }); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-hidden'), ).toBe(false); wrapper.setProps({ mode: 'inline' }); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-hidden'), ).toBe(false); }); it('should always follow openKeys when inlineCollapsed is switched', () => { const wrapper = mount( Option Option Option , ); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-inline'), ).toBe(true); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-hidden'), ).toBe(false); wrapper.setProps({ inlineCollapsed: true }); // 动画结束后套样式; jest.runAllTimers(); wrapper.update(); wrapper.simulate('transitionEnd', { propertyName: 'width' }); expect( wrapper .find('ul.ant-menu-root') .at(0) .hasClass('ant-menu-vertical'), ).toBe(true); expect(wrapper.find('ul.ant-menu-sub').length).toBe(0); wrapper.setProps({ inlineCollapsed: false }); jest.runAllTimers(); wrapper.update(); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-inline'), ).toBe(true); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-hidden'), ).toBe(false); }); it('inlineCollapsed should works well when specify a not existed default openKeys', () => { const wrapper = mount( Option Option Option , ); expect(wrapper.find('.ant-menu-sub').length).toBe(0); wrapper.setProps({ inlineCollapsed: true }); jest.runAllTimers(); wrapper.update(); wrapper.simulate('transitionEnd', { propertyName: 'width' }); wrapper .find('.ant-menu-submenu-title') .at(0) .simulate('mouseEnter'); jest.runAllTimers(); wrapper.update(); expect( wrapper .find('.ant-menu-submenu') .at(0) .hasClass('ant-menu-submenu-vertical'), ).toBe(true); expect( wrapper .find('.ant-menu-submenu') .at(0) .hasClass('ant-menu-submenu-open'), ).toBe(true); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-vertical'), ).toBe(true); expect( wrapper .find('ul.ant-menu-sub') .at(0) .hasClass('ant-menu-hidden'), ).toBe(false); }); describe('open submenu when click submenu title', () => { beforeEach(() => { jest.useFakeTimers(); }); afterEach(() => { jest.useRealTimers(); }); const toggleMenu = (wrapper, index, event) => { wrapper .find('.ant-menu-submenu-title') .at(index) .simulate(event); jest.runAllTimers(); wrapper.update(); }; it('inline', () => { const wrapper = mount( Option 1 Option 2 menu2 , ); expect(wrapper.find('.ant-menu-sub').length).toBe(0); toggleMenu(wrapper, 0, 'click'); 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); toggleMenu(wrapper, 0, 'click'); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).toBe(true); }); it('vertical', () => { const wrapper = mount( Option 1 Option 2 menu2 , ); expect(wrapper.find('.ant-menu-sub').length).toBe(0); toggleMenu(wrapper, 0, 'mouseenter'); 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); toggleMenu(wrapper, 0, 'mouseleave'); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).toBe(true); }); it('horizontal', () => { jest.useFakeTimers(); const wrapper = mount( Option 1 Option 2 menu2 , ); expect(wrapper.find('.ant-menu-sub').length).toBe(0); toggleMenu(wrapper, 0, 'mouseenter'); 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); toggleMenu(wrapper, 0, 'mouseleave'); expect( wrapper .find('.ant-menu-sub') .hostNodes() .at(0) .hasClass('ant-menu-hidden'), ).toBe(true); }); }); it('inline title', () => { jest.useFakeTimers(); const wrapper = mount( Option 1 test , ); wrapper.find('.ant-menu-item').simulate('mouseenter'); jest.runAllTimers(); wrapper.update(); const text = wrapper.find('.ant-tooltip-inner').text(); expect(text).toBe('bamboo lucky'); }); it('render correctly when using with Layout.Sider', () => { class Demo extends React.Component { state = { collapsed: false, }; onCollapse = collapsed => this.setState({ collapsed }); render() { const { collapsed } = this.state; return (
User } > Tom Bill Alex ); } } const wrapper = mount(); wrapper.find('.ant-menu-submenu-title').simulate('click'); wrapper.find('.ant-layout-sider-trigger').simulate('click'); jest.runAllTimers(); wrapper.update(); expect(wrapper.find('.ant-menu-submenu-popup').length).toBe(0); }); it('onMouseEnter should work', () => { const event = { target: 'test' }; const onMouseEnter = e => { expect(e).toEqual(event); }; const wrapper = mount().instance(); wrapper.handleMouseEnter(event); }); it('onOpenChange should work', () => { const event = []; const onOpenChange = e => { expect(e).toEqual(event); }; const wrapper = mount().instance(); wrapper.handleOpenChange(event); }); it('get correct animation type when switched from inline', () => { const wrapper = mount(); wrapper.setProps({ mode: 'horizontal' }); expect(wrapper.instance().getMenuOpenAnimation('')).toBe(''); expect(wrapper.instance().switchingModeFromInline).toBe(false); }); it('Menu should not shake when collapsed changed', () => { const wrapper = mount( Navigation One } > Option 5 Option 6 , ); expect(wrapper.instance().contextSiderCollapsed).toBe(true); wrapper.setProps({ inlineCollapsed: true }); expect(wrapper.instance().contextSiderCollapsed).toBe(false); jest.runAllTimers(); wrapper.update(); expect(wrapper.instance().contextSiderCollapsed).toBe(false); }); });