2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2024-01-03 08:45:11 +08:00
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
import Popover from '..';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2023-02-15 10:21:28 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2020-01-02 19:10:16 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2024-01-03 08:45:11 +08:00
|
|
|
import type { TooltipRef } from '../../tooltip';
|
|
|
|
|
|
|
|
const { _InternalPanelDoNotUseOrYouWillBeFired: InternalPanelDoNotUseOrYouWillBeFired } = Popover;
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
describe('Popover', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Popover);
|
|
|
|
|
2024-03-22 10:33:56 +08:00
|
|
|
const eventObject = expect.objectContaining({
|
|
|
|
target: expect.anything(),
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
});
|
|
|
|
|
2020-09-18 13:01:36 +08:00
|
|
|
it('should show overlay when trigger is clicked', () => {
|
2024-01-03 08:45:11 +08:00
|
|
|
const ref = React.createRef<TooltipRef>();
|
|
|
|
const { container } = render(
|
2020-04-27 11:59:05 +08:00
|
|
|
<Popover ref={ref} content="console.log('hello world')" title="code" trigger="click">
|
2016-12-14 14:48:09 +08:00
|
|
|
<span>show me your code</span>
|
2018-12-07 16:17:45 +08:00
|
|
|
</Popover>,
|
2016-12-14 14:48:09 +08:00
|
|
|
);
|
2024-01-03 08:45:11 +08:00
|
|
|
expect(container.querySelector('.ant-popover-inner-content')).toBeFalsy();
|
|
|
|
fireEvent.click(container.querySelector('span')!);
|
|
|
|
expect(container.querySelector('.ant-popover-inner-content')).toBeTruthy();
|
2020-03-10 13:57:02 +08:00
|
|
|
});
|
|
|
|
|
2024-04-17 11:34:11 +08:00
|
|
|
it('should support defaultOpen', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Popover title="code" defaultOpen>
|
|
|
|
<span>show me your code</span>
|
|
|
|
</Popover>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector('.ant-popover')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2020-03-10 13:57:02 +08:00
|
|
|
it('shows content for render functions', () => {
|
|
|
|
const renderTitle = () => 'some-title';
|
|
|
|
const renderContent = () => 'some-content';
|
2024-01-03 08:45:11 +08:00
|
|
|
const ref = React.createRef<TooltipRef>();
|
|
|
|
const { container } = render(
|
2020-04-27 11:59:05 +08:00
|
|
|
<Popover ref={ref} content={renderContent} title={renderTitle} trigger="click">
|
2022-08-12 18:09:51 +08:00
|
|
|
<span>show me your code </span>
|
2020-03-10 13:57:02 +08:00
|
|
|
</Popover>,
|
|
|
|
);
|
2024-01-03 08:45:11 +08:00
|
|
|
fireEvent.click(container.querySelector('span')!);
|
2023-02-15 10:21:28 +08:00
|
|
|
const popup = document.querySelector('.ant-popover')!;
|
2020-03-10 13:57:02 +08:00
|
|
|
expect(popup).not.toBe(null);
|
|
|
|
expect(popup.innerHTML).toContain('some-title');
|
|
|
|
expect(popup.innerHTML).toContain('some-content');
|
|
|
|
expect(popup.innerHTML).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles empty title/content props safely', () => {
|
2022-09-30 10:17:49 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Popover trigger="click">
|
2020-03-10 13:57:02 +08:00
|
|
|
<span>show me your code</span>
|
|
|
|
</Popover>,
|
|
|
|
);
|
2022-09-30 10:17:49 +08:00
|
|
|
fireEvent.click(container.querySelector('span')!);
|
2020-03-10 13:57:02 +08:00
|
|
|
|
2023-05-08 19:33:34 +08:00
|
|
|
const popup = document.querySelector('.ant-popover');
|
|
|
|
expect(popup).toBe(null);
|
2022-02-07 15:28:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not render popover when the title & content props is empty', () => {
|
2022-09-30 10:17:49 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Popover trigger="click">
|
2022-02-07 15:28:46 +08:00
|
|
|
<span>show me your code</span>
|
|
|
|
</Popover>,
|
|
|
|
);
|
2022-09-30 10:17:49 +08:00
|
|
|
fireEvent.click(container.querySelector('span')!);
|
2022-02-07 15:28:46 +08:00
|
|
|
|
2023-05-08 19:33:34 +08:00
|
|
|
const popup = document.querySelector('.ant-popover');
|
|
|
|
expect(popup).toBe(null);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
2019-07-09 11:46:21 +08:00
|
|
|
|
|
|
|
it('props#overlay do not warn anymore', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2019-07-09 11:46:21 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
const overlay = jest.fn();
|
2022-04-06 11:07:15 +08:00
|
|
|
render(
|
2019-07-09 11:46:21 +08:00
|
|
|
<Popover content="console.log('hello world')" title="code" trigger="click">
|
|
|
|
<span>show me your code</span>
|
|
|
|
</Popover>,
|
|
|
|
);
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
2019-07-09 11:46:21 +08:00
|
|
|
expect(overlay).not.toHaveBeenCalled();
|
|
|
|
});
|
2020-01-02 19:10:16 +08:00
|
|
|
|
|
|
|
it(`should be rendered correctly in RTL direction`, () => {
|
2024-01-03 08:45:11 +08:00
|
|
|
const { container } = render(
|
2020-01-02 19:10:16 +08:00
|
|
|
<ConfigProvider direction="rtl">
|
2022-08-26 17:17:13 +08:00
|
|
|
<Popover title="RTL" open>
|
2020-01-02 19:10:16 +08:00
|
|
|
<span>show me your Rtl demo</span>
|
|
|
|
</Popover>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2024-01-03 08:45:11 +08:00
|
|
|
expect(Array.from<Element>(container.children)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should right work when content is null & title is null', () => {
|
|
|
|
expect(() => {
|
|
|
|
render(<InternalPanelDoNotUseOrYouWillBeFired content={null} title={null} trigger="click" />);
|
|
|
|
}).not.toThrow();
|
2020-01-02 19:10:16 +08:00
|
|
|
});
|
2024-03-22 10:33:56 +08:00
|
|
|
|
|
|
|
it('should be closed by pressing ESC', () => {
|
|
|
|
const onOpenChange = jest.fn((_, e) => {
|
|
|
|
e?.persist?.();
|
|
|
|
});
|
|
|
|
const wrapper = render(
|
|
|
|
<Popover title="Title" trigger="click" onOpenChange={onOpenChange}>
|
|
|
|
<span>Delete</span>
|
|
|
|
</Popover>,
|
|
|
|
);
|
|
|
|
const triggerNode = wrapper.container.querySelectorAll('span')[0];
|
|
|
|
fireEvent.click(triggerNode);
|
|
|
|
expect(onOpenChange).toHaveBeenLastCalledWith(true, undefined);
|
|
|
|
fireEvent.keyDown(triggerNode, { key: 'Escape', keyCode: 27 });
|
|
|
|
expect(onOpenChange).toHaveBeenLastCalledWith(false, eventObject);
|
|
|
|
});
|
2024-07-26 13:38:55 +08:00
|
|
|
|
|
|
|
it('should not display overlay when the content is null/undefined', () => {
|
|
|
|
[null, undefined].forEach((item) => {
|
|
|
|
const { container } = render(
|
|
|
|
<Popover title={() => item} content={() => item} trigger="click">
|
|
|
|
<span>show me your code</span>
|
|
|
|
</Popover>,
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector<HTMLSpanElement>('span')!);
|
|
|
|
const popup = document.querySelector('.ant-popover');
|
|
|
|
expect(popup).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|