@@ -115,40 +115,50 @@ describe('usePersistentStorage', () => {
115115 } )
116116
117117 it ( 'should not fallback to cookies when localStorage is available but write fails' , ( ) => {
118- // Mock localStorage: setItem works for the test key (so checkLocalStorage passes)
119- // but fails for the data key.
120- Object . defineProperty ( window , 'localStorage' , {
121- value : {
122- setItem : jest . fn ( ( key , _value ) => {
123- if ( key === '__hrm_test__' ) {
124- return // Success for check
125- }
126- throw new Error ( 'QuotaExceeded' )
127- } ) ,
128- getItem : jest . fn ( ) ,
129- removeItem : jest . fn ( ) ,
130- clear : jest . fn ( ) ,
131- } ,
132- writable : true ,
133- configurable : true ,
134- } )
135-
136- const { result } = renderHook ( ( ) =>
137- usePersistentStorage ( TEST_KEY , INITIAL_VALUE , {
138- enableCookieFallback : true ,
118+ const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
119+
120+ try {
121+ // Mock localStorage: setItem works for the test key (so checkLocalStorage passes)
122+ // but fails for the data key.
123+ Object . defineProperty ( window , 'localStorage' , {
124+ value : {
125+ setItem : jest . fn ( ( key , _value ) => {
126+ if ( key === '__hrm_test__' ) {
127+ return // Success for check
128+ }
129+ throw new Error ( 'QuotaExceeded' )
130+ } ) ,
131+ getItem : jest . fn ( ) ,
132+ removeItem : jest . fn ( ) ,
133+ clear : jest . fn ( ) ,
134+ } ,
135+ writable : true ,
136+ configurable : true ,
139137 } )
140- )
141138
142- act ( ( ) => {
143- const [ , setValue ] = result . current
144- setValue ( UPDATED_VALUE )
145- } )
139+ const { result } = renderHook ( ( ) =>
140+ usePersistentStorage ( TEST_KEY , INITIAL_VALUE , {
141+ enableCookieFallback : true ,
142+ } )
143+ )
146144
147- expect ( window . localStorage . setItem ) . toHaveBeenCalledWith (
148- TEST_KEY ,
149- JSON . stringify ( UPDATED_VALUE )
150- )
151- expect ( Cookies . set ) . not . toHaveBeenCalled ( )
145+ act ( ( ) => {
146+ const [ , setValue ] = result . current
147+ setValue ( UPDATED_VALUE )
148+ } )
149+
150+ expect ( window . localStorage . setItem ) . toHaveBeenCalledWith (
151+ TEST_KEY ,
152+ JSON . stringify ( UPDATED_VALUE )
153+ )
154+ expect ( Cookies . set ) . not . toHaveBeenCalled ( )
155+ expect ( consoleSpy ) . toHaveBeenCalledWith (
156+ 'LocalStorage write failed:' ,
157+ expect . any ( Error )
158+ )
159+ } finally {
160+ consoleSpy . mockRestore ( )
161+ }
152162 } )
153163
154164 it ( 'should load initial value from localStorage if present' , ( ) => {
0 commit comments