Skip to content

Commit b94c16f

Browse files
committed
mock matchMedia and cookies
1 parent cc94b6a commit b94c16f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/nthul/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default defineConfig({
88
test: {
99
environment: "jsdom",
1010
globals: true,
11-
setupFiles: [],
11+
setupFiles: ["vitest.setup.ts"],
1212
coverage: {
1313
include: ["src/**"],
1414
exclude: ["src/**/index.ts", "src/**/declaration.d.ts"],

lib/nthul/vitest.setup.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { vi } from "vitest";
2+
3+
// mock matchMedia
4+
Object.defineProperty(window, "matchMedia", {
5+
writable: true,
6+
value: vi.fn().mockImplementation((query: string) => ({
7+
matches: query.includes(window.media),
8+
media: query,
9+
onchange: null,
10+
addEventListener: vi.fn(),
11+
removeEventListener: vi.fn(),
12+
dispatchEvent: vi.fn(),
13+
})),
14+
});
15+
16+
declare global {
17+
interface Window {
18+
media: "dark" | "light";
19+
}
20+
var cookies: Record<string, { value: string }>; // eslint-disable-line no-var -- let is not supported in defining global due to block scope
21+
var path: string; // eslint-disable-line no-var -- let is not supported in defining global due to block scope
22+
}
23+
Object.defineProperty(window, "media", {
24+
writable: true,
25+
value: "dark",
26+
});
27+
28+
globalThis.cookies = {};
29+
30+
vi.mock("next/headers", () => ({
31+
cookies: () => ({ get: (cookieName: string) => globalThis.cookies[cookieName] }),
32+
}));

0 commit comments

Comments
 (0)