mirror of
https://github.com/ant-design/ant-design.git
synced 2026-02-09 02:49:18 +08:00
test(modal): add mouseDown event before click in test (#56874)
* test(modal): add mouseDown event before click in test Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(modal): add mouseDown event before click in confirm test Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(modal): improve confirm test event firing and timer management - Add mouseDown event before click for mask interaction - Use fireEvent consistently instead of direct click() - Properly setup and cleanup fake timers Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -678,6 +678,8 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
|||||||
describe('the callback close should be a method when onCancel has a close parameter', () => {
|
describe('the callback close should be a method when onCancel has a close parameter', () => {
|
||||||
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
||||||
it(`click the close icon to trigger ${type} onCancel`, async () => {
|
it(`click the close icon to trigger ${type} onCancel`, async () => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
|
||||||
const mock = jest.fn();
|
const mock = jest.fn();
|
||||||
|
|
||||||
Modal[type]?.({
|
Modal[type]?.({
|
||||||
@@ -688,17 +690,21 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
|||||||
await waitFakeTimer();
|
await waitFakeTimer();
|
||||||
|
|
||||||
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
||||||
$$('.ant-modal-close')[0].click();
|
fireEvent.click($$('.ant-modal-close')[0]);
|
||||||
|
|
||||||
await waitFakeTimer();
|
await waitFakeTimer();
|
||||||
|
|
||||||
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
||||||
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
||||||
|
|
||||||
|
jest.useRealTimers();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
||||||
it(`press ESC to trigger ${type} onCancel`, async () => {
|
it(`press ESC to trigger ${type} onCancel`, async () => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
|
||||||
const mock = jest.fn();
|
const mock = jest.fn();
|
||||||
|
|
||||||
Modal[type]?.({
|
Modal[type]?.({
|
||||||
@@ -709,17 +715,21 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
|||||||
await waitFakeTimer();
|
await waitFakeTimer();
|
||||||
|
|
||||||
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
||||||
fireEvent.keyDown(window, { key: 'Escape' });
|
fireEvent.keyDown($$(`.ant-modal-confirm-${type}`)[0], { key: 'Escape' });
|
||||||
|
|
||||||
await waitFakeTimer(0);
|
await waitFakeTimer(0);
|
||||||
|
|
||||||
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
||||||
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
||||||
|
|
||||||
|
jest.useRealTimers();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
(['confirm', 'info', 'success', 'warning', 'error'] as const).forEach((type) => {
|
||||||
it(`click the mask to trigger ${type} onCancel`, async () => {
|
it(`click the mask to trigger ${type} onCancel`, async () => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
|
||||||
const mock = jest.fn();
|
const mock = jest.fn();
|
||||||
|
|
||||||
Modal[type]?.({
|
Modal[type]?.({
|
||||||
@@ -732,17 +742,22 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
|||||||
expect($$('.ant-modal-mask')).toHaveLength(1);
|
expect($$('.ant-modal-mask')).toHaveLength(1);
|
||||||
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
||||||
|
|
||||||
$$('.ant-modal-wrap')[0].click();
|
fireEvent.mouseDown($$('.ant-modal-wrap')[0]);
|
||||||
|
fireEvent.click($$('.ant-modal-wrap')[0]);
|
||||||
|
|
||||||
await waitFakeTimer();
|
await waitFakeTimer();
|
||||||
|
|
||||||
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(0);
|
||||||
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
||||||
|
|
||||||
|
jest.useRealTimers();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('confirm modal click Cancel button close callback is a function', async () => {
|
it('confirm modal click Cancel button close callback is a function', async () => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
|
||||||
const mock = jest.fn();
|
const mock = jest.fn();
|
||||||
|
|
||||||
Modal.confirm({
|
Modal.confirm({
|
||||||
@@ -751,13 +766,17 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
|||||||
|
|
||||||
await waitFakeTimer();
|
await waitFakeTimer();
|
||||||
|
|
||||||
$$('.ant-modal-confirm-btns > .ant-btn')[0].click();
|
fireEvent.click($$('.ant-modal-confirm-btns > .ant-btn')[0]);
|
||||||
await waitFakeTimer();
|
await waitFakeTimer();
|
||||||
|
|
||||||
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
expect(mock).toHaveBeenCalledWith(expect.any(Function));
|
||||||
|
|
||||||
|
jest.useRealTimers();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('close can close modal when onCancel has a close parameter', async () => {
|
it('close can close modal when onCancel has a close parameter', async () => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
|
||||||
Modal.confirm({
|
Modal.confirm({
|
||||||
onCancel: (close) => close(),
|
onCancel: (close) => close(),
|
||||||
});
|
});
|
||||||
@@ -766,10 +785,12 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
|||||||
|
|
||||||
expect($$('.ant-modal-confirm-confirm')).toHaveLength(1);
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(1);
|
||||||
|
|
||||||
$$('.ant-modal-confirm-btns > .ant-btn')[0].click();
|
fireEvent.click($$('.ant-modal-confirm-btns > .ant-btn')[0]);
|
||||||
await waitFakeTimer();
|
await waitFakeTimer();
|
||||||
|
|
||||||
expect($$('.ant-modal-confirm-confirm')).toHaveLength(0);
|
expect($$('.ant-modal-confirm-confirm')).toHaveLength(0);
|
||||||
|
|
||||||
|
jest.useRealTimers();
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://github.com/ant-design/ant-design/issues/37461
|
// https://github.com/ant-design/ant-design/issues/37461
|
||||||
|
|||||||
@@ -186,6 +186,7 @@ describe('Modal.hook', () => {
|
|||||||
expect(cancelCount).toEqual(1); // click cancel btn, trigger onCancel
|
expect(cancelCount).toEqual(1); // click cancel btn, trigger onCancel
|
||||||
|
|
||||||
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
|
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
|
||||||
|
fireEvent.mouseDown(document.body.querySelectorAll('.ant-modal-wrap')[0]);
|
||||||
fireEvent.click(document.body.querySelectorAll('.ant-modal-wrap')[0]);
|
fireEvent.click(document.body.querySelectorAll('.ant-modal-wrap')[0]);
|
||||||
expect(cancelCount).toEqual(2); // click modal wrapper, trigger onCancel
|
expect(cancelCount).toEqual(2); // click modal wrapper, trigger onCancel
|
||||||
});
|
});
|
||||||
@@ -322,6 +323,7 @@ describe('Modal.hook', () => {
|
|||||||
|
|
||||||
expect(document.body.querySelectorAll('.ant-modal-confirm-confirm')).toHaveLength(1);
|
expect(document.body.querySelectorAll('.ant-modal-confirm-confirm')).toHaveLength(1);
|
||||||
// Click mask to close
|
// Click mask to close
|
||||||
|
fireEvent.mouseDown(document.body.querySelectorAll('.ant-modal-wrap')[0]);
|
||||||
fireEvent.click(document.body.querySelectorAll('.ant-modal-wrap')[0]);
|
fireEvent.click(document.body.querySelectorAll('.ant-modal-wrap')[0]);
|
||||||
|
|
||||||
await waitFakeTimer();
|
await waitFakeTimer();
|
||||||
|
|||||||
Reference in New Issue
Block a user