generated from react18-tools/turborepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a97d162
commit 70a2236
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"nextjs-darkmode": patch | ||
--- | ||
|
||
Fix: FOUC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { vi } from "vitest"; | ||
|
||
const mediaListeners: (() => void)[] = []; | ||
// mock matchMedia | ||
Object.defineProperty(window, "matchMedia", { | ||
writable: true, | ||
value: vi.fn().mockImplementation((query: string) => ({ | ||
matches: query.includes(window.media), | ||
media: query, | ||
onchange() { | ||
this.matches = query.includes(window.media); | ||
mediaListeners.forEach(listener => listener()); | ||
}, | ||
addEventListener: (_: string, listener: () => void) => mediaListeners.push(listener), | ||
removeEventListener: (_: string, listener: () => void) => | ||
mediaListeners.splice(mediaListeners.indexOf(listener), 1), | ||
dispatchEvent: vi.fn(), | ||
})), | ||
}); | ||
|
||
declare global { | ||
interface Window { | ||
media: "dark" | "light"; | ||
} | ||
// skipcq: JS-0102 | ||
var cookies: Record<string, { value: string }>; // eslint-disable-line no-var -- let is not supported in defining global due to block scope | ||
} | ||
Object.defineProperty(window, "media", { | ||
writable: true, | ||
value: "dark", | ||
}); | ||
|
||
globalThis.cookies = {}; | ||
|
||
vi.mock("next/headers", () => ({ | ||
cookies: () => ({ get: (cookieName: string) => globalThis.cookies[cookieName] }), | ||
})); |