2018-01-21 01:03:51 +08:00
|
|
|
import MockDate from 'mockdate';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ReactElement } from 'react';
|
2022-08-08 17:01:25 +08:00
|
|
|
import React, { StrictMode } from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { RenderOptions } from '@testing-library/react';
|
2022-08-05 18:27:08 +08:00
|
|
|
import { render, act } from '@testing-library/react';
|
2022-06-29 12:07:28 +08:00
|
|
|
import { _rs as onLibResize } from 'rc-resize-observer/lib/utils/observerUtil';
|
|
|
|
import { _rs as onEsResize } from 'rc-resize-observer/es/utils/observerUtil';
|
2018-01-21 01:03:51 +08:00
|
|
|
|
2022-10-19 11:50:18 +08:00
|
|
|
export function assertsExist<T>(item: T | null | undefined): asserts item is T {
|
|
|
|
expect(item).not.toBeUndefined();
|
|
|
|
expect(item).not.toBeNull();
|
|
|
|
}
|
|
|
|
|
2018-03-12 11:01:49 +08:00
|
|
|
export function setMockDate(dateString = '2017-09-18T03:30:07.795') {
|
2020-05-21 23:27:02 +08:00
|
|
|
MockDate.set(dateString);
|
2018-01-21 01:03:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function resetMockDate() {
|
|
|
|
MockDate.reset();
|
|
|
|
}
|
2019-07-24 22:56:20 +08:00
|
|
|
|
2020-06-09 18:10:43 +08:00
|
|
|
const globalTimeout = global.setTimeout;
|
|
|
|
|
2020-09-20 21:13:43 +08:00
|
|
|
export const sleep = async (timeout = 0) => {
|
|
|
|
await act(async () => {
|
2021-11-26 15:19:31 +08:00
|
|
|
await new Promise(resolve => {
|
|
|
|
globalTimeout(resolve, timeout);
|
|
|
|
});
|
2020-09-20 21:13:43 +08:00
|
|
|
});
|
|
|
|
};
|
2022-04-18 21:02:11 +08:00
|
|
|
|
|
|
|
const customRender = (ui: ReactElement, options?: Omit<RenderOptions, 'wrapper'>) =>
|
|
|
|
render(ui, { wrapper: StrictMode, ...options });
|
|
|
|
|
2022-08-08 17:01:25 +08:00
|
|
|
export function renderHook<T>(func: () => T): { result: React.RefObject<T> } {
|
|
|
|
const result = React.createRef<T>();
|
|
|
|
|
|
|
|
const Demo = () => {
|
|
|
|
(result as any).current = func();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
customRender(<Demo />);
|
|
|
|
|
|
|
|
return { result };
|
|
|
|
}
|
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
/**
|
|
|
|
* Pure render like `@testing-lib` render which will not wrap with StrictMode.
|
|
|
|
*
|
|
|
|
* Please only use with render times times of memo usage case.
|
|
|
|
*/
|
|
|
|
const pureRender = render;
|
|
|
|
|
|
|
|
export { customRender as render, pureRender };
|
2022-04-18 21:02:11 +08:00
|
|
|
|
2022-06-02 16:28:22 +08:00
|
|
|
export const triggerResize = (target: Element) => {
|
|
|
|
const originGetBoundingClientRect = target.getBoundingClientRect;
|
|
|
|
|
2022-06-29 12:07:28 +08:00
|
|
|
target.getBoundingClientRect = () => ({ width: 510, height: 903 } as DOMRect);
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
onLibResize([{ target } as ResizeObserverEntry]);
|
|
|
|
onEsResize([{ target } as ResizeObserverEntry]);
|
|
|
|
});
|
2022-06-02 16:28:22 +08:00
|
|
|
|
|
|
|
target.getBoundingClientRect = originGetBoundingClientRect;
|
2022-06-29 12:07:28 +08:00
|
|
|
};
|
2022-06-02 16:28:22 +08:00
|
|
|
|
2022-09-27 17:39:02 +08:00
|
|
|
/**
|
|
|
|
* Wait for a time delay. Will wait `advanceTime * times` ms.
|
|
|
|
*
|
|
|
|
* @param advanceTime Default 1000
|
|
|
|
* @param times Default 20
|
|
|
|
*/
|
2022-09-27 15:56:09 +08:00
|
|
|
export async function waitFakeTimer(advanceTime = 1000, times = 20) {
|
|
|
|
for (let i = 0; i < times; i += 1) {
|
2022-09-23 13:59:21 +08:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await act(async () => {
|
|
|
|
await Promise.resolve();
|
|
|
|
|
|
|
|
if (advanceTime > 0) {
|
|
|
|
jest.advanceTimersByTime(advanceTime);
|
|
|
|
} else {
|
|
|
|
jest.runAllTimers();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 21:02:11 +08:00
|
|
|
export * from '@testing-library/react';
|