@@ -2,6 +2,7 @@ import { act, cleanup, fireEvent, render, renderHook } from "@testing-library/re
2
2
import { afterEach , beforeEach , describe , test } from "vitest" ;
3
3
import { useTheme } from "../../hooks/use-theme" ;
4
4
import { DEFAULT_ID } from "../../constants" ;
5
+ import { ServerTarget } from "../../server" ;
5
6
import { ThemeSwitcher } from "./theme-switcher" ;
6
7
7
8
describe ( "theme-switcher" , ( ) => {
@@ -52,7 +53,7 @@ describe("theme-switcher", () => {
52
53
test . todo ( "test media change event -- not supported by fireEvent" ) ;
53
54
} ) ;
54
55
55
- describe ( "test theme-switcher with props" , ( ) => {
56
+ describe ( "test theme-switcher with props and server target " , ( ) => {
56
57
afterEach ( cleanup ) ;
57
58
58
59
test ( "test dontSync" , async ( { expect } ) => {
@@ -72,4 +73,24 @@ describe("test theme-switcher with props", () => {
72
73
test . todo ( "test invalid targetId" , ( { expect } ) => {
73
74
expect ( render ( < ThemeSwitcher targetId = "" /> ) ) . toThrow ( "id can not be an empty string" ) ;
74
75
} ) ;
76
+
77
+ test ( "test with confined server target" , ( { expect } ) => {
78
+ const THEME = "my-theme-with-server" ;
79
+ const COLOR_SCHEME = "dark" ;
80
+ const targetId = "my-custom-target" ;
81
+ globalThis . cookies = {
82
+ [ targetId ] : {
83
+ value : `${ THEME } ,${ COLOR_SCHEME } ` ,
84
+ } ,
85
+ } ;
86
+ render ( < ServerTarget targetId = { targetId } /> ) ;
87
+ render ( < ThemeSwitcher targetId = { targetId } /> ) ;
88
+ const { result } = renderHook ( ( ) => useTheme ( targetId ) ) ;
89
+ const NEW_THEME = "new-theme" ;
90
+ act ( ( ) => {
91
+ result . current . setTheme ( NEW_THEME ) ;
92
+ } ) ;
93
+ expect ( document . cookie ) . not . toContain ( THEME ) ;
94
+ expect ( document . cookie ) . toContain ( NEW_THEME ) ;
95
+ } ) ;
75
96
} ) ;
0 commit comments