-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
112 lines (106 loc) · 3.41 KB
/
Copy pathjest.setup.js
File metadata and controls
112 lines (106 loc) · 3.41 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
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
const crypto = require('crypto');
const { TextEncoder, TextDecoder } = require('util');
/** Popup E2E tests use async HiveApp init + 15s waitFor in render helpers. */
jest.setTimeout(30000);
Object.assign(global, require('jest-chrome'));
Object.assign(global, {
TextEncoder,
TextDecoder,
});
/** jest-chrome may omit `storage.local.onChanged` used by HiveAppComponent. */
if (!global.chrome?.storage?.local?.onChanged?.addListener) {
global.chrome = global.chrome || {};
global.chrome.storage = global.chrome.storage || {};
global.chrome.storage.local = global.chrome.storage.local || {};
global.chrome.storage.local.onChanged = {
addListener: jest.fn(),
removeListener: jest.fn(),
};
}
/** VaultUtils uses `chrome.storage.session`; jest-chrome does not provide it. */
if (!global.chrome?.storage?.session) {
const sessionStore = {};
global.chrome.storage.session = {
get: jest.fn((key) => {
if (typeof key === 'string') {
return Promise.resolve(
key in sessionStore ? { [key]: sessionStore[key] } : {},
);
}
const result = {};
for (const k of key) {
if (k in sessionStore) result[k] = sessionStore[k];
}
return Promise.resolve(result);
}),
set: jest.fn((items) => {
Object.assign(sessionStore, items);
return Promise.resolve();
}),
remove: jest.fn((key) => {
if (typeof key === 'string') delete sessionStore[key];
else for (const k of key) delete sessionStore[k];
return Promise.resolve();
}),
};
}
/** VaultUtils uses `runtime.connect`; jest-chrome may omit a full port. */
global.chrome = global.chrome || {};
global.chrome.runtime = global.chrome.runtime || {};
const vaultPortStub = () => ({
onMessage: { addListener: jest.fn(), removeListener: jest.fn() },
onDisconnect: { addListener: jest.fn(), removeListener: jest.fn() },
disconnect: jest.fn(),
postMessage: jest.fn(),
});
const _connect = global.chrome.runtime.connect;
global.chrome.runtime.connect = jest.fn((...args) => {
const p = typeof _connect === 'function' ? _connect(...args) : null;
if (p && p.onMessage && p.onMessage.addListener) return p;
return vaultPortStub();
});
Object.defineProperty(global.self, 'crypto', {
value: {
subtle: crypto.subtle,
getRandomValues: (arr) => crypto.randomBytes(arr.length),
randomUUID: crypto.randomUUID,
},
});
/** Default fetch: jsdom provides `fetch`, but an empty JSON body breaks hive-tx RPC (`result` missing → throw). */
const defaultFetchImpl = (_url, init) => {
const httpMethod = init?.method || 'GET';
let body = {};
if (init?.body && typeof init.body === 'string') {
try {
body = JSON.parse(init.body);
} catch {
/* ignore */
}
}
const rpcMethod = body.method;
const jsonRpc = (result) =>
Promise.resolve({
status: 200,
ok: true,
json: () => Promise.resolve({ jsonrpc: '2.0', id: 1, result }),
});
// REST (e.g. PeakD notifications): callers expect an array from res.json()
if (httpMethod === 'GET') {
return Promise.resolve({
status: 200,
ok: true,
json: () => Promise.resolve([]),
});
}
if (rpcMethod === 'condenser_api.get_accounts') {
return jsonRpc([]);
}
if (
rpcMethod === 'condenser_api.get_withdraw_routes' ||
rpcMethod === 'condenser_api.get_open_orders'
) {
return jsonRpc([]);
}
return jsonRpc({});
};
globalThis.fetch = jest.fn(defaultFetchImpl);