-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
84 lines (77 loc) · 2.21 KB
/
Copy pathvitest.setup.ts
File metadata and controls
84 lines (77 loc) · 2.21 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
import '@testing-library/dom';
import '@testing-library/jest-dom/vitest';
import { afterEach, vi } from 'vitest';
import { cleanup } from '@testing-library/react';
// Cleanup after each test
afterEach(() => {
cleanup();
});
// Only set up browser mocks if window exists (not in node environment)
if (typeof window !== 'undefined') {
// Mock window.matchMedia for tests
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// jsdom does not always provide a working localStorage in this setup; install
// a Map-backed polyfill so components that read default-profile-id etc. don't
// throw. Reset between tests via afterEach below.
const memoryStorage = (): Storage => {
const store = new Map<string, string>();
return {
get length(): number {
return store.size;
},
clear(): void {
store.clear();
},
getItem(key: string): string | null {
return store.has(key) ? (store.get(key) as string) : null;
},
setItem(key: string, value: string): void {
store.set(key, String(value));
},
removeItem(key: string): void {
store.delete(key);
},
key(index: number): string | null {
return Array.from(store.keys())[index] ?? null;
},
};
};
Object.defineProperty(window, 'localStorage', {
configurable: true,
writable: true,
value: memoryStorage(),
});
Object.defineProperty(window, 'sessionStorage', {
configurable: true,
writable: true,
value: memoryStorage(),
});
afterEach(() => {
window.localStorage.clear();
window.sessionStorage.clear();
});
}
// Mock ResizeObserver
global.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
// Mock IntersectionObserver
global.IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));