mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 16:06:28 +08:00
fix: confirm modal firing onOk event twice (#40719)
* fix: confirm modal firing onOk event twice (#40272) * fix: use loading state instead using disabled * test: update test to use mock timer --------- Co-authored-by: 二货机器人 <smith3816@gmail.com>
This commit is contained in:
parent
ed94e03705
commit
14dd2cdcdb
@ -62,7 +62,6 @@ const ActionButton: React.FC<ActionButtonProps> = (props) => {
|
||||
setLoading(true);
|
||||
returnValueOfOnOk!.then(
|
||||
(...args: any[]) => {
|
||||
setLoading(false, true);
|
||||
onInternalClose(...args);
|
||||
clickedRef.current = false;
|
||||
},
|
||||
|
@ -7,7 +7,7 @@ import * as React from 'react';
|
||||
import TestUtils from 'react-dom/test-utils';
|
||||
import type { ModalFuncProps } from '..';
|
||||
import Modal from '..';
|
||||
import { waitFakeTimer, act } from '../../../tests/utils';
|
||||
import { act, waitFakeTimer } from '../../../tests/utils';
|
||||
import ConfigProvider from '../../config-provider';
|
||||
import type { ModalFunc } from '../confirm';
|
||||
import destroyFns from '../destroyFns';
|
||||
@ -95,11 +95,8 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
||||
};
|
||||
/* eslint-enable */
|
||||
|
||||
beforeAll(() => {
|
||||
jest.useFakeTimers();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.useFakeTimers();
|
||||
(global as any).injectPromise = false;
|
||||
(global as any).rejectPromise = null;
|
||||
});
|
||||
@ -203,6 +200,39 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
||||
expect(onCancel).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should not fire twice onOk when button is pressed twice', async () => {
|
||||
let resolveFn: VoidFunction;
|
||||
const onOk = jest.fn(
|
||||
() =>
|
||||
new Promise<void>((resolve) => {
|
||||
resolveFn = resolve;
|
||||
}),
|
||||
);
|
||||
await open({
|
||||
onOk,
|
||||
});
|
||||
|
||||
// Load will not clickable
|
||||
await waitFakeTimer();
|
||||
for (let i = 0; i < 10; i += 1) {
|
||||
act(() => {
|
||||
$$('.ant-btn-primary')[0].click();
|
||||
});
|
||||
}
|
||||
expect(onOk).toHaveBeenCalledTimes(1);
|
||||
|
||||
// Resolve this promise
|
||||
resolveFn!();
|
||||
await Promise.resolve();
|
||||
|
||||
// Resolve still can not clickable
|
||||
act(() => {
|
||||
$$('.ant-btn-primary')[0].click();
|
||||
});
|
||||
|
||||
expect(onOk).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should not hide confirm when onOk return Promise.resolve', async () => {
|
||||
await open({
|
||||
onOk: () => Promise.resolve(''),
|
||||
|
Loading…
Reference in New Issue
Block a user