-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsetupTests.js
142 lines (122 loc) · 2.81 KB
/
setupTests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React from 'react';
global.React = React;
jest.mock('react', () => ({
...jest.requireActual('react'),
useLayoutEffect: jest.requireActual('react').useEffect,
}));
jest.setTimeout(60000);
/* eslint-disable global-require */
if (typeof window !== 'undefined') {
// ref: https://github.com/ant-design/ant-design/issues/18774
if (!window.matchMedia) {
Object.defineProperty(global.window, 'matchMedia', {
writable: true,
configurable: true,
value: jest.fn(() => ({
matches: false,
addListener: jest.fn(),
removeListener: jest.fn(),
})),
});
}
if (!window.matchMedia) {
Object.defineProperty(global.window, 'matchMedia', {
writable: true,
configurable: true,
value: jest.fn((query) => ({
matches: query.includes('max-width'),
addListener: jest.fn(),
removeListener: jest.fn(),
})),
});
}
}
Object.defineProperty(window, 'open', {
value: jest.fn,
});
global.URL.createObjectURL = () => ({})
global.Element.prototype.getBoundingClientRect = () => {
return { // 模拟浏览器中的函数,能生成参数即可
toJSON: () => ({
"x": 0,
"y": -928,
"width": 1470,
"height": 944,
"top": -928,
"right": 1470,
"bottom": 16,
"left": 0
}),
"x": 0,
"y": -928,
"width": 1470,
"height": 944,
"top": -928,
"right": 1470,
"bottom": 16,
"left": 0
}
}
global.requestAnimationFrame =
global.requestAnimationFrame ||
function requestAnimationFrame(cb) {
return setTimeout(cb, 0);
};
global.cancelAnimationFrame =
global.cancelAnimationFrame ||
function cancelAnimationFrame() {
return null;
};
// browserMocks.js
export const localStorageMock = (() => {
let store = {
umi_locale: 'zh-CN',
};
return {
getItem(key) {
return store[key] || null;
},
setItem(key, value) {
store[key] = value.toString();
},
removeItem(key) {
store[key] = null;
},
clear() {
store = {};
},
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
writable: true,
});
Object.defineProperty(window, 'cancelAnimationFrame', {
value: () => null,
});
// @ts-ignore-next-line
global.Worker = class {
constructor(stringUrl) {
// @ts-ignore-next-line
this.url = stringUrl;
// @ts-ignore-next-line
this.onmessage = () => {};
}
postMessage(msg) {
// @ts-ignore-next-line
this.onmessage(msg);
}
};
const errorLog = console.error;
Object.defineProperty(global.window.console, 'error', {
writable: true,
configurable: true,
value: (...rest) => {
const logStr = rest.join('');
if (logStr.includes('Warning: An update to %s inside a test was not wrapped in act(...)')) {
return;
}
errorLog(...rest);
},
});
``;