test: add test case

This commit is contained in:
栗嘉男 2023-07-29 16:16:14 +08:00
parent 2b42bd1664
commit d7cc34f9d8

View File

@ -372,7 +372,7 @@ describe('Modal.hook', () => {
it('should be applied correctly locale', async () => {
jest.useFakeTimers();
const Demo = ({ count }: { count: number }) => {
const Demo: React.FC<{ count: number }> = ({ count }) => {
React.useEffect(() => {
const instance = Modal.confirm({});
return () => {
@ -409,4 +409,45 @@ describe('Modal.hook', () => {
jest.useRealTimers();
});
it('support await', async () => {
jest.useFakeTimers();
let notReady = true;
let lastResult: boolean | null = null;
const Demo: React.FC = () => {
const [modal, contextHolder] = Modal.useModal();
React.useEffect(() => {
(async () => {
lastResult = await modal.confirm({
content: <Input />,
onOk: async () => {
if (notReady) {
notReady = false;
return Promise.reject();
}
},
});
})();
}, []);
return contextHolder;
};
render(<Demo />); // Wait for modal show
await waitFakeTimer(); // First time click should not close
fireEvent.click(document.querySelector('.ant-btn-primary')!);
await waitFakeTimer();
expect(lastResult).toBeFalsy(); // Second time click to close
fireEvent.click(document.querySelector('.ant-btn-primary')!);
await waitFakeTimer();
expect(lastResult).toBeTruthy();
jest.useRealTimers();
});
});