Repository navigation
Expand file tree
/
Copy pathtest-setup.js
More file actions
190 lines (158 loc) · 4.28 KB
/
Copy pathtest-setup.js
File metadata and controls
190 lines (158 loc) · 4.28 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*!
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { createPinia, setActivePinia } from 'pinia'
import { vi } from 'vitest'
import { mockedCapabilities } from './__mocks__/capabilities.ts'
import 'regenerator-runtime/runtime'
vi.mock('extendable-media-recorder', () => ({
MediaRecorder: vi.fn(),
register: vi.fn(),
}))
vi.mock('extendable-media-recorder-wav-encoder', () => ({
connect: vi.fn(),
}))
vi.mock('@nextcloud/dialogs', () => ({
showInfo: vi.fn(),
showSuccess: vi.fn(),
showError: vi.fn(),
showWarning: vi.fn(),
TOAST_PERMANENT_TIMEOUT: -1,
}))
vi.mock('@nextcloud/files', () => ({
validateFileName: vi.fn(),
formatFileSize: vi.fn((size) => `${size} B`),
}))
vi.mock('@nextcloud/files/dav', () => ({
defaultRemoteURL: () => 'https://nextcloud.local/remote.php/dav',
}))
vi.mock('@nextcloud/initial-state', () => ({
loadState: vi.fn().mockImplementation((app, key, fallback) => {
return fallback
}),
}))
vi.mock('@nextcloud/upload', () => ({
getUploader: vi.fn(),
}))
vi.mock('@nextcloud/capabilities', () => ({
getCapabilities: vi.fn(() => mockedCapabilities),
}))
vi.mock('@sapphi-red/web-noise-suppressor', () => ({
loadRnnoise: vi.fn(),
RnnoiseWorkletNode: vi.fn(),
}))
HTMLAudioElement.prototype.setSinkId = vi.fn()
window._oc_webroot = '/nc-webroot' // used by getRootUrl() | since @nextcloud/router 2.2.1
global.appName = 'spreed'
global.OC = {
requestToken: '123',
coreApps: [
'core',
],
config: {
modRewriteWorking: true,
},
dialogs: {
},
isUserAdmin() {
return true
},
getLanguage() {
return 'en-GB'
},
getLocale() {
return 'en_GB'
},
MimeType: {
getIconUrl: vi.fn(),
},
PERMISSION_NONE: 0,
PERMISSION_READ: 1,
PERMISSION_UPDATE: 2,
PERMISSION_CREATE: 4,
PERMISSION_DELETE: 8,
PERMISSION_SHARE: 16,
PERMISSION_ALL: 31,
}
global.OCA = {
Talk: {
},
}
global.OCP = {
Accessibility: {
disableKeyboardShortcuts: () => false,
},
}
global.IS_DESKTOP = false
/** Mock toDataUrl() for jsdom */
HTMLCanvasElement.prototype.toDataURL = vi.fn((type = 'image/png') => `data:${type};base64,AAAA`)
/**
* Mock OffscreenCanvas for jsdom.
* Methods are defined on the prototype, as feature detection relies on it.
* Spy on the prototype methods to adjust the behaviour in tests.
*/
global.OffscreenCanvas = class OffscreenCanvas {
constructor(width, height) {
this.width = width
this.height = height
}
getContext() {
return { fillRect: vi.fn(), drawImage: vi.fn() }
}
convertToBlob({ type } = {}) {
return Promise.resolve(new Blob([], { type }))
}
}
/**
* Polyfill for Blob.prototype.arrayBuffer
* Required as jsdom breaks Nodejs's native Blob
*
* @see https://github.com/jsdom/jsdom/issues/2555
*/
function myArrayBuffer() {
// this: File or Blob
return new Promise((resolve) => {
const fr = new FileReader()
fr.onload = () => {
resolve(fr.result)
}
fr.readAsArrayBuffer(this)
})
}
global.Blob.prototype.arrayBuffer = Blob.prototype.arrayBuffer || myArrayBuffer
global.BroadcastChannel = vi.fn(class {
postMessage = vi.fn()
addEventListener = vi.fn()
})
global.IntersectionObserver = vi.fn(class {
observe = vi.fn()
unobserve = vi.fn()
disconnect = vi.fn()
})
global.ResizeObserver = vi.fn(class {
observe = vi.fn()
unobserve = vi.fn()
disconnect = vi.fn()
})
global.structuredClone = vi.fn((val) => JSON.parse(JSON.stringify(val)))
// Work around missing "URL.createObjectURL" (which is used in the code but not
// relevant for the tests) in jsdom: https://github.com/jsdom/jsdom/issues/1721
window.URL.createObjectURL = vi.fn()
window.URL.revokeObjectURL = vi.fn()
// Make test fail on errors or warnings (like a11y warning from nextcloud/vue library)
const originalWarn = global.console.warn
console.warn = function(message) {
originalWarn.apply(console, arguments)
throw (message instanceof Error ? message : new Error(message))
}
const originalError = global.console.error
console.error = function(message) {
originalError.apply(console, arguments)
throw (message instanceof Error ? message : new Error(message))
}
// Disable console.debug messages for the sake of cleaner test output
// Comment this line if required to see debug messages locally
console.debug = vi.fn()
// Set up Pinia for state management in tests
setActivePinia(createPinia())