@@ -2,12 +2,15 @@ import { act, cleanup, fireEvent, render, renderHook } from "@testing-library/re
22import { afterEach , beforeEach , describe , test } from "vitest" ;
33import { Core } from "./core" ;
44import { useMode } from "../../hooks" ;
5- import { COOKIE_KEY , DARK , LIGHT , MEDIA } from "../../constants" ;
5+ import { DARK , LIGHT } from "../../constants" ;
6+ import { s } from "./script" ;
67
8+ const STORAGE_KEY = "o" ;
79describe ( "theme-switcher" , ( ) => {
810 afterEach ( cleanup ) ;
911
1012 beforeEach ( ( ) => {
13+ s ( STORAGE_KEY ) ;
1114 render ( < Core /> ) ;
1215 } ) ;
1316
@@ -22,14 +25,14 @@ describe("theme-switcher", () => {
2225 test ( "test storing state to localStorage and DOM updates" , async ( { expect } ) => {
2326 const { result } = renderHook ( ( ) => useMode ( ) ) ;
2427 act ( ( ) => result . current . setMode ( LIGHT ) ) ;
25- expect ( localStorage . getItem ( COOKIE_KEY ) ) . toBe ( LIGHT ) ;
28+ expect ( localStorage . getItem ( STORAGE_KEY ) ) . toBe ( LIGHT ) ;
2629 expect ( document . documentElement . getAttribute ( "data-m" ) ) . toBe ( LIGHT ) ;
2730 } ) ;
2831
2932 test ( "Storage event" , async ( { expect } ) => {
3033 const hook = renderHook ( ( ) => useMode ( ) ) ;
3134 await act ( ( ) =>
32- fireEvent ( window , new StorageEvent ( "storage" , { key : COOKIE_KEY , newValue : DARK } ) ) ,
35+ fireEvent ( window , new StorageEvent ( "storage" , { key : STORAGE_KEY , newValue : DARK } ) ) ,
3336 ) ;
3437 expect ( hook . result . current . mode ) . toBe ( DARK ) ;
3538 } ) ;
@@ -39,7 +42,7 @@ describe("theme-switcher", () => {
3942 await act ( ( ) => {
4043 // globalThis.window.media = LIGHT as ResolvedScheme;
4144 // @ts -expect-error -- ok
42- matchMedia ( MEDIA ) . onchange ?.( ) ;
45+ m . onchange ?.( ) ;
4346 } ) ;
4447 expect ( hook . result . current . mode ) . toBe ( DARK ) ;
4548 } ) ;
0 commit comments