-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathvitest.setup.js
More file actions
93 lines (79 loc) · 2.18 KB
/
vitest.setup.js
File metadata and controls
93 lines (79 loc) · 2.18 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
import '@testing-library/jest-dom';
// jsdom環境でのモック設定
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// window.scrollのモック
window.scroll = vi.fn();
// TouchEventのモック
class TouchEventMock {
constructor(type, options = {}) {
this.type = type;
this.touches = options.touches || [];
this.changedTouches = options.changedTouches || [];
this.targetTouches = options.targetTouches || [];
}
}
global.TouchEvent = TouchEventMock;
// DeviceOrientationEventのモック
class DeviceOrientationEventMock {
constructor(type, options = {}) {
this.type = type;
this.alpha = options.alpha || null;
this.beta = options.beta || null;
this.gamma = options.gamma || null;
}
}
global.DeviceOrientationEvent = DeviceOrientationEventMock;
// ResizeObserverのモック
global.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
// IntersectionObserverのモック
global.IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
// requestAnimationFrameのモック
global.requestAnimationFrame = vi.fn((cb) => setTimeout(cb, 0));
global.cancelAnimationFrame = vi.fn();
// Element.prototype.scrollIntoViewのモック
Element.prototype.scrollIntoView = vi.fn();
// window.pageXOffsetとwindow.pageYOffsetのモック
Object.defineProperty(window, 'pageXOffset', {
writable: true,
value: 0,
});
Object.defineProperty(window, 'pageYOffset', {
writable: true,
value: 0,
});
// document.documentElementのモック
Object.defineProperty(document, 'documentElement', {
writable: true,
value: {
scrollLeft: 0,
scrollTop: 0,
},
});
// document.body.parentNodeのモック
Object.defineProperty(document.body, 'parentNode', {
writable: true,
value: {
scrollLeft: 0,
scrollTop: 0,
},
});