-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.js
More file actions
73 lines (70 loc) · 2.14 KB
/
Copy pathvitest.setup.js
File metadata and controls
73 lines (70 loc) · 2.14 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
import '@testing-library/jest-dom/vitest';
import { vi } from 'vitest';
/**
* Mock global de IntersectionObserver — jsdom no la implementa.
* Captura el callback de cada instancia para que los tests puedan
* disparar intersecciones manualmente vía `instance.__trigger(entries)`.
*/
class MockIntersectionObserver {
constructor(callback, options = {}) {
this.callback = callback;
this.options = options;
this.elements = new Set();
this.disconnected = false;
MockIntersectionObserver.instances.push(this);
}
observe(el) {
this.elements.add(el);
}
unobserve(el) {
this.elements.delete(el);
}
disconnect() {
this.elements.clear();
this.disconnected = true;
}
takeRecords() {
return [];
}
/**
* Helper para tests: dispara el callback con entries sintéticas.
* @param {Array<{ target: Element, isIntersecting: boolean }>} entries
*/
__trigger(entries) {
const synthetic = entries.map((e) => ({
target: e.target,
isIntersecting: e.isIntersecting,
intersectionRatio: e.isIntersecting ? 1 : 0,
boundingClientRect: e.target.getBoundingClientRect(),
intersectionRect: e.target.getBoundingClientRect(),
rootBounds: null,
time: performance.now(),
}));
this.callback(synthetic, this);
}
}
MockIntersectionObserver.instances = [];
MockIntersectionObserver.reset = () => {
MockIntersectionObserver.instances.length = 0;
};
vi.stubGlobal('IntersectionObserver', MockIntersectionObserver);
globalThis.__MockIO = MockIntersectionObserver;
/**
* Mock de matchMedia — jsdom no la implementa. Por defecto NO matchea
* `prefers-reduced-motion: reduce`. Los tests que necesiten simular motion
* reducido lo cambian via `globalThis.__setReducedMotion(true)`.
*/
let __reducedMotion = false;
window.matchMedia = (query) => ({
matches: query.includes('prefers-reduced-motion: reduce') ? __reducedMotion : false,
media: query,
onchange: null,
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => false,
});
globalThis.__setReducedMotion = (value) => {
__reducedMotion = value;
};