2022-06-21 15:48:29 +08:00
|
|
|
import React from 'react';
|
2022-08-15 17:47:10 +08:00
|
|
|
import type { TriggerProps } from 'rc-trigger';
|
2020-05-25 16:27:02 +08:00
|
|
|
import Dropdown from '..';
|
2022-08-15 17:47:10 +08:00
|
|
|
import type { DropDownProps } from '..';
|
2020-05-25 16:27:02 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-10-21 15:07:44 +08:00
|
|
|
import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2022-06-21 15:48:29 +08:00
|
|
|
import Menu from '../../menu';
|
2022-09-06 21:46:49 +08:00
|
|
|
import { resetWarned } from '../../_util/warning';
|
2020-05-25 16:27:02 +08:00
|
|
|
|
2022-08-15 17:47:10 +08:00
|
|
|
let triggerProps: TriggerProps;
|
|
|
|
|
|
|
|
jest.mock('rc-trigger', () => {
|
|
|
|
let Trigger = jest.requireActual('rc-trigger/lib/mock');
|
|
|
|
Trigger = Trigger.default || Trigger;
|
|
|
|
const h: typeof React = jest.requireActual('react');
|
|
|
|
|
|
|
|
return {
|
|
|
|
default: h.forwardRef<unknown, TriggerProps>((props, ref) => {
|
|
|
|
triggerProps = props;
|
|
|
|
return h.createElement(Trigger, { ref, ...props });
|
|
|
|
}),
|
|
|
|
__esModule: true,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-25 16:27:02 +08:00
|
|
|
describe('Dropdown', () => {
|
|
|
|
mountTest(() => (
|
2022-08-15 17:47:10 +08:00
|
|
|
<Dropdown overlay={<Menu />}>
|
2020-05-25 16:27:02 +08:00
|
|
|
<span />
|
|
|
|
</Dropdown>
|
|
|
|
));
|
|
|
|
|
|
|
|
rtlTest(() => (
|
2022-08-15 17:47:10 +08:00
|
|
|
<Dropdown overlay={<Menu />}>
|
2020-05-25 16:27:02 +08:00
|
|
|
<span />
|
|
|
|
</Dropdown>
|
|
|
|
));
|
|
|
|
|
|
|
|
it('overlay is function and has custom transitionName', () => {
|
2022-08-15 17:47:10 +08:00
|
|
|
const { asFragment } = render(
|
2022-09-02 17:22:23 +08:00
|
|
|
<Dropdown overlay={() => <div>menu</div>} transitionName="move-up" open>
|
2020-05-25 16:27:02 +08:00
|
|
|
<button type="button">button</button>
|
|
|
|
</Dropdown>,
|
|
|
|
);
|
2022-08-15 17:47:10 +08:00
|
|
|
expect(Array.from(asFragment().childNodes)).toMatchSnapshot();
|
2020-05-25 16:27:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('overlay is string', () => {
|
2022-08-15 17:47:10 +08:00
|
|
|
const { asFragment } = render(
|
2022-09-02 17:22:23 +08:00
|
|
|
<Dropdown overlay={'string' as any} open>
|
2020-05-25 16:27:02 +08:00
|
|
|
<button type="button">button</button>
|
|
|
|
</Dropdown>,
|
|
|
|
);
|
2022-08-15 17:47:10 +08:00
|
|
|
expect(Array.from(asFragment().childNodes)).toMatchSnapshot();
|
2020-05-25 16:27:02 +08:00
|
|
|
});
|
2021-02-17 17:09:13 +08:00
|
|
|
|
|
|
|
it('support Menu expandIcon', async () => {
|
2022-10-21 15:07:44 +08:00
|
|
|
jest.useFakeTimers();
|
2022-08-15 17:47:10 +08:00
|
|
|
const props: DropDownProps = {
|
2021-02-17 17:09:13 +08:00
|
|
|
overlay: (
|
|
|
|
<Menu expandIcon={<span id="customExpandIcon" />}>
|
2021-06-29 17:55:02 +08:00
|
|
|
<Menu.Item key="1">foo</Menu.Item>
|
2021-02-17 17:09:13 +08:00
|
|
|
<Menu.SubMenu title="SubMenu">
|
2021-06-29 17:55:02 +08:00
|
|
|
<Menu.Item key="1">foo</Menu.Item>
|
2021-02-17 17:09:13 +08:00
|
|
|
</Menu.SubMenu>
|
|
|
|
</Menu>
|
|
|
|
),
|
2022-09-02 17:22:23 +08:00
|
|
|
open: true,
|
2021-02-17 17:09:13 +08:00
|
|
|
getPopupContainer: node => node,
|
|
|
|
};
|
|
|
|
|
2022-08-15 17:47:10 +08:00
|
|
|
const { container } = render(
|
2021-02-17 17:09:13 +08:00
|
|
|
<Dropdown {...props}>
|
|
|
|
<button type="button">button</button>
|
|
|
|
</Dropdown>,
|
|
|
|
);
|
2022-10-21 15:07:44 +08:00
|
|
|
await waitFakeTimer();
|
2022-08-15 17:47:10 +08:00
|
|
|
expect(container.querySelectorAll('#customExpandIcon').length).toBe(1);
|
2022-10-21 15:07:44 +08:00
|
|
|
jest.useRealTimers();
|
2021-02-17 17:09:13 +08:00
|
|
|
});
|
2022-01-13 13:34:34 +08:00
|
|
|
|
|
|
|
it('should warn if use topCenter or bottomCenter', () => {
|
|
|
|
const error = jest.spyOn(console, 'error');
|
2022-08-15 17:47:10 +08:00
|
|
|
render(
|
2022-01-13 13:34:34 +08:00
|
|
|
<div>
|
2022-08-15 17:47:10 +08:00
|
|
|
<Dropdown overlay={'123' as any} placement="bottomCenter">
|
2022-01-13 13:34:34 +08:00
|
|
|
<button type="button">bottomCenter</button>
|
|
|
|
</Dropdown>
|
2022-08-15 17:47:10 +08:00
|
|
|
<Dropdown overlay={'123' as any} placement="topCenter">
|
2022-01-13 13:34:34 +08:00
|
|
|
<button type="button">topCenter</button>
|
|
|
|
</Dropdown>
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
expect(error).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining("[antd: Dropdown] You are using 'bottomCenter'"),
|
|
|
|
);
|
|
|
|
expect(error).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining("[antd: Dropdown] You are using 'topCenter'"),
|
|
|
|
);
|
2022-09-02 17:22:23 +08:00
|
|
|
error.mockRestore();
|
2022-01-13 13:34:34 +08:00
|
|
|
});
|
2022-03-09 19:34:46 +08:00
|
|
|
|
|
|
|
// zombieJ: when replaced with react test lib, it may be mock fully content
|
|
|
|
it('dropdown should support auto adjust placement', () => {
|
2022-08-15 17:47:10 +08:00
|
|
|
render(
|
2022-09-02 17:22:23 +08:00
|
|
|
<Dropdown overlay={<div>menu</div>} open>
|
2022-03-09 19:34:46 +08:00
|
|
|
<button type="button">button</button>
|
|
|
|
</Dropdown>,
|
|
|
|
);
|
|
|
|
|
2022-08-15 17:47:10 +08:00
|
|
|
expect(triggerProps.builtinPlacements).toEqual(
|
2022-03-09 19:34:46 +08:00
|
|
|
expect.objectContaining({
|
|
|
|
bottomLeft: expect.objectContaining({
|
|
|
|
overflow: {
|
|
|
|
adjustX: 1,
|
|
|
|
adjustY: 1,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2022-06-21 15:48:29 +08:00
|
|
|
|
|
|
|
it('menu item with group', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const { container } = render(
|
|
|
|
<Dropdown
|
2022-08-15 17:47:10 +08:00
|
|
|
trigger={['click']}
|
2022-06-21 15:48:29 +08:00
|
|
|
overlay={
|
|
|
|
<Menu
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
label: 'grp',
|
|
|
|
type: 'group',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
label: '1',
|
|
|
|
key: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<a />
|
|
|
|
</Dropdown>,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Open
|
2022-08-15 17:47:10 +08:00
|
|
|
fireEvent.click(container.querySelector('a')!);
|
2022-06-21 15:48:29 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Close
|
2022-08-15 17:47:10 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-dropdown-menu-item')!);
|
2022-06-21 15:48:29 +08:00
|
|
|
|
|
|
|
// Force Motion move on
|
|
|
|
for (let i = 0; i < 10; i += 1) {
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Motion End
|
2022-08-15 17:47:10 +08:00
|
|
|
fireEvent.animationEnd(container.querySelector('.ant-slide-up-leave-active')!);
|
2022-06-21 15:48:29 +08:00
|
|
|
|
|
|
|
expect(container.querySelector('.ant-dropdown-hidden')).toBeTruthy();
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2022-09-06 21:46:49 +08:00
|
|
|
|
|
|
|
it('legacy visible', () => {
|
|
|
|
resetWarned();
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
const onOpenChange = jest.fn();
|
|
|
|
const onVisibleChange = jest.fn();
|
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<Dropdown
|
|
|
|
visible
|
|
|
|
onOpenChange={onOpenChange}
|
|
|
|
onVisibleChange={onVisibleChange}
|
|
|
|
trigger={['click']}
|
|
|
|
overlay={<div className="bamboo" />}
|
|
|
|
>
|
|
|
|
<a className="little" />
|
|
|
|
</Dropdown>,
|
|
|
|
);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2020-05-25 16:27:02 +08:00
|
|
|
});
|