-
Notifications
You must be signed in to change notification settings - Fork 4
/
browserMock.js
165 lines (153 loc) · 4.37 KB
/
browserMock.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-useless-constructor */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { fetch as fetchPolyfill } from 'whatwg-fetch'
import fs from 'fs/promises'
import { performance } from 'perf_hooks'
Object.defineProperty(document, 'queryCommandSupported', {
value: jest.fn().mockImplementation(() => true)
})
window.process = undefined
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()
}))
})
Object.defineProperty(window, 'fetch', {
value: jest.fn(async (url, options) => {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (url.startsWith('file:')) {
const content = await fs.readFile(new URL(url).pathname)
return {
json: async () => JSON.stringify(JSON.parse(content.toString())),
arrayBuffer: async () => content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength),
status: 200
}
} else {
return fetchPolyfill(url, options)
}
})
})
Object.defineProperty(URL, 'createObjectURL', {
value: jest.fn((blob) => {
return 'blob:not-working'
})
})
Object.defineProperty(window, 'Worker', {
value: class Worker {
constructor (stringUrl) {}
postMessage (msg) {}
terminate () {}
removeEventListener () {}
}
})
Object.defineProperty(window, 'ResizeObserver', {
value: class ResizeObserver {
constructor (stringUrl) {}
observe () {}
}
})
// These 2 classes come from https://gist.github.com/Yaffle/5458286
Object.defineProperty(window, 'TextEncoder', {
value: class TextEncoder {
encode (string) {
const octets = []
const length = string.length
let i = 0
while (i < length) {
const codePoint = string.codePointAt(i)
let c = 0
let bits = 0
if (codePoint <= 0x0000007F) {
c = 0
bits = 0x00
} else if (codePoint <= 0x000007FF) {
c = 6
bits = 0xC0
} else if (codePoint <= 0x0000FFFF) {
c = 12
bits = 0xE0
} else if (codePoint <= 0x001FFFFF) {
c = 18
bits = 0xF0
}
octets.push(bits | (codePoint >> c))
c -= 6
while (c >= 0) {
octets.push(0x80 | ((codePoint >> c) & 0x3F))
c -= 6
}
i += codePoint >= 0x10000 ? 2 : 1
}
return Uint8Array.from(octets)
}
}
})
Object.defineProperty(window, 'TextDecoder', {
value: class TextDecoder {
decode (octets) {
if (octets == null) {
return ''
}
let string = ''
let i = 0
while (i < octets.length) {
let octet = octets[i]
let bytesNeeded = 0
let codePoint = 0
if (octet <= 0x7F) {
bytesNeeded = 0
codePoint = octet & 0xFF
} else if (octet <= 0xDF) {
bytesNeeded = 1
codePoint = octet & 0x1F
} else if (octet <= 0xEF) {
bytesNeeded = 2
codePoint = octet & 0x0F
} else if (octet <= 0xF4) {
bytesNeeded = 3
codePoint = octet & 0x07
}
if (octets.length - i - bytesNeeded > 0) {
let k = 0
while (k < bytesNeeded) {
octet = octets[i + k + 1]
codePoint = (codePoint << 6) | (octet & 0x3F)
k += 1
}
} else {
codePoint = 0xFFFD
bytesNeeded = octets.length - i
}
string += String.fromCodePoint(codePoint)
i += bytesNeeded + 1
}
return string
}
}
})
Object.defineProperty(window, 'Buffer', {
value: undefined
})
// Force override performance, for some reason the implementation is empty otherwise
const _performance = performance
// remove nodeTiming because otherwise VSCode refuse to detect the env as a browser env, and it also fails to detect a node env (no `process`) so it generates an error
performance.nodeTiming = undefined
Object.defineProperty(global, 'performance', {
get () { return _performance },
set (v) {
// ignore
}
})
global.CSS = {
escape: v => v
}
Element.prototype.scrollIntoView = jest.fn()