import React from 'react'; import type { DropDownProps } from '..'; import Dropdown from '..'; import mountTest from '../../../tests/shared/mountTest'; import rtlTest from '../../../tests/shared/rtlTest'; import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils'; import { resetWarned } from '../../_util/warning'; describe('Dropdown', () => { const items = [ { label: 'foo', key: '1', }, ]; mountTest(() => ( )); rtlTest(() => ( )); it('overlay is function and has custom transitionName', () => { const { asFragment } = render(
menu
} transitionName="move-up" open>
, ); expect(Array.from(asFragment().childNodes)).toMatchSnapshot(); }); it('overlay is string', () => { const { asFragment } = render( , ); expect(Array.from(asFragment().childNodes)).toMatchSnapshot(); }); it('should render custom dropdown correctly', () => { const { asFragment } = render( (
{menu}
CUSTOM NODE
)} >
, ); expect(Array.from(asFragment().childNodes)).toMatchSnapshot(); }); it('support Menu expandIcon', async () => { vi.useFakeTimers(); const props: DropDownProps = { menu: { items: [ { label: 'foo', key: '1', }, { label: 'SubMenu', key: 'submenu', children: [ { label: 'foo', key: '1', }, ], }, ], expandIcon: , }, open: true, getPopupContainer: (node) => node, }; const { container } = render( , ); await waitFakeTimer(); expect(container.querySelectorAll('#customExpandIcon').length).toBe(1); vi.useRealTimers(); }); it('should warn if use topCenter or bottomCenter', () => { const error = vi.spyOn(console, 'error').mockImplementation(() => {}); render(
, ); expect(error).toHaveBeenCalledWith( expect.stringContaining("[antd: Dropdown] You are using 'bottomCenter'"), ); expect(error).toHaveBeenCalledWith( expect.stringContaining("[antd: Dropdown] You are using 'topCenter'"), ); error.mockRestore(); }); // zombieJ: when replaced with react test lib, it may be mock fully content it('dropdown should support auto adjust placement', () => { render( , ); expect(globalThis.triggerProps.builtinPlacements).toEqual( expect.objectContaining({ bottomLeft: expect.objectContaining({ overflow: { adjustX: true, adjustY: true, }, }), }), ); }); it('menu item with group', async () => { vi.useFakeTimers(); const { container } = render( , ); // Open fireEvent.click(container.querySelector('a')!); act(() => { vi.runAllTimers(); }); // Close fireEvent.click(container.querySelector('.ant-dropdown-menu-item')!); // Force Motion move on await waitFakeTimer(0); // Motion End fireEvent.animationEnd(container.querySelector('.ant-slide-up-leave-active')!); expect(container.querySelector('.ant-dropdown-hidden')).toBeTruthy(); vi.useRealTimers(); }); it('legacy visible', () => { resetWarned(); const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); const onOpenChange = vi.fn(); const onVisibleChange = vi.fn(); const { container, rerender } = render( , key: 'bamboo', }, ], }} > , ); expect(document.querySelector('.bamboo')).toBeTruthy(); expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Dropdown] `visible` is deprecated, please use `open` instead.', ); expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Dropdown] `onVisibleChange` is deprecated, please use `onOpenChange` instead.', ); fireEvent.click(container.querySelector('.little')!); expect(onOpenChange).toHaveBeenCalled(); expect(onVisibleChange).toHaveBeenCalled(); rerender( menu}> , ); expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Dropdown] `overlay` is deprecated. Please use `menu` instead.', ); errorSpy.mockRestore(); }); });