@@ -382,3 +382,62 @@ describe('useQueryStates: dynamic keys', () => {
382382 expect ( result . current [ 0 ] . z ) . toBeUndefined ( )
383383 } )
384384} )
385+
386+ describe ( 'useQueryStates: update sequencing' , ( ) => {
387+ it ( 'should combine updates for a single key made in the same event loop tick' , async ( ) => {
388+ const onUrlUpdate = vi . fn < OnUrlUpdateFunction > ( )
389+ const { result } = renderHook (
390+ ( ) => useQueryStates ( { test : parseAsString } ) ,
391+ {
392+ wrapper : withNuqsTestingAdapter ( {
393+ onUrlUpdate
394+ } )
395+ }
396+ )
397+ await act ( ( ) => {
398+ result . current [ 1 ] ( { test : 'a' } )
399+ return result . current [ 1 ] ( { test : 'b' } )
400+ } )
401+ expect ( onUrlUpdate ) . toHaveBeenCalledOnce ( )
402+ expect ( onUrlUpdate . mock . calls [ 0 ] ! [ 0 ] . queryString ) . toEqual ( '?test=b' )
403+ } )
404+
405+ it ( 'should combine updates for multiple keys in the same hook made in the same event loop tick' , async ( ) => {
406+ const onUrlUpdate = vi . fn < OnUrlUpdateFunction > ( )
407+ const { result } = renderHook (
408+ ( ) => useQueryStates ( { a : parseAsString , b : parseAsString } ) ,
409+ {
410+ wrapper : withNuqsTestingAdapter ( {
411+ onUrlUpdate
412+ } )
413+ }
414+ )
415+ await act ( ( ) => {
416+ result . current [ 1 ] ( { a : 'a' } )
417+ return result . current [ 1 ] ( ( ) => ( { b : 'b' } ) )
418+ } )
419+ expect ( onUrlUpdate ) . toHaveBeenCalledOnce ( )
420+ expect ( onUrlUpdate . mock . calls [ 0 ] ! [ 0 ] . queryString ) . toEqual ( '?a=a&b=b' )
421+ } )
422+
423+ it ( 'should combine updates for multiple keys in different hook made in the same event loop tick' , async ( ) => {
424+ const onUrlUpdate = vi . fn < OnUrlUpdateFunction > ( )
425+ const { result } = renderHook (
426+ ( ) => ( {
427+ a : useQueryStates ( { a : parseAsString } ) ,
428+ b : useQueryStates ( { b : parseAsString } )
429+ } ) ,
430+ {
431+ wrapper : withNuqsTestingAdapter ( {
432+ onUrlUpdate
433+ } )
434+ }
435+ )
436+ await act ( ( ) => {
437+ result . current . a [ 1 ] ( { a : 'a' } )
438+ return result . current . b [ 1 ] ( ( ) => ( { b : 'b' } ) )
439+ } )
440+ expect ( onUrlUpdate ) . toHaveBeenCalledOnce ( )
441+ expect ( onUrlUpdate . mock . calls [ 0 ] ! [ 0 ] . queryString ) . toEqual ( '?a=a&b=b' )
442+ } )
443+ } )
0 commit comments