@@ -2,11 +2,11 @@ import { useLocalStorage, deleteFromStorage } from '../src';
2
2
import { renderHook , act } from '@testing-library/react-hooks' ;
3
3
4
4
jest . mock ( '../src/storage' , ( ) => ( {
5
- ...jest . requireActual ( '../src/storage' ) ,
6
- storage : jest . fn ( ) ,
5
+ ...jest . requireActual ( '../src/storage' ) ,
6
+ storage : jest . fn ( ) ,
7
7
} ) ) ;
8
8
9
- import { storage , LocalStorageProxy , MemoryStorageProxy } from '../src/storage'
9
+ import { storage , LocalStorageProxy , MemoryStorageProxy } from '../src/storage'
10
10
11
11
describe ( 'Module: use-localstorage' , ( ) => {
12
12
@@ -130,7 +130,7 @@ describe('Module: use-localstorage', () => {
130
130
const defaultValue = 'i' ;
131
131
const newValue = 'o' ;
132
132
const { result } = renderHook ( ( ) => useLocalStorage ( key , defaultValue ) ) ;
133
-
133
+
134
134
expect ( result . current [ 0 ] ) . toBe ( defaultValue ) ;
135
135
expect ( localStorage . getItem ( key ) ) . toBe ( defaultValue ) ;
136
136
@@ -144,6 +144,31 @@ describe('Module: use-localstorage', () => {
144
144
} ) ;
145
145
} ) ;
146
146
} ) ;
147
+
148
+ describe ( 'when a functional update is called' , ( ) => {
149
+ it ( 'passes the current state to the update function' , async ( ) => {
150
+ const key = 'functionUpdate' ;
151
+ const defaultValue = 'ImTheDefault' ;
152
+ const newValue = 'ImTheNewValue' ;
153
+ const { result } = renderHook ( ( ) => useLocalStorage ( key , defaultValue ) ) ;
154
+
155
+ act ( ( ) => result . current [ 1 ] ( prevValue => {
156
+ expect ( prevValue ) . toBe ( defaultValue ) ;
157
+ return newValue ;
158
+ } ) ) ;
159
+ expect ( result . current [ 0 ] ) . toBe ( newValue ) ;
160
+ } ) ;
161
+
162
+ it ( 'handles updates to arrays' , async ( ) => {
163
+ const key = 'arrayUpdate' ;
164
+ const defaultValue = [ 'A' ] ;
165
+ const newValue = 'B' ;
166
+ const { result } = renderHook ( ( ) => useLocalStorage ( key , defaultValue ) ) ;
167
+
168
+ act ( ( ) => result . current [ 1 ] ( prevValue => prevValue ? [ ...prevValue , newValue ] : [ newValue ] ) ) ;
169
+ expect ( result . current [ 0 ] ) . toEqual ( [ ...defaultValue , newValue ] ) ;
170
+ } )
171
+ } ) ;
147
172
} )
148
173
149
174
describe ( "when localStorage api is disabled" , ( ) => {
0 commit comments