-
Notifications
You must be signed in to change notification settings - Fork 412
/
Copy pathjest.setup.jsdom.js
131 lines (117 loc) · 3.06 KB
/
jest.setup.jsdom.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
require('./jest.setup.base');
require('jest-canvas-mock');
require('intersection-observer');
require('jest-fetch-mock').enableMocks();
const { Buffer } = require('buffer');
const timer = require('timers');
// vscode-jsonrpc 的 node 层需要 setImmediate 函数
global.setImmediate = timer.setImmediate;
global.Buffer = Buffer;
global.clearImmediate = timer.clearImmediate;
// packages/extension/__tests__/browser/main.thread.env.test.ts
// MainThreadEnvAPI Test Suites › can read/write text via clipboard
let text = '';
global.IS_REACT_ACT_ENVIRONMENT = true;
window.navigator = Object.assign(window.navigator, {
clipboard: {
writeText(value) {
text = value;
},
readText() {
return text;
},
},
});
// https://github.com/jsdom/jsdom/issues/1742
document.queryCommandSupported = () => {};
document.execCommand = (command, ui, value) => {
const node = window.getSelection().anchorNode;
switch (command) {
case 'insertHTML':
if (node.innerHTML) {
node.innerHTML += value;
} else {
// Text node
node.parentNode.innerHTML += value;
}
break;
case 'insertLineBreak':
node.innerHTML += '<br>';
break;
}
};
global.ElectronIpcRenderer = {
send: () => {},
removeListener: () => {},
on: () => {},
};
class MockLocalStorage {
constructor() {
this.store = {};
}
clear() {
this.store = {};
}
getItem(key) {
return this.store[key] || null;
}
setItem(key, value) {
this.store[key] = value.toString();
}
removeItem(key) {
delete this.store[key];
}
}
global.localStorage = new MockLocalStorage();
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock window.crypto
Object.defineProperty(window, 'crypto', {
writable: true,
value: {
randomUUID: () =>
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
}),
getRandomValues: (array) => {
if (
!(
array instanceof Int8Array ||
array instanceof Uint8Array ||
array instanceof Int16Array ||
array instanceof Uint16Array ||
array instanceof Int32Array ||
array instanceof Uint32Array ||
array instanceof Uint8ClampedArray
)
) {
throw new TypeError('Expected a TypedArray');
}
for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256);
}
return array;
},
},
});
// Mock window.CSS
Object.defineProperty(window, 'CSS', {
writable: true,
value: {
escape: jest.fn((str) => str),
},
});