-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
168 lines (141 loc) · 4.19 KB
/
vitest.setup.ts
File metadata and controls
168 lines (141 loc) · 4.19 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
import '@testing-library/jest-dom/vitest';
import { afterEach, vi } from 'vitest';
// Global cleanup after each test to prevent Tagify timer errors
// Tagify uses internal setTimeout that can fire after test teardown
afterEach(() => {
// Use fake timers briefly to clear any pending timeouts from Tagify
vi.useFakeTimers();
vi.clearAllTimers();
vi.useRealTimers();
});
class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
Object.defineProperty(globalThis, 'ResizeObserver', {
value: ResizeObserver,
});
// Mock IntersectionObserver for infinite scrolling and lazy loading tests
class MockIntersectionObserver implements IntersectionObserver {
readonly root: Element | Document | null = null;
readonly rootMargin: string = '';
readonly thresholds: ReadonlyArray<number> = [];
private callback: IntersectionObserverCallback;
private elements: Set<Element> = new Set();
constructor(callback: IntersectionObserverCallback) {
this.callback = callback;
}
observe(target: Element) {
this.elements.add(target);
// Immediately call callback with isIntersecting: true to simulate visibility
this.callback(
[
{
target,
isIntersecting: true,
intersectionRatio: 1,
boundingClientRect: target.getBoundingClientRect(),
intersectionRect: target.getBoundingClientRect(),
rootBounds: null,
time: Date.now(),
},
],
this
);
}
unobserve(target: Element) {
this.elements.delete(target);
}
disconnect() {
this.elements.clear();
}
takeRecords(): IntersectionObserverEntry[] {
return [];
}
}
Object.defineProperty(globalThis, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver,
});
// Mock matchMedia for accessibility tests (prefers-reduced-motion)
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: (query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: () => {}, // deprecated
removeListener: () => {}, // deprecated
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => true,
}),
});
// Mock hasPointerCapture for Radix UI Select compatibility
if (!Element.prototype.hasPointerCapture) {
Element.prototype.hasPointerCapture = function () {
return false;
};
}
if (!Element.prototype.setPointerCapture) {
Element.prototype.setPointerCapture = function () {
// No-op
};
}
if (!Element.prototype.releasePointerCapture) {
Element.prototype.releasePointerCapture = function () {
// No-op
};
}
// Mock scrollIntoView for Radix UI Select compatibility
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = function () {
// No-op
};
}
// Mock window.scrollTo
if (typeof window.scrollTo !== 'function') {
window.scrollTo = function () {
// No-op
};
}
// Set environment variables for consistent URL generation in tests
process.env.VITE_APP_URL = '';
process.env.APP_URL = '';
// Mock localStorage for tests
class LocalStorageMock {
private store: Record<string, string> = {};
clear() {
this.store = {};
}
getItem(key: string) {
return this.store[key] || null;
}
setItem(key: string, value: string) {
this.store[key] = value.toString();
}
removeItem(key: string) {
delete this.store[key];
}
get length() {
return Object.keys(this.store).length;
}
key(index: number) {
const keys = Object.keys(this.store);
return keys[index] || null;
}
}
global.localStorage = new LocalStorageMock() as Storage;
// Mock Clipboard API for tests
if (typeof navigator !== 'undefined') {
Object.defineProperty(navigator, 'clipboard', {
value: {
writeText: () => Promise.resolve(),
readText: () => Promise.resolve(''),
},
configurable: true,
writable: true,
});
}