-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.frontend.ts
More file actions
89 lines (78 loc) · 2.04 KB
/
vitest.setup.frontend.ts
File metadata and controls
89 lines (78 loc) · 2.04 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
import '@testing-library/jest-dom/vitest'
import { cleanup } from '@testing-library/react'
import { afterEach, beforeAll, beforeEach, vi } from 'vitest'
import { initI18n } from '@/lib/i18n'
// jsdom can expose different Event/CustomEvent constructors on globalThis and window.
// Radix dispatches globalThis.CustomEvent instances through DOM nodes, so align both.
function syncDomEventConstructors() {
if (typeof window === 'undefined') {
return
}
Object.defineProperty(globalThis, 'Event', {
configurable: true,
writable: true,
value: window.Event,
})
Object.defineProperty(globalThis, 'CustomEvent', {
configurable: true,
writable: true,
value: window.CustomEvent,
})
}
afterEach(() => {
cleanup()
})
beforeAll(async () => {
await initI18n('de')
syncDomEventConstructors()
if (typeof window === 'undefined') {
return
}
if (!window.matchMedia) {
Object.defineProperty(window, 'matchMedia', {
configurable: true,
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
})
}
if (!window.ResizeObserver) {
class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
Object.defineProperty(window, 'ResizeObserver', {
configurable: true,
writable: true,
value: ResizeObserver,
})
}
if (!window.IntersectionObserver) {
class IntersectionObserver {
observe() {}
unobserve() {}
disconnect() {}
takeRecords() {
return []
}
}
Object.defineProperty(window, 'IntersectionObserver', {
configurable: true,
writable: true,
value: IntersectionObserver,
})
}
})
// Some tests intentionally reset globals, so restore the jsdom event constructors per test.
beforeEach(() => {
syncDomEventConstructors()
})