@@ -705,6 +705,111 @@ describe('useLocalStorageState()', () => {
705705 } )
706706 } )
707707
708+ describe ( 'localStorage is null (Firefox with dom.storage.enabled: false)' , ( ) => {
709+ test ( 'returns defaultValue when localStorage is null' , ( ) => {
710+ vi . spyOn ( window , 'localStorage' , 'get' ) . mockReturnValue ( null as any )
711+
712+ const { result } = renderHook ( ( ) =>
713+ useLocalStorageState ( 'todos' , { defaultValue : [ 'first' , 'second' ] } ) ,
714+ )
715+
716+ const [ todos ] = result . current
717+ expect ( todos ) . toStrictEqual ( [ 'first' , 'second' ] )
718+ } )
719+
720+ test ( 'isPersistent is true when value equals defaultValue and localStorage is null' , ( ) => {
721+ vi . spyOn ( window , 'localStorage' , 'get' ) . mockReturnValue ( null as any )
722+
723+ const { result } = renderHook ( ( ) =>
724+ useLocalStorageState ( 'todos' , { defaultValue : [ 'first' , 'second' ] } ) ,
725+ )
726+
727+ const [ , , { isPersistent } ] = result . current
728+ expect ( isPersistent ) . toBe ( true )
729+ } )
730+
731+ test ( 'isPersistent is false when value is changed and localStorage is null' , ( ) => {
732+ vi . spyOn ( window , 'localStorage' , 'get' ) . mockReturnValue ( null as any )
733+
734+ const { result } = renderHook ( ( ) =>
735+ useLocalStorageState ( 'todos' , { defaultValue : [ 'first' , 'second' ] } ) ,
736+ )
737+
738+ act ( ( ) => {
739+ const setTodos = result . current [ 1 ]
740+ setTodos ( [ 'third' , 'forth' ] )
741+ } )
742+
743+ const [ todos , , { isPersistent } ] = result . current
744+ expect ( todos ) . toStrictEqual ( [ 'third' , 'forth' ] )
745+ expect ( isPersistent ) . toBe ( false )
746+ } )
747+
748+ test ( 'setValue works when localStorage is null' , ( ) => {
749+ vi . spyOn ( window , 'localStorage' , 'get' ) . mockReturnValue ( null as any )
750+
751+ const { result } = renderHook ( ( ) =>
752+ useLocalStorageState ( 'todos' , { defaultValue : [ 'first' , 'second' ] } ) ,
753+ )
754+
755+ act ( ( ) => {
756+ const setTodos = result . current [ 1 ]
757+ setTodos ( [ 'third' , 'forth' ] )
758+ } )
759+
760+ const [ todos ] = result . current
761+ expect ( todos ) . toStrictEqual ( [ 'third' , 'forth' ] )
762+ } )
763+
764+ test ( 'setValue with callback works when localStorage is null' , ( ) => {
765+ vi . spyOn ( window , 'localStorage' , 'get' ) . mockReturnValue ( null as any )
766+
767+ const { result } = renderHook ( ( ) =>
768+ useLocalStorageState ( 'todos' , { defaultValue : [ 'first' , 'second' ] } ) ,
769+ )
770+
771+ act ( ( ) => {
772+ const setTodos = result . current [ 1 ]
773+ setTodos ( ( value ) => [ ...value , 'third' ] )
774+ } )
775+
776+ const [ todos ] = result . current
777+ expect ( todos ) . toStrictEqual ( [ 'first' , 'second' , 'third' ] )
778+ } )
779+
780+ test ( 'removeItem works when localStorage is null' , ( ) => {
781+ vi . spyOn ( window , 'localStorage' , 'get' ) . mockReturnValue ( null as any )
782+
783+ const { result } = renderHook ( ( ) =>
784+ useLocalStorageState ( 'todos' , { defaultValue : [ 'first' , 'second' ] } ) ,
785+ )
786+
787+ act ( ( ) => {
788+ const setTodos = result . current [ 1 ]
789+ setTodos ( [ 'third' , 'forth' ] )
790+ } )
791+
792+ act ( ( ) => {
793+ const removeItem = result . current [ 2 ] . removeItem
794+ removeItem ( )
795+ } )
796+
797+ const [ todos ] = result . current
798+ expect ( todos ) . toStrictEqual ( [ 'first' , 'second' ] )
799+ } )
800+
801+ test ( 'returns undefined when no defaultValue and localStorage is null' , ( ) => {
802+ vi . spyOn ( window , 'localStorage' , 'get' ) . mockReturnValue ( null as any )
803+
804+ const { result } = renderHook ( ( ) =>
805+ useLocalStorageState ( 'todos' ) ,
806+ )
807+
808+ const [ todos ] = result . current
809+ expect ( todos ) . toBe ( undefined )
810+ } )
811+ } )
812+
708813 describe ( '"serializer" option' , ( ) => {
709814 test ( 'can serialize Date from initial value' , ( ) => {
710815 const date = new Date ( )
0 commit comments