-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathjest.setup.js
More file actions
56 lines (51 loc) · 1.69 KB
/
Copy pathjest.setup.js
File metadata and controls
56 lines (51 loc) · 1.69 KB
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
// Interpolating, key-returning translation function, matching the behaviour
// tests relied on when they ran inside Electron with the i18n global mocked.
const mockT = (str, obj = {}) => {
const keys = Object.keys(obj);
let replacedStr = str;
for (const key of keys) {
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp(`{{\\s*${escapedKey}\\s*}}`, 'g');
replacedStr = replacedStr.replace(re, () => obj[key]);
}
return replacedStr;
};
jest.mock('@electron/remote', () => {
const mockI18n = {
t: jest.fn(mockT),
};
return {
getGlobal: jest.fn(() => mockI18n),
};
});
// Modules that run outside Electron fall back to requiring i18next directly,
// which is never initialized in tests.
jest.mock('i18next', () => {
const mockI18n = {
t: jest.fn(mockT),
};
mockI18n.default = mockI18n;
return mockI18n;
});
// idb-keyval needs indexedDB, which Electron's renderer provided and Node lacks.
jest.mock('idb-keyval', () => ({
get: jest.fn(() => Promise.resolve(undefined)),
set: jest.fn(() => Promise.resolve()),
del: jest.fn(() => Promise.resolve()),
update: jest.fn(() => Promise.resolve()),
clear: jest.fn(() => Promise.resolve()),
keys: jest.fn(() => Promise.resolve([])),
}));
// Node's navigator global lacks userAgentData, which Chromium provides in Electron.
const mockUAPlatform = { darwin: 'macOS', win32: 'Windows' }[process.platform] || 'Linux';
Object.defineProperty(globalThis.navigator, 'userAgentData', {
value: {
platform: mockUAPlatform,
getHighEntropyValues: () => Promise.resolve({
platform: mockUAPlatform,
platformVersion: '10.0.0',
bitness: '64',
}),
},
configurable: true,
});