blob: 952561d5e77263843f80198bc31ea7408f123516 [file] [log] [blame]
86133aaa3f5d2025-04-20 21:33:29 +08001const localStorageMock = {
2 getItem: jest.fn(),
3 setItem: jest.fn(),
4 removeItem: jest.fn(),
5 clear: jest.fn(),
6};
7
8global.localStorage = localStorageMock;
9
10Object.defineProperty(URL, 'createObjectURL', {
11 writable: true,
12 value: jest.fn(),
13});
14
15class Worker {
16 constructor(stringUrl) {
17 this.url = stringUrl;
18 this.onmessage = () => {};
19 }
20
21 postMessage(msg) {
22 this.onmessage(msg);
23 }
24}
25window.Worker = Worker;
26
27/* eslint-disable global-require */
28if (typeof window !== 'undefined') {
29 // ref: https://github.com/ant-design/ant-design/issues/18774
30 if (!window.matchMedia) {
31 Object.defineProperty(global.window, 'matchMedia', {
32 writable: true,
33 configurable: true,
34 value: jest.fn(() => ({
35 matches: false,
36 addListener: jest.fn(),
37 removeListener: jest.fn(),
38 })),
39 });
40 }
41 if (!window.matchMedia) {
42 Object.defineProperty(global.window, 'matchMedia', {
43 writable: true,
44 configurable: true,
45 value: jest.fn((query) => ({
46 matches: query.includes('max-width'),
47 addListener: jest.fn(),
48 removeListener: jest.fn(),
49 })),
50 });
51 }
52}
53const errorLog = console.error;
54Object.defineProperty(global.window.console, 'error', {
55 writable: true,
56 configurable: true,
57 value: (...rest) => {
58 const logStr = rest.join('');
59 if (logStr.includes('Warning: An update to %s inside a test was not wrapped in act(...)')) {
60 return;
61 }
62 errorLog(...rest);
63 },
64});