-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvitest.setup.mts
114 lines (98 loc) · 3.2 KB
/
vitest.setup.mts
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import "@testing-library/jest-dom/vitest";
import { vi } from "vitest";
if (typeof window.MediaQueryListEvent === "undefined") {
class MediaQueryListEvent extends Event {
matches: boolean;
media: string;
constructor(type: string, initDict: MediaQueryListEventInit) {
super(type, initDict);
this.matches = initDict.matches ?? false;
this.media = initDict.media ?? "";
}
}
(window as any).MediaQueryListEvent = MediaQueryListEvent;
}
globalThis.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
let currentWidth = 1024;
interface MediaQueryListMock {
matches: boolean;
media: string;
onchange: ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | null;
addListener: (listener: (ev: MediaQueryListEvent) => any) => void;
removeListener: (listener: (ev: MediaQueryListEvent) => any) => void;
addEventListener: (
type: string,
listener: (ev: MediaQueryListEvent) => any
) => void;
removeEventListener: (
type: string,
listener: (ev: MediaQueryListEvent) => any
) => void;
dispatchEvent: (event: MediaQueryListEvent) => boolean;
listeners: Array<(e: MediaQueryListEvent) => void>;
}
const mediaQueryLists = new Map<string, MediaQueryListMock>();
(globalThis as any).mediaQueryLists = mediaQueryLists;
function evalMediaQuery(query: string, width: number): boolean {
const minMatch = query.match(/\(min-width:\s*(\d+)px\)/);
const maxMatch = query.match(/\(max-width:\s*(\d+)px\)/);
let minWidth = 0;
let maxWidth = Infinity;
if (minMatch) {
minWidth = parseInt(minMatch[1], 10);
}
if (maxMatch) {
maxWidth = parseInt(maxMatch[1], 10);
}
return width >= minWidth && width <= maxWidth;
}
// @ts-expect-error This is an incomplete Mock implementation.
window.matchMedia = (query: string): MediaQueryListMock => {
if (mediaQueryLists.has(query)) return mediaQueryLists.get(query)!;
const mql: MediaQueryListMock = {
matches: evalMediaQuery(query, currentWidth),
media: query,
listeners: [],
onchange: null,
addListener(listener) {
if (listener) this.listeners.push(listener);
},
removeListener(listener) {
if (listener)
this.listeners = this.listeners.filter((l) => l !== listener);
},
addEventListener(type, listener) {
if (type === "change" && listener) this.listeners.push(listener);
},
removeEventListener(type, listener) {
if (type === "change" && listener) {
this.listeners = this.listeners.filter((l) => l !== listener);
}
},
dispatchEvent(event: MediaQueryListEvent): boolean {
this.listeners.forEach((listener) => listener.call(this, event));
return true;
},
};
mediaQueryLists.set(query, mql);
return mql;
};
globalThis.setScreenWidth = (width: number) => {
currentWidth = width;
mediaQueryLists.forEach((mql) => {
const matches = evalMediaQuery(mql.media, currentWidth);
if (mql.matches !== matches) {
mql.matches = matches;
const event = new MediaQueryListEvent("change", {
matches,
media: mql.media,
});
mql.dispatchEvent(event);
}
});
};