ant-design/tests/setup.ts
二货爱吃白萝卜 eff720837c
test: Update image snapshot logic (#46529)
* test: mix snapshot

* test: clean up

* test: fix order

* test: fix test logic

* test: fix reset

* test: refactor code

* test: clean fetch

* chore: lint

* test: clean up

* test: delay check

* chore: refactor fetch

* docs: update diff html

* test: add ssr support

* chore: part use ssr

* chore: update style

* chore: slice of it

* docs: fix cut
2023-12-21 10:30:08 +08:00

71 lines
2.4 KiB
TypeScript

/* eslint-disable no-console, import/prefer-default-export */
import util from 'util';
import type { DOMWindow } from 'jsdom';
// import { fillWindowEnv } from './utils';
const React = require('react');
// eslint-disable-next-line no-console
console.log('Current React Version:', React.version);
const originConsoleErr = console.error;
// Hack off React warning to avoid too large log in CI.
console.error = (...args) => {
const str = args.join('').replace(/\n/g, '');
if (
['validateDOMNesting', 'on an unmounted component', 'not wrapped in act'].every(
(warn) => !str.includes(warn),
)
) {
originConsoleErr(...args);
}
};
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
// This function can not move to external file since jest setup not support
export function fillWindowEnv(window: Window | DOMWindow) {
const win = window as Writeable<Window> & typeof globalThis;
win.resizeTo = (width, height) => {
win.innerWidth = width || win.innerWidth;
win.innerHeight = height || win.innerHeight;
win.dispatchEvent(new Event('resize'));
};
win.scrollTo = () => {};
// ref: https://github.com/ant-design/ant-design/issues/18774
if (!win.matchMedia) {
Object.defineProperty(win, 'matchMedia', {
writable: true,
configurable: true,
value: jest.fn((query) => ({
matches: query.includes('max-width'),
addListener: jest.fn(),
removeListener: jest.fn(),
})),
});
}
// Fix css-animation or rc-motion deps on these
// https://github.com/react-component/motion/blob/9c04ef1a210a4f3246c9becba6e33ea945e00669/src/util/motion.ts#L27-L35
// https://github.com/yiminghe/css-animation/blob/a5986d73fd7dfce75665337f39b91483d63a4c8c/src/Event.js#L44
win.AnimationEvent = win.AnimationEvent || win.Event;
win.TransitionEvent = win.TransitionEvent || win.Event;
// ref: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
// ref: https://github.com/jsdom/jsdom/issues/2524
Object.defineProperty(win, 'TextEncoder', { writable: true, value: util.TextEncoder });
Object.defineProperty(win, 'TextDecoder', { writable: true, value: util.TextDecoder });
}
/* eslint-disable global-require */
if (typeof window !== 'undefined') {
fillWindowEnv(window);
}
global.requestAnimationFrame = global.requestAnimationFrame || global.setTimeout;
global.cancelAnimationFrame = global.cancelAnimationFrame || global.clearTimeout;