@@ -60,7 +61,7 @@ describe("Test Cascader's fire functions", () => {
* @link queryInput
*/
test('queryInput', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container, getByTestId } = render(
@@ -84,7 +85,7 @@ describe("Test Cascader's fire functions", () => {
* @link fireChange
*/
test('queryMenu', () => {
- const fn = jest.fn();
+ const fn = testFn();
render(
);
cascader.fireChange(cascader.queryMenu(document)!, 0);
expect(fn).toBeCalled();
@@ -94,7 +95,7 @@ describe("Test Cascader's fire functions", () => {
* @link queryMenuItem
*/
test('queryMenuItem', () => {
- const fn = jest.fn();
+ const fn = testFn();
render(
);
fireEvent.click(cascader.queryMenuItem(document)!);
expect(fn).toBeCalled();
@@ -104,7 +105,7 @@ describe("Test Cascader's fire functions", () => {
* @link fireOpen
*/
test('fireOpen', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
);
cascader.fireOpen(container);
expect(fn).toBeCalledTimes(1);
@@ -114,7 +115,7 @@ describe("Test Cascader's fire functions", () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
node.parentNode} options={options} />
);
@@ -124,7 +125,7 @@ describe("Test Cascader's fire functions", () => {
});
test('fireChange with hover trigger', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
* @link fireSearch
*/
test('fireSearch', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
* @link fireClear
*/
test('fireClear', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
);
@@ -171,8 +172,8 @@ describe("Test Cascader's fire functions", () => {
});
test('fireChange with multiple', () => {
- const fn1 = jest.fn();
- const fn2 = jest.fn();
+ const fn1 = testFn();
+ const fn2 = testFn();
const { container } = render(
<>
node.parentNode} options={options} />
diff --git a/src/checkbox/__tests__/checkbox.test.tsx b/src/checkbox/__tests__/checkbox.test.tsx
index 1decc03..8dcda86 100644
--- a/src/checkbox/__tests__/checkbox.test.tsx
+++ b/src/checkbox/__tests__/checkbox.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Checkbox } from 'antd';
+import { testFn } from '../../testFramework';
import * as checkbox from '..';
describe('Test checkbox', () => {
@@ -37,14 +38,14 @@ describe('Test checkbox', () => {
* @link queryInput
*/
test('queryInput', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
checkbox.fireChange(checkbox.queryInput(container)!, 0);
expect(fn).toBeCalledWith(['Apple']);
});
test('queryInput via chain', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
checkbox.queryInput(container)?.fireChange(0);
expect(fn).toBeCalledWith(['Apple']);
@@ -54,7 +55,7 @@ describe('Test checkbox', () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
checkbox.fireChange(container, 0);
expect(fn).toBeCalledWith(['Apple']);
diff --git a/src/collapse/__tests__/collapse.test.tsx b/src/collapse/__tests__/collapse.test.tsx
index 7cf316f..58507d9 100644
--- a/src/collapse/__tests__/collapse.test.tsx
+++ b/src/collapse/__tests__/collapse.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Collapse } from 'antd';
+import { testFn } from '../../testFramework';
import * as collapse from '..';
describe("Test Collapse's fire functions", () => {
@@ -9,8 +10,8 @@ describe("Test Collapse's fire functions", () => {
* @link query
*/
test('query', () => {
- const fn1 = jest.fn();
- const fn2 = jest.fn();
+ const fn1 = testFn();
+ const fn2 = testFn();
const items = [
{ key: '1', label: 'panel1', children: 'panel1' },
{ key: '2', label: 'panel2', children: 'panel2' },
@@ -59,7 +60,7 @@ describe("Test Collapse's fire functions", () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const items = [
{ key: '1', label: 'panel1', children: 'panel1' },
{ key: '2', label: 'panel2', children: 'panel2' },
diff --git a/src/colorPicker/__tests__/colorPicker.test.tsx b/src/colorPicker/__tests__/colorPicker.test.tsx
index 910b5c5..e9e9bb1 100644
--- a/src/colorPicker/__tests__/colorPicker.test.tsx
+++ b/src/colorPicker/__tests__/colorPicker.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { ColorPicker } from 'antd';
+import { testFn } from '../../testFramework';
import * as colorPicker from '..';
describe("Test ColorPicker's fire functions", () => {
@@ -69,12 +70,12 @@ describe("Test ColorPicker's fire functions", () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
colorPicker.fireOpen(container);
colorPicker.fireChange(document, { hexColor: '#000000', alpha: '30' });
// onChange will triiger twice if change hexColor and alpha at the same time
- expect(fn.mock.calls[0][1]).toBe('#000000');
+ expect(fn.mock.calls[0][1]).toBe('rgb(0,0,0)');
expect(fn.mock.calls[1][0].metaColor?.a).toBe(0.3);
});
});
diff --git a/src/datePicker/__tests__/datePicker.test.tsx b/src/datePicker/__tests__/datePicker.test.tsx
index 21e83cf..166caca 100644
--- a/src/datePicker/__tests__/datePicker.test.tsx
+++ b/src/datePicker/__tests__/datePicker.test.tsx
@@ -3,6 +3,7 @@ import { render, waitFor } from '@testing-library/react';
import { DatePicker } from 'antd';
import dayjs from 'dayjs';
+import { testFn } from '../../testFramework';
import * as datePicker from '..';
const dateAdaptor = dayjs;
@@ -40,7 +41,7 @@ describe("Test DatePicker's fire functions", () => {
* @link queryDropdown
*/
test('queryDropdown', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
* @link fireOpen
*/
test('fireOpen', async () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
datePicker.fireOpen(container);
expect(fn).toBeCalledTimes(1);
@@ -79,8 +80,8 @@ describe("Test DatePicker's fire functions", () => {
* @link fireClose
*/
test('fireClose', async () => {
- const fn1 = jest.fn();
- const fn2 = jest.fn();
+ const fn1 = testFn();
+ const fn2 = testFn();
const { container } = render( (isOpen ? fn1() : fn2())} />);
datePicker.fireOpen(container);
expect(fn1).toBeCalled();
@@ -95,7 +96,7 @@ describe("Test DatePicker's fire functions", () => {
* @link firePanelChange
*/
test('firePanelChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
});
test('firePanelChange with certain button', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
});
test('fireCalendarChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
* @link fireOk
*/
test('fireOk', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
@@ -9,14 +10,14 @@ describe("Test Drawer fire's functions", () => {
* @link fireClose
*/
test('fireClose', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
drawer.fireClose(container);
expect(fn).toBeCalled();
});
test('fireClose by icon', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
drawer.fireClose(container, { closeByMask: true });
expect(fn).toBeCalled();
diff --git a/src/dropdown/__tests__/dropdown.test.tsx b/src/dropdown/__tests__/dropdown.test.tsx
index d66ec96..73ca754 100644
--- a/src/dropdown/__tests__/dropdown.test.tsx
+++ b/src/dropdown/__tests__/dropdown.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { Dropdown } from 'antd';
+import { testFn, useFakeTimers, useRealTimers } from '../../testFramework';
import * as dropdown from '..';
const items = [
@@ -11,18 +12,18 @@ const items = [
describe("Test Dropdown's fire functions", () => {
beforeEach(() => {
- jest.useFakeTimers();
+ useFakeTimers();
});
afterEach(() => {
- jest.useRealTimers();
+ useRealTimers();
});
/**
* @link fireOpen
*/
test('test fireOpen', async () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
button
@@ -40,7 +41,7 @@ describe("Test Dropdown's fire functions", () => {
* @link fireSelect
*/
test('test fireSelect', async () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
trigger.parentElement!}>
button
@@ -55,7 +56,7 @@ describe("Test Dropdown's fire functions", () => {
* @link fireCloseWithESC
*/
test('test fireCloseWithESC', async () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
!open && fn()}>
button
@@ -89,7 +90,7 @@ describe("Test Dropdown's fire functions", () => {
* @link queryDropdownMenuItem
*/
test('test queryDropdownMenuItem', async () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
{
- jest.runAllTimers();
+ runAllTimers();
});
}
diff --git a/src/floatButton/__tests__/backTop.test.tsx b/src/floatButton/__tests__/backTop.test.tsx
index 7d0ad56..2974138 100644
--- a/src/floatButton/__tests__/backTop.test.tsx
+++ b/src/floatButton/__tests__/backTop.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { FloatButton } from 'antd';
+import { testFn } from '../../testFramework';
import * as floatButton from '..';
describe("Test FloatButton's fire functions", () => {
@@ -9,8 +10,8 @@ describe("Test FloatButton's fire functions", () => {
* @link fireClick
*/
test('test fireClick', () => {
- const fn1 = jest.fn();
- const fn2 = jest.fn();
+ const fn1 = testFn();
+ const fn2 = testFn();
const { container } = render(
<>
diff --git a/src/form/__tests__/form.test.tsx b/src/form/__tests__/form.test.tsx
index 30c217f..f8c8795 100644
--- a/src/form/__tests__/form.test.tsx
+++ b/src/form/__tests__/form.test.tsx
@@ -3,14 +3,16 @@ import { render, waitFor } from '@testing-library/react';
import { Button, Form, Input } from 'antd';
import * as button from '../../button';
+import { IContainer } from '../../interface';
+import { testFn, useFakeTimers, useRealTimers } from '../../testFramework';
import * as form from '../';
describe("Test form's functions", () => {
beforeEach(() => {
- jest.useFakeTimers();
+ useFakeTimers();
});
afterEach(() => {
- jest.useRealTimers();
+ useRealTimers();
});
/**
@@ -31,7 +33,10 @@ describe("Test form's functions", () => {
* @link queryFormItems
*/
test('queryFormItems', async () => {
- const fn = jest.fn();
+ // 暂时使用真实的 timers 来避免表单提交的时序问题
+ useRealTimers();
+
+ const fn = testFn();
const { container } = render(
@@ -42,10 +47,26 @@ describe("Test form's functions", () => {
);
- button.fireClick(form.queryFormItems(container, 1)!);
- await waitFor(() => {
- expect(fn).toBeCalledTimes(1);
- });
+
+ // 获取第二个 form item(包含提交按钮的那个)
+ const formItem = form.queryFormItems(container, 1)!;
+ expect(formItem).not.toBeNull();
+
+ // 在 form item 中查找 submit 按钮并点击它
+ const submitButton = formItem.querySelector('button[type="submit"]')!;
+ expect(submitButton).not.toBeNull();
+
+ button.fireClick(submitButton as IContainer);
+
+ await waitFor(
+ () => {
+ expect(fn).toBeCalledTimes(1);
+ },
+ { timeout: 3000 }
+ );
+
+ // 恢复 fake timers
+ useFakeTimers();
});
/**
diff --git a/src/index.ts b/src/index.ts
index 1c8551b..71d1f40 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -31,6 +31,15 @@ export * as switch from './switch';
export * as table from './table';
export * as tabs from './tabs';
export * as tag from './tag';
+export {
+ advanceTimersByTime,
+ initializeTestFramework,
+ runAllTimers,
+ spyOn,
+ testFn,
+ useFakeTimers,
+ useRealTimers,
+} from './testFramework';
export * as timePicker from './timePicker';
export * as tooltip from './tooltip';
export * as tour from './tour';
diff --git a/src/input/__tests__/input.test.tsx b/src/input/__tests__/input.test.tsx
index b059d78..618c2cf 100644
--- a/src/input/__tests__/input.test.tsx
+++ b/src/input/__tests__/input.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import { Input } from 'antd';
+import { testFn } from '../../testFramework';
import * as input from '..';
describe("Test input's fire functions", () => {
@@ -23,7 +24,7 @@ describe("Test input's fire functions", () => {
* @link querySearchButton
*/
test('querySearchButton', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
fireEvent.click(input.querySearchButton(container)!);
expect(fn).toBeCalled();
@@ -33,7 +34,7 @@ describe("Test input's fire functions", () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
input.fireChange(container, 'test');
expect(fn).toBeCalled();
@@ -43,7 +44,7 @@ describe("Test input's fire functions", () => {
* @link fireFocus
*/
test('fireFocus', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
input.fireFocus(container);
expect(fn).toBeCalled();
@@ -53,7 +54,7 @@ describe("Test input's fire functions", () => {
* @link fireBlur
*/
test('fireBlur', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
input.fireBlur(container);
expect(fn).toBeCalled();
@@ -63,7 +64,7 @@ describe("Test input's fire functions", () => {
* @link fireClear
*/
test('fireClear', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
input.fireClear(container);
expect(fn).toBeCalled();
@@ -73,7 +74,7 @@ describe("Test input's fire functions", () => {
* @link firePressEnter
*/
test('firePressEnter', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
input.firePressEnter(container);
expect(fn).toBeCalled();
@@ -83,7 +84,7 @@ describe("Test input's fire functions", () => {
* @link firePressEnter
*/
test('fireSearch', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
input.fireSearch(container);
expect(fn).toBeCalled();
diff --git a/src/input/__tests__/textarea.test.tsx b/src/input/__tests__/textarea.test.tsx
index f617d76..45f591d 100644
--- a/src/input/__tests__/textarea.test.tsx
+++ b/src/input/__tests__/textarea.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Input } from 'antd';
+import { testFn } from '../../testFramework';
import * as textarea from '../textarea';
describe("Test textarea's fire functions", () => {
@@ -23,7 +24,7 @@ describe("Test textarea's fire functions", () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
textarea.fireChange(container, 'test');
expect(fn).toBeCalled();
@@ -33,7 +34,7 @@ describe("Test textarea's fire functions", () => {
* @link fireFocus
*/
test('fireFocus', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
textarea.fireFocus(container);
expect(fn).toBeCalled();
@@ -43,7 +44,7 @@ describe("Test textarea's fire functions", () => {
* @link fireBlur
*/
test('fireBlur', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
textarea.fireBlur(container);
expect(fn).toBeCalled();
@@ -53,7 +54,7 @@ describe("Test textarea's fire functions", () => {
* @link fireClear
*/
test('fireClear', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
textarea.fireClear(container);
expect(fn).toBeCalled();
@@ -63,7 +64,7 @@ describe("Test textarea's fire functions", () => {
* @link firePressEnter
*/
test('firePressEnter', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
textarea.firePressEnter(container);
expect(fn).toBeCalled();
diff --git a/src/inputNumber/__tests__/inputNumber.test.tsx b/src/inputNumber/__tests__/inputNumber.test.tsx
index e4bcfbb..323bdf1 100644
--- a/src/inputNumber/__tests__/inputNumber.test.tsx
+++ b/src/inputNumber/__tests__/inputNumber.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { InputNumber } from 'antd';
+import { testFn } from '../../testFramework';
import * as inputNumber from '..';
describe("Test inputNumber's fire functions", () => {
@@ -31,7 +32,7 @@ describe("Test inputNumber's fire functions", () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
inputNumber.fireChange(container, 1);
@@ -42,7 +43,7 @@ describe("Test inputNumber's fire functions", () => {
* @link fireStepUp
*/
test('fireStepUp', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
inputNumber.fireStepUp(container);
@@ -53,7 +54,7 @@ describe("Test inputNumber's fire functions", () => {
* @link fireStepDown
*/
test('fireStepDown', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
inputNumber.fireStepDown(container);
@@ -64,7 +65,7 @@ describe("Test inputNumber's fire functions", () => {
* @link fireFocus
*/
test('fireFocus', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
inputNumber.fireFocus(container);
@@ -75,7 +76,7 @@ describe("Test inputNumber's fire functions", () => {
* @link fireBlur
*/
test('fireBlur', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
inputNumber.fireBlur(container);
@@ -86,7 +87,7 @@ describe("Test inputNumber's fire functions", () => {
* @link firePressEnter
*/
test('firePressEnter', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
inputNumber.firePressEnter(container);
diff --git a/src/menu/__tests__/menu.test.tsx b/src/menu/__tests__/menu.test.tsx
index 09ee006..72277bc 100644
--- a/src/menu/__tests__/menu.test.tsx
+++ b/src/menu/__tests__/menu.test.tsx
@@ -3,6 +3,7 @@ import { act, render } from '@testing-library/react';
import type { MenuProps } from 'antd';
import { Menu } from 'antd';
+import { testFn } from '../../testFramework';
import * as menu from '..';
type MenuItems = Required['items'];
@@ -12,7 +13,7 @@ describe("Test menu fire's functions", () => {
* @link fireMenuItemClick
*/
test('fire menu item click', async () => {
- const fn = jest.fn();
+ const fn = testFn();
const menuItems: MenuItems = [
{ key: 'Option1', label: 'Option1' },
{ key: 'Option2', label: 'Option2' },
@@ -27,7 +28,7 @@ describe("Test menu fire's functions", () => {
* @link fireSubMenuClick
*/
test('fire submenu click', async () => {
- const fn = jest.fn();
+ const fn = testFn();
const menuItems: MenuItems = [
{ key: 'Option1', label: 'Option1' },
{
diff --git a/src/message/__tests__/message.test.tsx b/src/message/__tests__/message.test.tsx
index bb4ee2b..9db5cdb 100644
--- a/src/message/__tests__/message.test.tsx
+++ b/src/message/__tests__/message.test.tsx
@@ -1,18 +1,21 @@
import React from 'react';
-import { render, waitFor } from '@testing-library/react';
+import { act, render, waitFor } from '@testing-library/react';
import { Button, message as Message } from 'antd';
import * as button from '../../button';
+import { testFn, useFakeTimers, useRealTimers } from '../../testFramework';
import * as message from '../index';
describe('Test Message', () => {
beforeEach(() => {
document.body.innerHTML = '';
- jest.useFakeTimers();
+ useRealTimers();
});
afterEach(() => {
- jest.useRealTimers();
- Message.destroy();
+ useRealTimers();
+ act(() => {
+ Message.destroy();
+ });
});
/**
@@ -21,16 +24,19 @@ describe('Test Message', () => {
test('query', async () => {
const { container } = render();
button.fireClick(container);
- await waitFor(async () => {
- expect(message.query(document)).not.toBeNull();
- });
+ await waitFor(
+ async () => {
+ expect(message.query(document)).not.toBeNull();
+ },
+ { timeout: 3000 }
+ );
});
/**
* @link fireClick
*/
test('fireClick', async () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
);
- button.fireClick(container);
- message.fireClick(document);
- await waitFor(async () => {
- expect(fn).toBeCalledTimes(1);
+
+ // 点击按钮触发 Message
+ act(() => {
+ button.fireClick(container);
});
+
+ // 等待 Message 渲染
+ await waitFor(
+ () => {
+ expect(message.query(document)).not.toBeNull();
+ },
+ { timeout: 3000 }
+ );
+
+ // 点击 Message
+ act(() => {
+ message.fireClick(document);
+ });
+
+ // 验证 onClick 回调被调用
+ await waitFor(
+ () => {
+ expect(fn).toBeCalledTimes(1);
+ },
+ { timeout: 3000 }
+ );
});
/**
* @link fireClose
*/
test('fireClose', async () => {
- const fn = jest.fn();
+ // 使用 fake timers 来控制时间
+ useFakeTimers();
+
+ const fn = testFn();
const { container } = render(
);
- button.fireClick(container);
- await message.fireClose(4000);
- await waitFor(async () => {
- expect(fn).toBeCalledTimes(1);
+
+ // 点击按钮触发 Message
+ act(() => {
+ button.fireClick(container);
});
+
+ // 等待 Message 渲染
+ await waitFor(
+ () => {
+ expect(message.query(document)).not.toBeNull();
+ },
+ { timeout: 3000 }
+ );
+
+ // 触发 fireClose(模拟时间推进)
+ await message.fireClose(4000);
+
+ // 验证 onClose 回调被调用
+ expect(fn).toBeCalledTimes(1);
+
+ // 恢复真实 timers
+ useRealTimers();
});
});
diff --git a/src/message/index.ts b/src/message/index.ts
index b58881f..1170faf 100644
--- a/src/message/index.ts
+++ b/src/message/index.ts
@@ -2,6 +2,7 @@ import { act, fireEvent } from '@testing-library/react';
import { IContainer } from '../interface';
import { getProvider } from '../provider';
+import { advanceTimersByTime } from '../testFramework';
import { failedQuerySelector, mixinElementWithTestFuncs, queryViaSelector } from '../utils';
const mixins = {
@@ -15,7 +16,7 @@ const mixins = {
*/
export function fireClick(container: IContainer) {
const ele = queryContent(container);
- if (!ele) throw failedQuerySelector(`.${getProvider('prefixCls')}-notice-content`);
+ if (!ele) throw failedQuerySelector(`.${getProvider('prefixCls')}-message-notice-content`);
fireEvent.click(ele);
}
@@ -29,8 +30,11 @@ export async function fireClose(duration = 3000) {
await Promise.resolve();
}
- act(() => {
- jest.advanceTimersByTime(duration);
+ // Use act to wrap timer advancement for both Jest and Vitest
+ await act(async () => {
+ advanceTimersByTime(duration);
+ // Allow React to process the timer advancement
+ await Promise.resolve();
});
}
diff --git a/src/modal/__tests__/confirm.test.tsx b/src/modal/__tests__/confirm.test.tsx
index 64f86ad..ba06da4 100644
--- a/src/modal/__tests__/confirm.test.tsx
+++ b/src/modal/__tests__/confirm.test.tsx
@@ -3,6 +3,7 @@ import { render } from '@testing-library/react';
import { Modal } from 'antd';
import { getProvider } from '../../provider';
+import { testFn, useFakeTimers, useRealTimers } from '../../testFramework';
import * as confirm from '../confirm';
function Confirm({ onOk, onCancel }: any) {
@@ -23,12 +24,12 @@ function Confirm({ onOk, onCancel }: any) {
describe("Test confirm's fire functions", () => {
beforeEach(() => {
- jest.useFakeTimers();
+ useFakeTimers();
document.body.innerHTML = '';
});
afterEach(() => {
- jest.useRealTimers();
+ useRealTimers();
});
/**
@@ -53,7 +54,7 @@ describe("Test confirm's fire functions", () => {
* @link queryCancelButton
*/
test('queryCancelButton', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { getByTestId } = render();
confirm.fireOpen(getByTestId('trigger'));
confirm.fireCancel(confirm.queryCancelButton(document)!);
@@ -64,7 +65,7 @@ describe("Test confirm's fire functions", () => {
* @link queryOkButton
*/
test('queryOkButton', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { getByTestId } = render();
confirm.fireOpen(getByTestId('trigger'));
confirm.fireOk(confirm.queryOkButton(document)!);
@@ -75,7 +76,7 @@ describe("Test confirm's fire functions", () => {
* @link fireOpen
*/
test('fireOpen', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { getByTestId } = render();
confirm.fireOpen(getByTestId('trigger'));
@@ -86,7 +87,7 @@ describe("Test confirm's fire functions", () => {
* @link fireOk
*/
test('fireOk', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { getByTestId } = render();
confirm.fireOpen(getByTestId('trigger'));
confirm.fireOk(document.body);
@@ -97,7 +98,7 @@ describe("Test confirm's fire functions", () => {
* @link fireCancel
*/
test('fireCancel', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { getByTestId } = render();
confirm.fireOpen(getByTestId('trigger'));
confirm.fireCancel(document.body);
diff --git a/src/modal/__tests__/modal.test.tsx b/src/modal/__tests__/modal.test.tsx
index ba4f8b3..795b59e 100644
--- a/src/modal/__tests__/modal.test.tsx
+++ b/src/modal/__tests__/modal.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Modal } from 'antd';
+import { testFn } from '../../testFramework';
import * as modal from '..';
describe("Test Modal fire's functions", () => {
@@ -17,7 +18,7 @@ describe("Test Modal fire's functions", () => {
* @link queryCancelButton
*/
test('queryCancelButton', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
modal.fireCancel(modal.queryCancelButton(container)!);
expect(fn).toBeCalled();
@@ -27,7 +28,7 @@ describe("Test Modal fire's functions", () => {
* @link queryOkButton
*/
test('queryOkButton', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
modal.fireOk(modal.queryOkButton(container)!);
expect(fn).toBeCalled();
@@ -37,7 +38,7 @@ describe("Test Modal fire's functions", () => {
* @link fireCancel
*/
test('fireCancel', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
modal.fireCancel(container);
expect(fn).toBeCalled();
@@ -47,7 +48,7 @@ describe("Test Modal fire's functions", () => {
* @link queryMask
*/
test('fireCancel with queryMask', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
modal.fireCancel(modal.queryMask(container)!);
expect(fn).toBeCalled();
@@ -57,7 +58,7 @@ describe("Test Modal fire's functions", () => {
* @link fireOk
*/
test('fireOk', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
modal.fireOk(container);
expect(fn).toBeCalled();
diff --git a/src/modal/confirm.ts b/src/modal/confirm.ts
index b76c4f2..7fe5719 100644
--- a/src/modal/confirm.ts
+++ b/src/modal/confirm.ts
@@ -3,18 +3,19 @@ import { act, fireEvent } from '@testing-library/react';
import * as button from '../button';
import type { IContainer } from '../interface';
import { getProvider } from '../provider';
+import { runAllTimers } from '../testFramework';
import { failedTriggerElement, queryViaSelector } from '../utils';
import * as modal from './';
/**
* Open confirm
- * @prerequisite call `jest.useFakeTimers()`
+ * @prerequisite call `useFakeTimers()` from ant-design-testing
*/
export function fireOpen(ele?: HTMLElement) {
if (!ele) throw failedTriggerElement();
fireEvent.click(ele);
act(() => {
- jest.runAllTimers();
+ runAllTimers();
});
}
diff --git a/src/notification/__tests__/notification.test.tsx b/src/notification/__tests__/notification.test.tsx
index c42e6f2..042d528 100644
--- a/src/notification/__tests__/notification.test.tsx
+++ b/src/notification/__tests__/notification.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { act, waitFor } from '@testing-library/react';
import { notification as Notification } from 'antd';
+import { testFn } from '../../testFramework';
import * as notification from '..';
describe("Test Notification's fire functions", () => {
@@ -9,7 +10,7 @@ describe("Test Notification's fire functions", () => {
* @link fireClick
*/
test('test fireClick', async () => {
- const fn = jest.fn();
+ const fn = testFn();
act(() => {
Notification.info({
message: 'This is a notification message',
@@ -32,7 +33,7 @@ describe("Test Notification's fire functions", () => {
* @link fireClose
*/
test('test fireClose', async () => {
- const fn = jest.fn();
+ const fn = testFn();
act(() => {
Notification.info({
message: 'This is a notification message',
diff --git a/src/pagination/__tests__/pagination.test.tsx b/src/pagination/__tests__/pagination.test.tsx
index 4ffac3b..444aa6b 100644
--- a/src/pagination/__tests__/pagination.test.tsx
+++ b/src/pagination/__tests__/pagination.test.tsx
@@ -2,15 +2,16 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Pagination } from 'antd';
+import { testFn, useFakeTimers, useRealTimers } from '../../testFramework';
import * as pagination from '../';
describe('Test Pagination', () => {
beforeEach(() => {
- jest.useFakeTimers();
+ useFakeTimers();
});
afterEach(() => {
- jest.useRealTimers();
+ useRealTimers();
});
/**
@@ -49,7 +50,7 @@ describe('Test Pagination', () => {
* @link fireSizeChange
*/
test('fireSizeChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
pagination.fireSizeOpen(container);
pagination.fireSizeChange(container, 1);
@@ -60,14 +61,14 @@ describe('Test Pagination', () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
pagination.fireChange(container);
expect(fn).toBeCalledWith(2, 10);
});
test('fireChange with queryPrevButton', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
pagination.fireChange(container);
expect(fn).toBeCalledWith(2, 10);
@@ -76,7 +77,7 @@ describe('Test Pagination', () => {
});
test('fireChange with specific item', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
pagination.fireChange(pagination.queryPaginationItem(container, 3)!);
expect(fn).toBeCalledWith(3, 10);
@@ -86,7 +87,7 @@ describe('Test Pagination', () => {
* @link queryJumpNext
*/
test('fireChange with jump next', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
pagination.fireChange(pagination.queryJumpNext(container)!);
expect(fn).toBeCalledWith(6, 10);
@@ -96,7 +97,7 @@ describe('Test Pagination', () => {
* @link queryJumpPrev
*/
test('fireChange with jump prev', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
pagination.fireChange(pagination.queryJumpPrev(container)!);
expect(fn).toBeCalledWith(1, 10);
@@ -106,7 +107,7 @@ describe('Test Pagination', () => {
* @link queryQuickJump
*/
test('fireChange with quick jump', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
pagination.fireChange(pagination.queryQuickJump(container)!, 20);
expect(fn).toBeCalledWith(20, 10);
diff --git a/src/popconfirm/__tests__/popconfirm.test.tsx b/src/popconfirm/__tests__/popconfirm.test.tsx
index fc51c5b..d49512b 100644
--- a/src/popconfirm/__tests__/popconfirm.test.tsx
+++ b/src/popconfirm/__tests__/popconfirm.test.tsx
@@ -3,15 +3,16 @@ import { render } from '@testing-library/react';
import { Button, Popconfirm } from 'antd';
import * as button from '../../button';
+import { testFn, useFakeTimers, useRealTimers } from '../../testFramework';
import * as confirm from '..';
describe("Test popconfirm fire's functions", () => {
beforeEach(() => {
- jest.useFakeTimers();
+ useFakeTimers();
});
afterEach(() => {
- jest.useRealTimers();
+ useRealTimers();
});
/**
@@ -44,7 +45,7 @@ describe("Test popconfirm fire's functions", () => {
* @link queryCancelButton
*/
test('queryCancelButton', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
node.parentElement!}>
@@ -59,7 +60,7 @@ describe("Test popconfirm fire's functions", () => {
* @link queryConfirmButton
*/
test('queryConfirmButton', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
node.parentElement!}>
@@ -74,7 +75,7 @@ describe("Test popconfirm fire's functions", () => {
* @link fireOpen
*/
test('fireOpen', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
node.parentElement!}>
@@ -88,7 +89,7 @@ describe("Test popconfirm fire's functions", () => {
* @link fireCancel
*/
test('fireCancel', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
node.parentElement!}>
@@ -103,7 +104,7 @@ describe("Test popconfirm fire's functions", () => {
* @link fireConfirm
*/
test('fireConfirm', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
node.parentElement!}>
diff --git a/src/popconfirm/index.ts b/src/popconfirm/index.ts
index 8fa00ac..97aaf55 100644
--- a/src/popconfirm/index.ts
+++ b/src/popconfirm/index.ts
@@ -3,6 +3,7 @@ import { act, fireEvent } from '@testing-library/react';
import * as button from '../button';
import type { IContainer } from '../interface';
import { getProvider } from '../provider';
+import { runAllTimers } from '../testFramework';
import { failedQuerySelector, failedTriggerElement, mixinElementWithTestFuncs, queryViaSelector } from '../utils';
const mixins = {
@@ -19,7 +20,7 @@ const mixins = {
*/
export function fireOpen(trigger: HTMLElement) {
if (!trigger) throw failedTriggerElement();
- jest.runAllTimers();
+ runAllTimers();
act(() => {
fireEvent.click(trigger);
});
diff --git a/src/provider/index.ts b/src/provider/index.ts
index cd3095d..eca2db3 100644
--- a/src/provider/index.ts
+++ b/src/provider/index.ts
@@ -1,5 +1,13 @@
-const globalConfig = {
+import { initializeTestFramework } from '../testFramework';
+
+export interface GlobalConfig {
+ prefixCls: string;
+ testFramework: 'jest' | 'vitest';
+}
+
+const globalConfig: GlobalConfig = {
prefixCls: 'ant',
+ testFramework: 'jest', // Default to jest for backward compatibility
};
export function getProviders() {
@@ -10,6 +18,11 @@ export function getProvider(key: keyof typeof globalConfig) {
return globalConfig[key];
}
-export function provider(opt: Partial) {
+export function provider(opt: Partial) {
Object.assign(globalConfig, opt);
+
+ // Initialize test framework when config changes
+ if (opt.testFramework) {
+ initializeTestFramework(opt.testFramework);
+ }
}
diff --git a/src/qrCode/__tests__/qrCode.test.tsx b/src/qrCode/__tests__/qrCode.test.tsx
index 2a70130..ae8f817 100644
--- a/src/qrCode/__tests__/qrCode.test.tsx
+++ b/src/qrCode/__tests__/qrCode.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { QRCode } from 'antd';
+import { testFn } from '../../testFramework';
import * as qrCode from '..';
describe("Test QRCode fire's functions", () => {
@@ -10,8 +11,8 @@ describe("Test QRCode fire's functions", () => {
const originalError = console.error;
beforeAll(() => {
- console.log = jest.fn();
- console.error = jest.fn();
+ console.log = testFn();
+ console.error = testFn();
});
afterAll(() => {
@@ -31,7 +32,7 @@ describe("Test QRCode fire's functions", () => {
* @link fireRefresh
*/
test('fireRefresh', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
qrCode.fireRefresh(container);
expect(fn).toBeCalled();
diff --git a/src/radio/__tests__/radio.test.tsx b/src/radio/__tests__/radio.test.tsx
index abc4da5..b623bee 100644
--- a/src/radio/__tests__/radio.test.tsx
+++ b/src/radio/__tests__/radio.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Radio } from 'antd';
+import { testFn } from '../../testFramework';
import * as radio from '..';
describe("Test Radio's fire functions", () => {
@@ -33,7 +34,7 @@ describe("Test Radio's fire functions", () => {
* @link fireMouseEnter
*/
test('fireMouseEnter', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(Radio);
radio.fireMouseEnter(container);
@@ -41,7 +42,7 @@ describe("Test Radio's fire functions", () => {
});
test('fireMouseEnter with group', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
A
@@ -56,7 +57,7 @@ describe("Test Radio's fire functions", () => {
* @link fireMouseLeave
*/
test('fireMouseLeave', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(Radio);
radio.fireMouseLeave(container);
@@ -67,7 +68,7 @@ describe("Test Radio's fire functions", () => {
* @link fireMouseLeave
*/
test('fireMouseLeave with group', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
A
@@ -82,7 +83,7 @@ describe("Test Radio's fire functions", () => {
* @link fireChange
*/
test('fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(
A
diff --git a/src/rate/__tests__/rate.test.tsx b/src/rate/__tests__/rate.test.tsx
index 73a4e28..290104c 100644
--- a/src/rate/__tests__/rate.test.tsx
+++ b/src/rate/__tests__/rate.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Rate } from 'antd';
+import { testFn } from '../../testFramework';
import * as rate from '..';
describe("Test rate's fire functions", () => {
@@ -9,7 +10,7 @@ describe("Test rate's fire functions", () => {
* @link fireChange
*/
test('test fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
rate.fireChange(container, 3);
expect(fn).toBeCalledWith(3);
@@ -21,7 +22,7 @@ describe("Test rate's fire functions", () => {
* @link fireHoverChange
*/
test('test fireHoverChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
rate.fireHoverChange(container, 3.5);
expect(fn).toBeCalledWith(3.5);
diff --git a/src/segmented/__tests__/segmented.test.tsx b/src/segmented/__tests__/segmented.test.tsx
index 98d1aa0..51364c6 100644
--- a/src/segmented/__tests__/segmented.test.tsx
+++ b/src/segmented/__tests__/segmented.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Segmented } from 'antd';
+import { testFn } from '../../testFramework';
import * as segmented from '..';
describe("Test Segmented's fire functions", () => {
@@ -9,7 +10,7 @@ describe("Test Segmented's fire functions", () => {
* @link fireChange
*/
test('test fireChange', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render();
segmented.fireChange(container, 1);
expect(fn).toBeCalledWith('Two');
diff --git a/src/select/__tests__/select.test.tsx b/src/select/__tests__/select.test.tsx
index 6867357..63c03d3 100644
--- a/src/select/__tests__/select.test.tsx
+++ b/src/select/__tests__/select.test.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { Select } from 'antd';
+import { testFn } from '../../testFramework';
import * as select from '..';
describe('Test Select fire functions', () => {
@@ -27,8 +28,8 @@ describe('Test Select fire functions', () => {
* @link queryInput
*/
test('queryInput', () => {
- const fn1 = jest.fn();
- const fn2 = jest.fn();
+ const fn1 = testFn();
+ const fn2 = testFn();
const { container } = render(
<>
@@ -45,8 +46,8 @@ describe('Test Select fire functions', () => {
* @link querySelectorWrapper
*/
test('querySelectorWrapper', () => {
- const fn1 = jest.fn();
- const fn2 = jest.fn();
+ const fn1 = testFn();
+ const fn2 = testFn();
const { container } = render(
<>
@@ -63,8 +64,8 @@ describe('Test Select fire functions', () => {
* @link queryDropdown
*/
test('queryDropdown', () => {
- const fn1 = jest.fn();
- const fn2 = jest.fn();
+ const fn1 = testFn();
+ const fn2 = testFn();
const { container } = render(
<>
@@ -84,7 +85,7 @@ describe('Test Select fire functions', () => {
* @link queryOption
*/
test('queryOption', () => {
- const fn = jest.fn();
+ const fn = testFn();
const { container } = render(