Repository navigation
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
74 lines (66 loc) · 2.2 KB
/
Copy pathvitest.setup.ts
File metadata and controls
74 lines (66 loc) · 2.2 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';
import { expect, afterEach, vi } from 'vitest';
import { cleanup } from '@testing-library/react';
import * as matchers from '@testing-library/jest-dom/matchers';
// Extend Vitest's expect method with methods from react-testing-library
expect.extend(matchers);
// Clean up after each test case (e.g. clearing jsdom)
afterEach(() => {
cleanup();
});
// Mock window.matchMedia for components that use it
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(),
})),
});
// Mock ResizeObserver. Defined as a real class (not an arrow-returning vi.fn)
// because @floating-ui/dom's autoUpdate calls `new ResizeObserver(...)`, and a
// spy backed by an arrow implementation throws "is not a constructor" under new.
class ResizeObserverMock {
observe = vi.fn();
unobserve = vi.fn();
disconnect = vi.fn();
}
global.ResizeObserver = ResizeObserverMock as unknown as typeof ResizeObserver;
// Mock IntersectionObserver (used by overlays, carousel, lazy content, and
// @floating-ui/dom's autoUpdate, which likewise constructs it with `new`).
class IntersectionObserverMock {
observe = vi.fn();
unobserve = vi.fn();
disconnect = vi.fn();
takeRecords = vi.fn().mockReturnValue([]);
root = null;
rootMargin = '';
thresholds = [];
}
global.IntersectionObserver =
IntersectionObserverMock as unknown as typeof IntersectionObserver;
// jsdom does not implement layout — stub the APIs overlay/positioning code
// (popover, combobox, date-picker, menu, tooltip, dialog) relies on.
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = vi.fn();
} else {
Element.prototype.scrollIntoView = vi.fn();
}
if (!Element.prototype.getBoundingClientRect) {
Element.prototype.getBoundingClientRect = vi.fn(() => ({
width: 0,
height: 0,
top: 0,
left: 0,
bottom: 0,
right: 0,
x: 0,
y: 0,
toJSON: () => {},
})) as unknown as typeof Element.prototype.getBoundingClientRect;
}