11import { act , renderHook } from "@testing-library/react" ;
2- import { useCookie } from "../hooks/useCookie" ;
2+ import { revalidateCookies , useCookie } from "../hooks/useCookie" ;
33
44function setValue (
55 value : string | ( ( prevValue ?: string ) => string | undefined ) | undefined ,
@@ -14,8 +14,15 @@ function getValue(hook: { current: ReturnType<typeof useCookie> }) {
1414 return hook . current [ 0 ] ;
1515}
1616
17+ afterEach ( ( ) => {
18+ // Clear all cookies after each test
19+ document . cookie . split ( ";" ) . forEach ( ( c ) => {
20+ document . cookie = `${ c . trim ( ) . split ( "=" ) [ 0 ] } =;expires=Thu, 01 Jan 1970 00:00:00 UTC;` ;
21+ } ) ;
22+ } ) ;
23+
1724test ( "should manage cookies" , ( ) => {
18- const { result : hook } = renderHook ( ( ) => useCookie ( "test" ) ) ;
25+ const { result : hook } = renderHook ( ( ) => useCookie ( "manage- test" ) ) ;
1926
2027 setValue ( "custom value" , hook ) ;
2128
@@ -30,7 +37,7 @@ test("should manage cookies", () => {
3037
3138test ( "should manage cookies with default value" , ( ) => {
3239 const { result : hook } = renderHook ( ( ) =>
33- useCookie ( "test " , { defaultValue : "default value" } ) ,
40+ useCookie ( "default-value " , { defaultValue : "default value" } ) ,
3441 ) ;
3542
3643 expect ( getValue ( hook ) ) . toBe ( "default value" ) ;
@@ -43,11 +50,24 @@ test("should manage cookies with default value", () => {
4350} ) ;
4451
4552test ( "should sync values across hooks" , ( ) => {
46- const { result : hook } = renderHook ( ( ) => useCookie ( "test " ) ) ;
47- const { result : hook2 } = renderHook ( ( ) => useCookie ( "test " ) ) ;
53+ const { result : hook } = renderHook ( ( ) => useCookie ( "sync " ) ) ;
54+ const { result : hook2 } = renderHook ( ( ) => useCookie ( "sync " ) ) ;
4855
4956 setValue ( "new value" , hook ) ;
5057
5158 expect ( getValue ( hook ) ) . toBe ( "new value" ) ;
5259 expect ( getValue ( hook2 ) ) . toBe ( "new value" ) ;
5360} ) ;
61+
62+ test ( "should be able to revalidate cookies externally" , ( ) => {
63+ const { result : hook } = renderHook ( ( ) => useCookie ( "external" ) ) ;
64+ document . cookie = "external=new value" ;
65+ expect ( hook . current [ 0 ] ) . toBe ( undefined ) ;
66+
67+ act ( ( ) => {
68+ // Revalidate the cookies, trigger the external sync
69+ revalidateCookies ( ) ;
70+ } ) ;
71+
72+ expect ( hook . current [ 0 ] ) . toBe ( "new value" ) ;
73+ } ) ;
0 commit comments