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:
Rafael Martins 2023-02-24 00:59:57 -08:00 committed by GitHub
parent ed94e03705
commit 14dd2cdcdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 6 deletions

View File

@ -62,7 +62,6 @@ const ActionButton: React.FC<ActionButtonProps> = (props) => {
setLoading(true);
returnValueOfOnOk!.then(
(...args: any[]) => {
setLoading(false, true);
onInternalClose(...args);
clickedRef.current = false;
},

View File

@ -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(''),