2017-01-15 00:27:02 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
2020-09-21 16:38:43 +08:00
|
|
|
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
|
2017-01-15 00:27:02 +08:00
|
|
|
import Popconfirm from '..';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2022-04-18 21:02:11 +08:00
|
|
|
import { sleep, render, fireEvent } from '../../../tests/utils';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2021-12-29 11:31:50 +08:00
|
|
|
import Button from '../../button';
|
2017-01-15 00:27:02 +08:00
|
|
|
|
|
|
|
describe('Popconfirm', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Popconfirm);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Popconfirm);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2018-09-08 15:32:10 +08:00
|
|
|
const eventObject = expect.objectContaining({
|
|
|
|
target: expect.anything(),
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
});
|
|
|
|
|
2020-09-21 16:38:43 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
spyElementPrototype(HTMLElement, 'offsetParent', {
|
|
|
|
get: () => ({}),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-15 00:27:02 +08:00
|
|
|
it('should popup Popconfirm dialog', () => {
|
|
|
|
const onVisibleChange = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Popconfirm
|
|
|
|
title={<span className="popconfirm-test">Are you sure delete this task?</span>}
|
|
|
|
okText="Yes"
|
|
|
|
cancelText="No"
|
|
|
|
mouseEnterDelay={0}
|
|
|
|
mouseLeaveDelay={0}
|
|
|
|
onVisibleChange={onVisibleChange}
|
|
|
|
>
|
|
|
|
<span>Delete</span>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Popconfirm>,
|
2017-01-15 00:27:02 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const triggerNode = wrapper.find('span').at(0);
|
|
|
|
triggerNode.simulate('click');
|
2018-09-08 15:32:10 +08:00
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(true, undefined);
|
2017-11-11 00:07:03 +08:00
|
|
|
expect(wrapper.find('.popconfirm-test').length).toBe(1);
|
2017-01-15 00:27:02 +08:00
|
|
|
|
|
|
|
triggerNode.simulate('click');
|
2018-09-08 15:32:10 +08:00
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(false, undefined);
|
2017-01-15 00:27:02 +08:00
|
|
|
});
|
2017-07-07 20:26:08 +08:00
|
|
|
|
2019-12-05 16:15:10 +08:00
|
|
|
it('should show overlay when trigger is clicked', async () => {
|
2020-04-26 22:24:13 +08:00
|
|
|
const ref = React.createRef();
|
2017-07-07 20:26:08 +08:00
|
|
|
const popconfirm = mount(
|
2020-04-26 22:24:13 +08:00
|
|
|
<Popconfirm ref={ref} title="code">
|
2017-07-07 20:26:08 +08:00
|
|
|
<span>show me your code</span>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Popconfirm>,
|
2017-07-07 20:26:08 +08:00
|
|
|
);
|
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
expect(ref.current.getPopupDomNode()).toBe(null);
|
2017-07-07 20:26:08 +08:00
|
|
|
|
|
|
|
popconfirm.find('span').simulate('click');
|
2019-12-05 16:15:10 +08:00
|
|
|
await sleep(100);
|
2017-07-07 20:26:08 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const popup = ref.current.getPopupDomNode();
|
2017-07-07 20:26:08 +08:00
|
|
|
expect(popup).not.toBe(null);
|
|
|
|
expect(popup.className).toContain('ant-popover-placement-top');
|
|
|
|
expect(popup.innerHTML).toMatchSnapshot();
|
|
|
|
expect(popup.innerHTML).toMatchSnapshot();
|
2020-03-10 13:57:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows content for render functions', async () => {
|
|
|
|
const makeRenderFunction = content => () => content;
|
2020-04-26 22:24:13 +08:00
|
|
|
const ref = React.createRef();
|
2020-03-10 13:57:02 +08:00
|
|
|
|
|
|
|
const popconfirm = mount(
|
2020-04-26 22:24:13 +08:00
|
|
|
<Popconfirm ref={ref} title={makeRenderFunction('some-title')}>
|
2020-03-10 13:57:02 +08:00
|
|
|
<span>show me your code</span>
|
|
|
|
</Popconfirm>,
|
|
|
|
);
|
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
expect(ref.current.getPopupDomNode()).toBe(null);
|
2020-03-10 13:57:02 +08:00
|
|
|
|
|
|
|
popconfirm.find('span').simulate('click');
|
|
|
|
await sleep(100);
|
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const popup = ref.current.getPopupDomNode();
|
2020-03-10 13:57:02 +08:00
|
|
|
expect(popup).not.toBe(null);
|
|
|
|
expect(popup.innerHTML).toContain('some-title');
|
|
|
|
expect(popup.innerHTML).toMatchSnapshot();
|
2017-07-07 20:26:08 +08:00
|
|
|
});
|
2018-05-25 20:34:53 +08:00
|
|
|
|
|
|
|
it('should be controlled by visible', () => {
|
2020-04-26 22:24:13 +08:00
|
|
|
const ref = React.createRef();
|
2018-05-25 20:34:53 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
const popconfirm = mount(
|
2020-04-26 22:24:13 +08:00
|
|
|
<Popconfirm ref={ref} title="code">
|
2018-05-25 20:34:53 +08:00
|
|
|
<span>show me your code</span>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Popconfirm>,
|
2018-05-25 20:34:53 +08:00
|
|
|
);
|
2020-04-26 22:24:13 +08:00
|
|
|
expect(ref.current.getPopupDomNode()).toBeFalsy();
|
2018-05-25 20:34:53 +08:00
|
|
|
popconfirm.setProps({ visible: true });
|
2020-04-26 22:24:13 +08:00
|
|
|
expect(ref.current.getPopupDomNode()).toBeTruthy();
|
|
|
|
expect(ref.current.getPopupDomNode().className).not.toContain('ant-popover-hidden');
|
2018-05-25 20:34:53 +08:00
|
|
|
popconfirm.setProps({ visible: false });
|
2020-04-26 22:24:13 +08:00
|
|
|
popconfirm.update(); // https://github.com/enzymejs/enzyme/issues/2305
|
2018-05-25 20:34:53 +08:00
|
|
|
jest.runAllTimers();
|
2019-12-05 16:15:10 +08:00
|
|
|
expect(popconfirm.find('Trigger').props().popupVisible).toBe(false);
|
2018-05-25 20:34:53 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger onConfirm and onCancel', () => {
|
|
|
|
const confirm = jest.fn();
|
|
|
|
const cancel = jest.fn();
|
|
|
|
const onVisibleChange = jest.fn();
|
|
|
|
const popconfirm = mount(
|
2018-12-07 20:02:01 +08:00
|
|
|
<Popconfirm
|
|
|
|
title="code"
|
|
|
|
onConfirm={confirm}
|
|
|
|
onCancel={cancel}
|
|
|
|
onVisibleChange={onVisibleChange}
|
|
|
|
>
|
2018-05-25 20:34:53 +08:00
|
|
|
<span>show me your code</span>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Popconfirm>,
|
2018-05-25 20:34:53 +08:00
|
|
|
);
|
|
|
|
const triggerNode = popconfirm.find('span').at(0);
|
|
|
|
triggerNode.simulate('click');
|
|
|
|
popconfirm.find('.ant-btn-primary').simulate('click');
|
|
|
|
expect(confirm).toHaveBeenCalled();
|
2018-09-08 15:32:10 +08:00
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(false, eventObject);
|
2018-05-25 20:34:53 +08:00
|
|
|
triggerNode.simulate('click');
|
2020-04-26 22:24:13 +08:00
|
|
|
popconfirm.find('.ant-btn').at(0).simulate('click');
|
2018-05-25 20:34:53 +08:00
|
|
|
expect(cancel).toHaveBeenCalled();
|
2018-09-08 15:32:10 +08:00
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(false, eventObject);
|
2018-05-25 20:34:53 +08:00
|
|
|
});
|
2018-07-06 23:48:37 +08:00
|
|
|
|
2021-06-09 12:18:52 +08:00
|
|
|
it('should support onConfirm to return Promise', async () => {
|
2021-11-26 15:19:31 +08:00
|
|
|
const confirm = () =>
|
|
|
|
new Promise(res => {
|
|
|
|
setTimeout(res, 300);
|
|
|
|
});
|
2021-06-09 12:18:52 +08:00
|
|
|
const onVisibleChange = jest.fn();
|
|
|
|
const popconfirm = mount(
|
|
|
|
<Popconfirm title="code" onConfirm={confirm} onVisibleChange={onVisibleChange}>
|
|
|
|
<span>show me your code</span>
|
|
|
|
</Popconfirm>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const triggerNode = popconfirm.find('span').at(0);
|
|
|
|
triggerNode.simulate('click');
|
|
|
|
expect(onVisibleChange).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
popconfirm.find('.ant-btn').at(0).simulate('click');
|
|
|
|
await sleep(400);
|
|
|
|
expect(onVisibleChange).toHaveBeenCalledWith(false, eventObject);
|
|
|
|
});
|
|
|
|
|
2018-07-06 23:48:37 +08:00
|
|
|
it('should support customize icon', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Popconfirm title="code" icon={<span className="customize-icon">custom-icon</span>}>
|
|
|
|
<span>show me your code</span>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Popconfirm>,
|
2018-07-06 23:48:37 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const triggerNode = wrapper.find('span').at(0);
|
|
|
|
triggerNode.simulate('click');
|
|
|
|
expect(wrapper.find('.customize-icon').length).toBe(1);
|
|
|
|
});
|
2018-10-19 11:13:29 +08:00
|
|
|
|
|
|
|
it('should prefixCls correctly', () => {
|
|
|
|
const btnPrefixCls = 'custom-btn';
|
|
|
|
const wrapper = mount(
|
|
|
|
<Popconfirm
|
|
|
|
visible
|
|
|
|
title="x"
|
|
|
|
prefixCls="custom-popconfirm"
|
|
|
|
okButtonProps={{ prefixCls: btnPrefixCls }}
|
|
|
|
cancelButtonProps={{ prefixCls: btnPrefixCls }}
|
|
|
|
>
|
|
|
|
<span>show me your code</span>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Popconfirm>,
|
2018-10-19 11:13:29 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.find('.custom-popconfirm').length).toBeGreaterThan(0);
|
|
|
|
expect(wrapper.find('.custom-btn').length).toBeGreaterThan(0);
|
|
|
|
});
|
2018-10-19 23:57:16 +08:00
|
|
|
|
|
|
|
it('should support defaultVisible', () => {
|
2020-04-26 22:24:13 +08:00
|
|
|
const ref = React.createRef();
|
|
|
|
mount(
|
|
|
|
<Popconfirm ref={ref} title="code" defaultVisible>
|
2018-10-19 23:57:16 +08:00
|
|
|
<span>show me your code</span>
|
2018-12-07 20:02:01 +08:00
|
|
|
</Popconfirm>,
|
2018-10-19 23:57:16 +08:00
|
|
|
);
|
2020-04-26 22:24:13 +08:00
|
|
|
expect(ref.current.getPopupDomNode()).toBeTruthy();
|
2018-10-19 23:57:16 +08:00
|
|
|
});
|
2019-06-05 20:33:00 +08:00
|
|
|
|
|
|
|
it('should not open in disabled', () => {
|
2020-04-26 22:24:13 +08:00
|
|
|
const ref = React.createRef();
|
|
|
|
|
2019-06-05 20:33:00 +08:00
|
|
|
const popconfirm = mount(
|
2020-04-26 22:24:13 +08:00
|
|
|
<Popconfirm ref={ref} title="code" disabled>
|
2019-06-06 01:47:20 +08:00
|
|
|
<span>click me</span>
|
2019-06-05 20:33:00 +08:00
|
|
|
</Popconfirm>,
|
|
|
|
);
|
2019-06-06 01:47:20 +08:00
|
|
|
const triggerNode = popconfirm.find('span').at(0);
|
2019-06-05 20:33:00 +08:00
|
|
|
triggerNode.simulate('click');
|
2020-04-26 22:24:13 +08:00
|
|
|
expect(ref.current.getPopupDomNode()).toBeFalsy();
|
2019-06-05 20:33:00 +08:00
|
|
|
});
|
2020-05-25 17:43:59 +08:00
|
|
|
|
|
|
|
it('should be closed by pressing ESC', () => {
|
|
|
|
const onVisibleChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Popconfirm
|
|
|
|
title="title"
|
|
|
|
mouseEnterDelay={0}
|
|
|
|
mouseLeaveDelay={0}
|
|
|
|
onVisibleChange={onVisibleChange}
|
|
|
|
>
|
|
|
|
<span>Delete</span>
|
|
|
|
</Popconfirm>,
|
|
|
|
);
|
|
|
|
const triggerNode = wrapper.find('span').at(0);
|
|
|
|
triggerNode.simulate('click');
|
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(true, undefined);
|
|
|
|
triggerNode.simulate('keydown', { key: 'Escape', keyCode: 27 });
|
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(false, eventObject);
|
|
|
|
});
|
2021-12-29 11:31:50 +08:00
|
|
|
|
|
|
|
it('should not warn memory leaking if setState in async callback', async () => {
|
|
|
|
const error = jest.spyOn(console, 'error');
|
|
|
|
|
|
|
|
const Test = () => {
|
|
|
|
const [show, setShow] = React.useState(true);
|
|
|
|
|
|
|
|
if (show) {
|
|
|
|
return (
|
|
|
|
<Popconfirm
|
|
|
|
title="will unmount"
|
|
|
|
onConfirm={() =>
|
|
|
|
new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
setShow(false);
|
|
|
|
resolve();
|
|
|
|
}, 300);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Button className="clickTarget">Test</Button>
|
|
|
|
</Popconfirm>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return <Button>Unmounted</Button>;
|
|
|
|
};
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
const { container } = render(
|
2021-12-29 11:31:50 +08:00
|
|
|
<div>
|
|
|
|
<Test />
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
expect(container.textContent).toEqual('Test');
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('.clickTarget'));
|
|
|
|
fireEvent.click(container.querySelector('.ant-btn-primary'));
|
|
|
|
|
2021-12-29 11:31:50 +08:00
|
|
|
await sleep(500);
|
2022-04-06 11:07:15 +08:00
|
|
|
expect(container.textContent).toEqual('Unmounted');
|
2021-12-29 11:31:50 +08:00
|
|
|
expect(error).not.toHaveBeenCalled();
|
|
|
|
});
|
2017-01-15 00:27:02 +08:00
|
|
|
});
|