-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
121 lines (109 loc) · 3.19 KB
/
Copy pathvitest.setup.ts
File metadata and controls
121 lines (109 loc) · 3.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
/**
* Vitest Test Setup
* Provides polyfills and global mocks for jsdom environment
*/
import { vi } from 'vitest';
import '@testing-library/jest-dom';
// Polyfill HTMLCanvasElement for jsdom
if (typeof HTMLCanvasElement === 'undefined') {
class MockHTMLCanvasElement {
width = 300;
height = 150;
getContext() {
return {
clearRect: vi.fn(),
beginPath: vi.fn(),
moveTo: vi.fn(),
lineTo: vi.fn(),
stroke: vi.fn(),
arc: vi.fn(),
fill: vi.fn(),
fillText: vi.fn(),
strokeRect: vi.fn(),
fillRect: vi.fn(),
measureText: vi.fn().mockReturnValue({ width: 10 }),
save: vi.fn(),
restore: vi.fn(),
translate: vi.fn(),
rotate: vi.fn(),
scale: vi.fn(),
setTransform: vi.fn(),
createLinearGradient: vi.fn().mockReturnValue({
addColorStop: vi.fn()
}),
createRadialGradient: vi.fn().mockReturnValue({
addColorStop: vi.fn()
}),
drawImage: vi.fn(),
getImageData: vi.fn().mockReturnValue({
data: new Uint8ClampedArray(4),
width: 1,
height: 1
}),
putImageData: vi.fn(),
canvas: { width: 300, height: 150 },
fillStyle: '',
strokeStyle: '',
lineWidth: 1,
font: '10px sans-serif',
textAlign: 'start',
textBaseline: 'alphabetic',
globalAlpha: 1
};
}
toDataURL() {
return 'data:image/png;base64,mockdata';
}
toBlob(callback: (blob: Blob | null) => void) {
callback(new Blob(['mock'], { type: 'image/png' }));
}
}
(globalThis as any).HTMLCanvasElement = MockHTMLCanvasElement;
}
// Mock requestAnimationFrame and cancelAnimationFrame
if (typeof requestAnimationFrame === 'undefined') {
(globalThis as any).requestAnimationFrame = vi.fn().mockImplementation((cb: FrameRequestCallback) => {
return setTimeout(() => cb(Date.now()), 16);
});
}
if (typeof cancelAnimationFrame === 'undefined') {
(globalThis as any).cancelAnimationFrame = vi.fn().mockImplementation((id: number) => {
clearTimeout(id);
});
}
// Mock ResizeObserver
if (typeof ResizeObserver === 'undefined') {
(globalThis as any).ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn()
}));
}
// Mock IntersectionObserver
if (typeof IntersectionObserver === 'undefined') {
(globalThis as any).IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn()
}));
}
// Mock matchMedia
if (typeof matchMedia === 'undefined') {
(globalThis as any).matchMedia = 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()
}));
}
// Mock URL.createObjectURL and revokeObjectURL
if (typeof URL.createObjectURL === 'undefined') {
URL.createObjectURL = vi.fn().mockImplementation(() => 'blob:mock-url');
}
if (typeof URL.revokeObjectURL === 'undefined') {
URL.revokeObjectURL = vi.fn();
}