-
Notifications
You must be signed in to change notification settings - Fork 384
/
jest.setup.jsdom.js
91 lines (79 loc) · 2.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
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(),
})),
});