@@ -15,6 +15,7 @@ describe('NextRouter', () => {
1515
1616 const searchSchema = z . object ( {
1717 search : z . string ( ) . optional ( ) . catch ( undefined ) ,
18+ options : z . string ( ) . or ( z . array ( z . string ( ) ) ) . optional ( ) . catch ( undefined ) ,
1819 } )
1920
2021 const useNextRouterQueryWithSearch = (
@@ -289,20 +290,50 @@ describe('NextRouter', () => {
289290 expect ( router . query . greeting ) . toBe ( greeting )
290291 } )
291292
292- test ( 'query keys with default value should not be stored in the url' , ( ) => {
293- const defaultSearch = ''
293+ test ( 'query keys with default value by reset should not be stored in the url' , ( ) => {
294+ const defaultSearch = 'Default'
295+ const defaultOptions = [ 'A' , 'B' ]
294296
295- router . query = { search : 'Max' }
297+ router . query = { search : 'Max' , options : [ 'X' , 'Y' ] }
296298
297299 const { result } = renderHook ( ( ) =>
298- useQuery ( searchSchema , { search : defaultSearch } ) ,
300+ useQuery ( searchSchema , {
301+ search : defaultSearch ,
302+ options : defaultOptions ,
303+ } ) ,
299304 )
300305
301306 act ( ( ) => {
302307 result . current . resetQuery ( )
303308 } )
304309
305310 expect ( result . current . query . search ) . toBe ( defaultSearch )
311+ expect ( result . current . query . options ) . toStrictEqual ( defaultOptions )
312+ expect ( router . query . search ) . toBeUndefined ( )
313+ } )
314+
315+ test ( 'query keys with default value by set should not be stored in the url' , ( ) => {
316+ const defaultSearch = 'Default'
317+ const defaultOptions = [ 'A' , 'B' ]
318+
319+ router . query = { search : 'Max' , options : [ 'X' , 'Y' ] }
320+
321+ const { result } = renderHook ( ( ) =>
322+ useQuery ( searchSchema , {
323+ search : defaultSearch ,
324+ options : defaultOptions ,
325+ } ) ,
326+ )
327+
328+ act ( ( ) => {
329+ result . current . setQuery ( {
330+ search : defaultSearch ,
331+ options : defaultOptions ,
332+ } )
333+ } )
334+
335+ expect ( result . current . query . search ) . toBe ( defaultSearch )
336+ expect ( result . current . query . options ) . toStrictEqual ( defaultOptions )
306337 expect ( router . query . search ) . toBeUndefined ( )
307338 } )
308339
@@ -345,4 +376,28 @@ describe('NextRouter', () => {
345376 expect ( result . current . query . department ) . toBeUndefined ( )
346377 expect ( result . current . query . role ) . toBe ( defaultRole )
347378 } )
379+
380+ test ( 'should handle array query parameters with single and multiple options' , ( ) => {
381+ const optionsSchema = z . object ( {
382+ options : z . array ( z . string ( ) ) ,
383+ } )
384+
385+ const { result } = renderHook ( ( ) =>
386+ useQuery ( optionsSchema , { options : [ ] } ) ,
387+ )
388+
389+ act ( ( ) => {
390+ result . current . setQuery ( { options : [ 'A' ] } )
391+ } )
392+
393+ expect ( result . current . query . options ) . toStrictEqual ( [ 'A' ] )
394+ expect ( router . query . options ) . toStrictEqual ( [ 'A' ] )
395+
396+ act ( ( ) => {
397+ result . current . setQuery ( { options : [ 'A' , 'B' ] } )
398+ } )
399+
400+ expect ( result . current . query . options ) . toStrictEqual ( [ 'A' , 'B' ] )
401+ expect ( router . query . options ) . toStrictEqual ( [ 'A' , 'B' ] )
402+ } )
348403} )
0 commit comments