@@ -328,4 +328,62 @@ describe('useQueryParams', () => {
328
328
const [ decodedValue3 ] = result . current ;
329
329
expect ( decodedValue3 . foo ) . toBe ( decodedValue2 . foo ) ;
330
330
} ) ;
331
+
332
+
333
+ describe ( 'should call custom paramConfig.decode properly' , ( ) => {
334
+ it ( 'when custom paramConfig decode undefined as non-undefined value, should not call decode function when irrelevant update happens' , ( ) => {
335
+ const { wrapper } = setupWrapper ( { bar : "1" } ) ;
336
+ const customQueryParam = {
337
+ encode : ( str : string | undefined | null ) => str ,
338
+ decode : ( str : string | undefined | null ) => {
339
+ if ( str === undefined ) {
340
+ return null ;
341
+ }
342
+ return str ;
343
+ } ,
344
+ }
345
+ const decodeSpy = jest . spyOn ( customQueryParam , 'decode' ) ;
346
+ const { result, rerender } = renderHook (
347
+ ( ) => useQueryParams ( { foo : customQueryParam , bar : StringParam } ) ,
348
+ {
349
+ wrapper,
350
+ }
351
+ ) ;
352
+
353
+ const [ decodedValue , setter ] = result . current ;
354
+ expect ( decodedValue ) . toEqual ( { foo : null , bar : "1" } ) ;
355
+ expect ( decodeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
356
+
357
+ setter ( { bar : "2" } ) ;
358
+ rerender ( ) ;
359
+ setter ( { bar : "3" } ) ;
360
+ rerender ( ) ;
361
+ expect ( decodeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
362
+ } ) ;
363
+
364
+ it ( 'when custom paramConfig decode undefined as undefined, should call decode function when irrelevant update happens' , ( ) => {
365
+ const { wrapper } = setupWrapper ( { bar : "1" } ) ;
366
+ const customQueryParam = {
367
+ encode : ( str : string | undefined | null ) => str ,
368
+ decode : ( str : string | undefined | null ) => str ,
369
+ }
370
+ const decodeSpy = jest . spyOn ( customQueryParam , 'decode' ) ;
371
+ const { result, rerender } = renderHook (
372
+ ( ) => useQueryParams ( { foo : customQueryParam , bar : StringParam } ) ,
373
+ {
374
+ wrapper,
375
+ }
376
+ ) ;
377
+
378
+ const [ decodedValue , setter ] = result . current ;
379
+ expect ( decodedValue ) . toEqual ( { foo : undefined , bar : "1" } ) ;
380
+ expect ( decodeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
381
+
382
+ setter ( { bar : "2" } ) ;
383
+ rerender ( ) ;
384
+ setter ( { bar : "3" } ) ;
385
+ rerender ( ) ;
386
+ expect ( decodeSpy ) . toHaveBeenCalledTimes ( 3 ) ;
387
+ } ) ;
388
+ } ) ;
331
389
} ) ;
0 commit comments