@@ -56,7 +56,7 @@ describe("useLocalState()", () => {
56
56
expect ( todos ) . toEqual ( values ) ;
57
57
} ) ;
58
58
59
- it ( "accepts callback as a value" , async ( ) => {
59
+ it ( "accepts callback as an initial value" , async ( ) => {
60
60
const key = "todos" ;
61
61
const values = [ "first" , "second" ] ;
62
62
const callback = ( ) => values ;
@@ -66,12 +66,32 @@ describe("useLocalState()", () => {
66
66
expect ( todos ) . toEqual ( values ) ;
67
67
} ) ;
68
68
69
+ it ( "accepts callback as a initial value and can be updated" , async ( ) => {
70
+ const key = "todos" ;
71
+ const values = [ "first" , "second" ] ;
72
+ const callback = ( ) => values ;
73
+ const { result } = renderHook ( ( ) => useLocalState ( key , callback ) ) ;
74
+
75
+ const [ initialValues ] = result . current ;
76
+ expect ( initialValues ) . toEqual ( values ) ;
77
+
78
+ const newValue = [ "third" , "fourth" ] ;
79
+ act ( ( ) => {
80
+ const setValue = result . current [ 1 ] ;
81
+ setValue ( newValue ) ;
82
+ } ) ;
83
+
84
+ const [ changedValues ] = result . current ;
85
+ expect ( changedValues ) . toEqual ( newValue ) ;
86
+ } ) ;
87
+
69
88
it ( "can update value as string" , async ( ) => {
70
89
const key = "key" ;
71
90
const value = "something" ;
72
-
73
91
const { result } = renderHook ( ( ) => useLocalState ( key , value ) ) ;
74
- expect ( result . current [ 0 ] ) . toEqual ( value ) ;
92
+
93
+ const [ initialValue ] = result . current ;
94
+ expect ( initialValue ) . toEqual ( value ) ;
75
95
76
96
const newValue = "something else" ;
77
97
act ( ( ) => {
@@ -87,9 +107,10 @@ describe("useLocalState()", () => {
87
107
it ( "can update value as object" , async ( ) => {
88
108
const key = "key" ;
89
109
const value = { something : "something" } ;
90
-
91
110
const { result } = renderHook ( ( ) => useLocalState ( key , value ) ) ;
92
- expect ( result . current [ 0 ] ) . toEqual ( value ) ;
111
+
112
+ const [ initialValue ] = result . current ;
113
+ expect ( initialValue ) . toEqual ( value ) ;
93
114
94
115
const newValue = { something : "else" } ;
95
116
act ( ( ) => {
@@ -118,8 +139,7 @@ describe("useLocalState()", () => {
118
139
expect ( todos ) . toEqual ( newValues ) ;
119
140
} ) ;
120
141
121
- // todo(): this logic could be super handy...
122
- it ( "updates state with callback function" , ( ) => {
142
+ it ( "updates state with callback function" , async ( ) => {
123
143
const key = "todos" ;
124
144
const values = [ "first" , "second" ] ;
125
145
const { result } = renderHook ( ( ) => useLocalState ( key , values ) ) ;
@@ -182,7 +202,6 @@ describe("useLocalState()", () => {
182
202
it ( "can handle `undefined` values" , async ( ) => {
183
203
const key = "todos" ;
184
204
const values = [ "first" , "second" ] ;
185
-
186
205
const { result } = renderHook ( ( ) =>
187
206
useLocalState < string [ ] | undefined > ( key , values )
188
207
) ;
@@ -199,7 +218,6 @@ describe("useLocalState()", () => {
199
218
it ( "can handle `null` values" , async ( ) => {
200
219
const key = "todos" ;
201
220
const values = [ "first" , "second" ] ;
202
-
203
221
const { result } = renderHook ( ( ) =>
204
222
useLocalState < string [ ] | null > ( key , values )
205
223
) ;
@@ -212,4 +230,61 @@ describe("useLocalState()", () => {
212
230
const [ value ] = result . current ;
213
231
expect ( value ) . toBe ( null ) ;
214
232
} ) ;
233
+
234
+ it ( "can reset to default value" , async ( ) => {
235
+ if ( ! SUPPORTED ) return ;
236
+
237
+ const key = "key" ;
238
+ const value = "something" ;
239
+ const { result } = renderHook ( ( ) => useLocalState ( key , value ) ) ;
240
+
241
+ const [ initialValue ] = result . current ;
242
+ expect ( initialValue ) . toEqual ( value ) ;
243
+
244
+ const newValue = "something else" ;
245
+ act ( ( ) => {
246
+ const setValue = result . current [ 1 ] ;
247
+ setValue ( newValue ) ;
248
+ } ) ;
249
+
250
+ const [ changedValue ] = result . current ;
251
+ expect ( changedValue ) . toEqual ( newValue ) ;
252
+
253
+ act ( ( ) => {
254
+ const resetValue = result . current [ 2 ] ;
255
+ resetValue ( ) ;
256
+ } ) ;
257
+
258
+ const [ resetValue ] = result . current ;
259
+ expect ( resetValue ) . toEqual ( value ) ;
260
+ expect ( localStorage . getItem ( key ) ) . toEqual ( null ) ;
261
+ } ) ;
262
+
263
+ it ( "can reset to default value as callback" , async ( ) => {
264
+ const key = "todos" ;
265
+ const values = [ "first" , "second" ] ;
266
+ const callback = ( ) => values ;
267
+ const { result } = renderHook ( ( ) => useLocalState ( key , callback ) ) ;
268
+
269
+ const [ initialValues ] = result . current ;
270
+ expect ( initialValues ) . toEqual ( values ) ;
271
+
272
+ const newValues = [ "third" , "fourth" ] ;
273
+ act ( ( ) => {
274
+ const setValues = result . current [ 1 ] ;
275
+ setValues ( newValues ) ;
276
+ } ) ;
277
+
278
+ const [ changedValues ] = result . current ;
279
+ expect ( changedValues ) . toEqual ( newValues ) ;
280
+
281
+ act ( ( ) => {
282
+ const resetValues = result . current [ 2 ] ;
283
+ resetValues ( ) ;
284
+ } ) ;
285
+
286
+ const [ resetValues ] = result . current ;
287
+ expect ( resetValues ) . toEqual ( values ) ;
288
+ expect ( localStorage . getItem ( key ) ) . toEqual ( null ) ;
289
+ } ) ;
215
290
} ) ;
0 commit comments