2022-08-15 17:47:10 +08:00
|
|
|
import React from 'react';
|
|
|
|
import DropdownButton from '../dropdown-button';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
|
|
|
import Menu from '../../menu';
|
|
|
|
import type { DropdownProps } from '../dropdown';
|
|
|
|
import { render } from '../../../tests/utils';
|
|
|
|
|
|
|
|
let dropdownProps: DropdownProps;
|
2022-09-30 17:44:24 +08:00
|
|
|
|
2022-08-15 17:47:10 +08:00
|
|
|
jest.mock('../dropdown', () => {
|
|
|
|
const ActualDropdown = jest.requireActual('../dropdown');
|
|
|
|
const ActualDropdownComponent = ActualDropdown.default;
|
|
|
|
const h: typeof React = jest.requireActual('react');
|
|
|
|
|
2022-09-30 17:44:24 +08:00
|
|
|
const MockedDropdown: React.FC<DropdownProps> & {
|
|
|
|
Button: typeof ActualDropdownComponent.Button;
|
|
|
|
} = props => {
|
2022-08-15 17:47:10 +08:00
|
|
|
dropdownProps = props;
|
|
|
|
const { children, ...restProps } = props;
|
|
|
|
return h.createElement(ActualDropdownComponent, { ...restProps }, children);
|
|
|
|
};
|
2022-09-30 17:44:24 +08:00
|
|
|
MockedDropdown.Button = ActualDropdownComponent.Button;
|
2022-08-15 17:47:10 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
...ActualDropdown,
|
|
|
|
__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', () => {
|
|
|
|
const props: DropdownProps = {
|
|
|
|
align: {
|
|
|
|
offset: [10, 20],
|
|
|
|
},
|
|
|
|
overlay: (
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item key="1">foo</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
),
|
|
|
|
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) => {
|
|
|
|
expect(dropdownProps[key]).toBe(props[key]);
|
|
|
|
});
|
2022-09-02 17:22:23 +08:00
|
|
|
|
2022-09-05 22:02:23 +08:00
|
|
|
rerender(<DropdownButton overlay={<div>123</div>} open />);
|
2022-09-02 17:22:23 +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-08-15 17:47:10 +08:00
|
|
|
const menu = (
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item key="1">foo</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
render(<DropdownButton overlay={menu} />);
|
2022-09-02 17:22:23 +08:00
|
|
|
expect('open' in dropdownProps).toBe(false);
|
2022-08-15 17:47:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support href like Button', () => {
|
|
|
|
const menu = (
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item key="1">foo</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
const { asFragment } = render(<DropdownButton overlay={menu} href="https://ant.design" />);
|
|
|
|
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', () => {
|
|
|
|
const menu = (
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item key="1">foo</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
render(<DropdownButton mouseEnterDelay={1} mouseLeaveDelay={2} overlay={menu} />);
|
|
|
|
expect(dropdownProps.mouseEnterDelay).toBe(1);
|
|
|
|
expect(dropdownProps.mouseLeaveDelay).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support overlayClassName and overlayStyle', () => {
|
|
|
|
const menu = (
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item key="1">foo</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
const { container } = render(
|
|
|
|
<DropdownButton
|
|
|
|
overlayClassName="className"
|
|
|
|
overlayStyle={{ color: 'red' }}
|
|
|
|
overlay={menu}
|
2022-09-02 17:22:23 +08:00
|
|
|
open
|
2022-08-15 17:47:10 +08:00
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector('.ant-dropdown')?.classList).toContain('className');
|
|
|
|
expect((container.querySelector('.ant-dropdown') as HTMLElement).style.color).toContain('red');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support loading', () => {
|
|
|
|
const { container } = render(<DropdownButton overlay={<div />} loading />);
|
|
|
|
|
|
|
|
expect(container.querySelector('.ant-dropdown-button .ant-btn-loading')?.classList).toContain(
|
|
|
|
'ant-btn',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|