mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-13 15:49:10 +08:00
6759887c44
* chore: migrate to vitest * chore: update ci * fix: test correctly * test: support puppeteer * chore: update coverage * chore: update include/exclude * chore: update config * test: update incorrect tests * chore: update script * chore: update * fix: should close browser at the ended * chore: improve * fix: test cause tsc error * fix: eslint error * chore: exclude correctly * test: update snap and fix some tests * chore: update test config * fix: countup.js * fix: incorrect test * chore: update reference * test: update * fix: countup.js * fix: timeout * chore: update site test * fix: fixed countup version * chore: remove unsed code * test: update * test: update demo timeout * test: update timeout * chore: update image test * chore: update threads * fix: image/svg+xml test failed * chore: limits threads * test: update test coverage include * chore: remove jest files * chore: rename jest to vi * chore: update document * chore: fix missing @types/jsdom * chore: update coverage * chore: update snap * fix:watermark test cases are incorrect * feat: update ignore comment * test: fix test case * test: reset body scrollTop * test: clean up * test: use vi * test: update snapshot * test: update snapshot * test: fix dropdown test failed * fix: toHaveStyle cause test fail * test: improve test case * test: fix * fix: color failed, refer to https://github.com/jsdom/jsdom/pull/3560 * test: fix * test: fix * test: fix circular import * test: revert * ci: coverage failed * test: fix c8 ignore comment * chore: incorrect config * chore: fix ignore ci * test: revert svg+xml * test: fix realTimers * feat: rc-trigger should be remove * test: fix some failed test * chore: remove unused deps and configure eslint-plugin-vitest * test: update snap * chore: remove jest * test: fix lint error --------- Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: afc163 <afc163@gmail.com>
326 lines
10 KiB
TypeScript
326 lines
10 KiB
TypeScript
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
|
|
import React from 'react';
|
|
import Popconfirm from '..';
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
|
import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
|
import Button from '../../button';
|
|
|
|
describe('Popconfirm', () => {
|
|
mountTest(Popconfirm);
|
|
rtlTest(Popconfirm);
|
|
|
|
const eventObject = expect.objectContaining({
|
|
target: expect.anything(),
|
|
preventDefault: expect.any(Function),
|
|
});
|
|
|
|
beforeAll(() => {
|
|
spyElementPrototype(HTMLElement, 'offsetParent', {
|
|
get: () => ({}),
|
|
});
|
|
});
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.clearAllTimers();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('should popup Popconfirm dialog', () => {
|
|
const onOpenChange = vi.fn();
|
|
|
|
const wrapper = render(
|
|
<Popconfirm
|
|
title={<span className="popconfirm-test">Are you sure delete this task?</span>}
|
|
okText="Yes"
|
|
cancelText="No"
|
|
mouseEnterDelay={0}
|
|
mouseLeaveDelay={0}
|
|
onOpenChange={onOpenChange}
|
|
>
|
|
<span>Delete</span>
|
|
</Popconfirm>,
|
|
);
|
|
|
|
const triggerNode = wrapper.container.querySelectorAll('span')[0];
|
|
fireEvent.click(triggerNode);
|
|
expect(onOpenChange).toHaveBeenLastCalledWith(true, undefined);
|
|
expect(wrapper.container.querySelectorAll('.popconfirm-test').length).toBe(1);
|
|
|
|
fireEvent.click(triggerNode);
|
|
expect(onOpenChange).toHaveBeenLastCalledWith(false, undefined);
|
|
});
|
|
|
|
it('should show overlay when trigger is clicked', async () => {
|
|
const popconfirm = render(
|
|
<Popconfirm title="code" autoAdjustOverflow={false}>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
|
|
expect(popconfirm.container.querySelector('.ant-popover')).toBe(null);
|
|
|
|
const triggerNode = popconfirm.container.querySelectorAll('span')[0];
|
|
fireEvent.click(triggerNode);
|
|
|
|
await waitFakeTimer(100);
|
|
|
|
expect(popconfirm.container.querySelector('.ant-popover')).not.toBeNull();
|
|
expect(popconfirm.container.querySelector('.ant-popover')?.className).toContain(
|
|
'ant-popover-placement-top',
|
|
);
|
|
expect(popconfirm.container.querySelector('.ant-popover')).toMatchSnapshot();
|
|
});
|
|
|
|
it('shows content for render functions', async () => {
|
|
const makeRenderFunction = (content: string) => () => content;
|
|
|
|
const popconfirm = render(
|
|
<Popconfirm title={makeRenderFunction('some-title')}>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
|
|
expect(popconfirm.container.querySelector('.ant-popover')).toBe(null);
|
|
|
|
const triggerNode = popconfirm.container.querySelectorAll('span')[0];
|
|
fireEvent.click(triggerNode);
|
|
await waitFakeTimer(100);
|
|
|
|
expect(popconfirm.container.querySelector('.ant-popover')).not.toBe(null);
|
|
expect(popconfirm.container.querySelector('.ant-popover')?.innerHTML).toContain('some-title');
|
|
expect(popconfirm.container.querySelector('.ant-popover')).toMatchSnapshot();
|
|
});
|
|
|
|
it('should be controlled by open', () => {
|
|
vi.useFakeTimers();
|
|
const popconfirm = render(
|
|
<Popconfirm title="code">
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
|
|
expect(popconfirm.container.querySelector('.ant-popover')).toBe(null);
|
|
popconfirm.rerender(
|
|
<Popconfirm title="code" open>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
|
|
expect(popconfirm.container.querySelector('.ant-popover')).not.toBe(null);
|
|
expect(popconfirm.container.querySelector('.ant-popover')?.className).not.toContain(
|
|
'ant-popover-hidden',
|
|
);
|
|
|
|
popconfirm.rerender(
|
|
<Popconfirm title="code" open={false}>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
act(() => {
|
|
vi.runAllTimers();
|
|
});
|
|
expect(popconfirm.container.querySelector('.ant-popover')).not.toBe(null);
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('should trigger onConfirm and onCancel', async () => {
|
|
const confirm = vi.fn();
|
|
const cancel = vi.fn();
|
|
const onOpenChange = vi.fn((_, e) => {
|
|
e?.persist?.();
|
|
});
|
|
const popconfirm = render(
|
|
<Popconfirm title="code" onConfirm={confirm} onCancel={cancel} onOpenChange={onOpenChange}>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
const triggerNode = popconfirm.container.querySelector('span')!;
|
|
fireEvent.click(triggerNode);
|
|
await waitFakeTimer();
|
|
|
|
fireEvent.click(popconfirm.container.querySelector('.ant-btn-primary')!);
|
|
expect(confirm).toHaveBeenCalled();
|
|
expect(onOpenChange).toHaveBeenLastCalledWith(false, eventObject);
|
|
|
|
fireEvent.click(triggerNode);
|
|
await waitFakeTimer();
|
|
|
|
fireEvent.click(popconfirm.container.querySelector('.ant-btn')!);
|
|
expect(cancel).toHaveBeenCalled();
|
|
expect(onOpenChange).toHaveBeenLastCalledWith(false, eventObject);
|
|
});
|
|
|
|
it('should support onConfirm to return Promise', async () => {
|
|
const confirm = () =>
|
|
new Promise((res) => {
|
|
setTimeout(res, 300);
|
|
});
|
|
const onOpenChange = vi.fn((_, e) => {
|
|
e?.persist?.();
|
|
});
|
|
const popconfirm = render(
|
|
<Popconfirm title="code" onConfirm={confirm} onOpenChange={onOpenChange}>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
|
|
const triggerNode = popconfirm.container.querySelectorAll('span')[0];
|
|
fireEvent.click(triggerNode);
|
|
expect(onOpenChange).toHaveBeenCalledTimes(1);
|
|
|
|
fireEvent.click(popconfirm.container.querySelectorAll('.ant-btn')[0]);
|
|
await waitFakeTimer();
|
|
expect(onOpenChange).toHaveBeenCalledWith(false, eventObject);
|
|
});
|
|
|
|
it('should support customize icon', () => {
|
|
const popconfirm = render(
|
|
<Popconfirm title="code" icon={<span className="customize-icon">custom-icon</span>}>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
|
|
const triggerNode = popconfirm.container.querySelectorAll('span')[0];
|
|
fireEvent.click(triggerNode);
|
|
expect(popconfirm.container.querySelectorAll('.customize-icon').length).toBe(1);
|
|
});
|
|
|
|
it('should prefixCls correctly', () => {
|
|
const btnPrefixCls = 'custom-btn';
|
|
const wrapper = render(
|
|
<Popconfirm
|
|
open
|
|
title="x"
|
|
prefixCls="custom-popconfirm"
|
|
okButtonProps={{ prefixCls: btnPrefixCls }}
|
|
cancelButtonProps={{ prefixCls: btnPrefixCls }}
|
|
>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
|
|
expect(wrapper.container.querySelectorAll('.custom-popconfirm').length).toBeGreaterThan(0);
|
|
expect(wrapper.container.querySelectorAll('.custom-btn').length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should support defaultOpen', () => {
|
|
const wrapper = render(
|
|
<Popconfirm title="code" defaultOpen>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
expect(wrapper.container.querySelector('.ant-popover')).toBeTruthy();
|
|
});
|
|
|
|
it('should not open in disabled', () => {
|
|
const wrapper = render(
|
|
<Popconfirm title="code" disabled>
|
|
<span>click me</span>
|
|
</Popconfirm>,
|
|
);
|
|
const triggerNode = wrapper.container.querySelectorAll('span')[0];
|
|
fireEvent.click(triggerNode);
|
|
expect(wrapper.container.querySelector('.ant-popover')).toBeFalsy();
|
|
});
|
|
|
|
it('should be closed by pressing ESC', () => {
|
|
const onOpenChange = vi.fn((_, e) => {
|
|
e?.persist?.();
|
|
});
|
|
const wrapper = render(
|
|
<Popconfirm title="title" mouseEnterDelay={0} mouseLeaveDelay={0} onOpenChange={onOpenChange}>
|
|
<span>Delete</span>
|
|
</Popconfirm>,
|
|
);
|
|
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);
|
|
});
|
|
|
|
it('should not warn memory leaking if setState in async callback', async () => {
|
|
const error = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
const Test = () => {
|
|
const [show, setShow] = React.useState(true);
|
|
|
|
if (show) {
|
|
return (
|
|
<Popconfirm
|
|
title="will unmount"
|
|
onConfirm={() =>
|
|
new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
setShow(false);
|
|
resolve(true);
|
|
}, 300);
|
|
})
|
|
}
|
|
>
|
|
<Button className="clickTarget">Test</Button>
|
|
</Popconfirm>
|
|
);
|
|
}
|
|
return <Button>Unmounted</Button>;
|
|
};
|
|
|
|
const { container } = render(
|
|
<div>
|
|
<Test />
|
|
</div>,
|
|
);
|
|
|
|
expect(container.textContent).toEqual('Test');
|
|
|
|
fireEvent.click(container.querySelector('.clickTarget')!);
|
|
fireEvent.click(container.querySelector('.ant-btn-primary')!);
|
|
|
|
await waitFakeTimer(500);
|
|
// expect(container.textContent).toEqual('Unmounted');
|
|
expect(error).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should trigger onPopupClick', async () => {
|
|
const onPopupClick = vi.fn();
|
|
|
|
const popconfirm = render(
|
|
<Popconfirm title="pop test" onPopupClick={onPopupClick}>
|
|
<span>show me your code</span>
|
|
</Popconfirm>,
|
|
);
|
|
const triggerNode = popconfirm.container.querySelector('span')!;
|
|
fireEvent.click(triggerNode);
|
|
await waitFakeTimer();
|
|
fireEvent.click(popconfirm.container.querySelector('.ant-popover-inner-content')!);
|
|
expect(onPopupClick).toHaveBeenCalled();
|
|
});
|
|
|
|
// https://github.com/ant-design/ant-design/issues/42314
|
|
it('legacy onVisibleChange should only trigger once', async () => {
|
|
const onOpenChange = vi.fn();
|
|
const onVisibleChange = vi.fn();
|
|
|
|
const { container } = render(
|
|
<Popconfirm
|
|
title="will unmount"
|
|
onOpenChange={onOpenChange}
|
|
onVisibleChange={onVisibleChange}
|
|
>
|
|
<span className="target" />
|
|
</Popconfirm>,
|
|
);
|
|
|
|
fireEvent.click(container.querySelector('.target')!);
|
|
await waitFakeTimer();
|
|
|
|
expect(onOpenChange).toHaveBeenCalledTimes(1);
|
|
expect(onVisibleChange).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|