2022-08-15 17:47:10 +08:00
|
|
|
import React from 'react';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2023-06-18 13:21:39 +08:00
|
|
|
import { render, waitFakeTimer } from '../../../tests/utils';
|
2023-02-15 10:21:28 +08:00
|
|
|
import type { DropdownProps } from '../dropdown';
|
|
|
|
import DropdownButton from '../dropdown-button';
|
2022-08-15 17:47:10 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
let dropdownProps: DropdownProps;
|
2022-09-30 17:44:24 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.mock('../dropdown', () => {
|
|
|
|
const ActualDropdown = jest.requireActual('../dropdown');
|
2022-08-15 17:47:10 +08:00
|
|
|
const ActualDropdownComponent = ActualDropdown.default;
|
2023-06-07 21:59:21 +08:00
|
|
|
const h: typeof React = jest.requireActual('react');
|
2022-08-15 17:47:10 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
const MockedDropdown: React.FC<DropdownProps> & {
|
|
|
|
Button: typeof ActualDropdownComponent.Button;
|
|
|
|
} = (props) => {
|
2023-02-15 10:21:28 +08:00
|
|
|
const clone: Record<string, any> = {};
|
|
|
|
Object.keys(props).forEach((key: keyof typeof props) => {
|
|
|
|
clone[key] = props[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
dropdownProps = clone;
|
2022-08-15 17:47:10 +08:00
|
|
|
const { children, ...restProps } = props;
|
|
|
|
return h.createElement(ActualDropdownComponent, { ...restProps }, children);
|
|
|
|
};
|
2023-06-07 21:59:21 +08:00
|
|
|
MockedDropdown.Button = ActualDropdownComponent.Button;
|
2022-08-15 17:47:10 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
...ActualDropdown,
|
2023-06-07 21:59:21 +08:00
|
|
|
__esModule: true,
|
2022-09-30 17:44:24 +08:00
|
|
|
default: MockedDropdown,
|
2022-08-15 17:47:10 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DropdownButton', () => {
|
|
|
|
mountTest(DropdownButton);
|
|
|
|
rtlTest(DropdownButton);
|
|
|
|
|
|
|
|
it('pass appropriate props to Dropdown', () => {
|
2022-10-23 00:33:45 +08:00
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
label: 'foo',
|
|
|
|
key: '1',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-08-15 17:47:10 +08:00
|
|
|
const props: DropdownProps = {
|
|
|
|
align: {
|
|
|
|
offset: [10, 20],
|
|
|
|
},
|
2022-10-23 00:33:45 +08:00
|
|
|
menu: { items },
|
2022-08-15 17:47:10 +08:00
|
|
|
disabled: false,
|
|
|
|
trigger: ['hover'],
|
2022-09-02 17:22:23 +08:00
|
|
|
open: true,
|
|
|
|
onOpenChange: () => {},
|
2022-08-15 17:47:10 +08:00
|
|
|
};
|
|
|
|
|
2022-09-02 17:22:23 +08:00
|
|
|
const { rerender } = render(<DropdownButton {...props} />);
|
2022-08-15 17:47:10 +08:00
|
|
|
|
|
|
|
Object.keys(props).forEach((key: keyof DropdownProps) => {
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(dropdownProps[key]).toBe(props[key]);
|
2022-08-15 17:47:10 +08:00
|
|
|
});
|
2022-09-02 17:22:23 +08:00
|
|
|
|
2022-11-07 23:32:46 +08:00
|
|
|
rerender(<DropdownButton menu={{ items }} open />);
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(dropdownProps.open).toBe(true);
|
2022-08-15 17:47:10 +08:00
|
|
|
});
|
|
|
|
|
2022-09-02 17:22:23 +08:00
|
|
|
it("don't pass open to Dropdown if it's not exits", () => {
|
2022-10-23 00:33:45 +08:00
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
label: 'foo',
|
|
|
|
key: '1',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
render(<DropdownButton menu={{ items }} />);
|
2023-06-07 21:59:21 +08:00
|
|
|
expect('open' in dropdownProps).toBe(false);
|
2022-08-15 17:47:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support href like Button', () => {
|
2022-10-23 00:33:45 +08:00
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
label: 'foo',
|
|
|
|
key: '1',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const { asFragment } = render(<DropdownButton menu={{ items }} href="https://ant.design" />);
|
2022-08-15 17:47:10 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('have static property for type detecting', () => {
|
|
|
|
expect(DropdownButton.__ANT_BUTTON).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should pass mouseEnterDelay and mouseLeaveDelay to Dropdown', () => {
|
2022-10-23 00:33:45 +08:00
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
label: 'foo',
|
|
|
|
key: '1',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
render(<DropdownButton mouseEnterDelay={1} mouseLeaveDelay={2} menu={{ items }} />);
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(dropdownProps.mouseEnterDelay).toBe(1);
|
|
|
|
expect(dropdownProps.mouseLeaveDelay).toBe(2);
|
2022-08-15 17:47:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support overlayClassName and overlayStyle', () => {
|
2022-10-23 00:33:45 +08:00
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
label: 'foo',
|
|
|
|
key: '1',
|
|
|
|
},
|
|
|
|
];
|
2022-08-15 17:47:10 +08:00
|
|
|
const { container } = render(
|
|
|
|
<DropdownButton
|
|
|
|
overlayClassName="className"
|
|
|
|
overlayStyle={{ color: 'red' }}
|
2022-10-23 00:33:45 +08:00
|
|
|
menu={{ items }}
|
2022-09-02 17:22:23 +08:00
|
|
|
open
|
2022-08-15 17:47:10 +08:00
|
|
|
/>,
|
|
|
|
);
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(container.querySelector('.ant-dropdown')?.classList).toContain('className');
|
2022-08-15 17:47:10 +08:00
|
|
|
expect((container.querySelector('.ant-dropdown') as HTMLElement).style.color).toContain('red');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support loading', () => {
|
2022-10-23 00:33:45 +08:00
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
label: 'foo',
|
|
|
|
key: '1',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const { container } = render(<DropdownButton menu={{ items }} loading />);
|
2022-08-15 17:47:10 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(container.querySelector('.ant-dropdown-button .ant-btn-loading')?.classList).toContain(
|
|
|
|
'ant-btn',
|
|
|
|
);
|
2022-08-15 17:47:10 +08:00
|
|
|
});
|
2022-11-15 15:51:34 +08:00
|
|
|
it('should console Error when `overlay` in props', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-11-09 09:46:27 +08:00
|
|
|
render(<DropdownButton overlay={<div>test</div>} />);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Dropdown] `overlay` is deprecated. Please use `menu` instead.',
|
|
|
|
);
|
2022-11-09 11:55:11 +08:00
|
|
|
errSpy.mockRestore();
|
2022-11-09 09:46:27 +08:00
|
|
|
});
|
2022-11-15 15:51:34 +08:00
|
|
|
it('should not console Error when `overlay` not in props', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-11-09 09:46:27 +08:00
|
|
|
render(<DropdownButton />);
|
2022-11-09 11:55:11 +08:00
|
|
|
expect(errSpy).not.toHaveBeenCalled();
|
|
|
|
errSpy.mockRestore();
|
2022-11-09 09:46:27 +08:00
|
|
|
});
|
2022-11-22 20:33:10 +08:00
|
|
|
|
|
|
|
it('should support dropdownRender', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const dropdownRender = jest.fn((menu) => <div>Custom Menu {menu}</div>);
|
2022-11-22 20:33:10 +08:00
|
|
|
render(<DropdownButton open dropdownRender={dropdownRender} />);
|
|
|
|
expect(dropdownRender).toHaveBeenCalled();
|
|
|
|
});
|
2023-06-18 13:21:39 +08:00
|
|
|
|
|
|
|
it('should support focus menu when set autoFocus', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
label: 'foo',
|
|
|
|
key: '1',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const { container } = render(<DropdownButton open autoFocus menu={{ items }} />);
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect(container.querySelector('.ant-dropdown-menu-item-active')).toBeTruthy();
|
|
|
|
});
|
2022-08-15 17:47:10 +08:00
|
|
|
});
|