@@ -2,7 +2,6 @@ import { act, renderHook } from '@testing-library/react'
22import router from 'next/router'
33import { z } from 'zod'
44import { vi } from 'vitest'
5- import { NonNullableRecord } from '../../utils'
65import { useQuery , useQueryAndPagination } from '../../zod/routers/nextRouter'
76import { usePagination } from '../nextRouter'
87
@@ -19,8 +18,8 @@ describe('NextRouter', () => {
1918 } )
2019
2120 const useNextRouterQueryWithSearch = (
22- defaultQuery : NonNullableRecord < z . infer < typeof searchSchema > > ,
23- ) => useQuery ( defaultQuery , searchSchema )
21+ defaultQuery : Partial < z . infer < typeof searchSchema > > = { } ,
22+ ) => useQuery ( searchSchema , defaultQuery )
2423
2524 test ( 'should set default page and size' , ( ) => {
2625 const page = 0
@@ -64,7 +63,7 @@ describe('NextRouter', () => {
6463 const page = 2
6564
6665 const { result } = renderHook ( ( ) =>
67- useQueryAndPagination ( { search : '' } , searchSchema ) ,
66+ useQueryAndPagination ( searchSchema , { search : '' } ) ,
6867 )
6968
7069 act ( ( ) => {
@@ -104,7 +103,7 @@ describe('NextRouter', () => {
104103 const page = 2
105104
106105 const { result } = renderHook ( ( ) =>
107- useQueryAndPagination ( { search : defaultSearch } , searchSchema ) ,
106+ useQueryAndPagination ( searchSchema , { search : defaultSearch } ) ,
108107 )
109108
110109 act ( ( ) => {
@@ -146,7 +145,7 @@ describe('NextRouter', () => {
146145 const page = 2
147146
148147 const { result } = renderHook ( ( ) =>
149- useQueryAndPagination ( { search : defaultSearch } , searchSchema ) ,
148+ useQueryAndPagination ( searchSchema , { search : defaultSearch } ) ,
150149 )
151150
152151 act ( ( ) => {
@@ -194,17 +193,14 @@ describe('NextRouter', () => {
194193 } )
195194
196195 const { result } = renderHook ( ( ) =>
197- useQuery (
198- {
199- search : '' ,
200- department : '' ,
201- age : defaultAge ,
202- birthDate : defaultBirthDate ,
203- netWorth : defaultNetWorth ,
204- darkMode : defaultDarkMode ,
205- } ,
206- schema ,
207- ) ,
196+ useQuery ( schema , {
197+ search : '' ,
198+ department : '' ,
199+ age : defaultAge ,
200+ birthDate : defaultBirthDate ,
201+ netWorth : defaultNetWorth ,
202+ darkMode : defaultDarkMode ,
203+ } ) ,
208204 )
209205
210206 act ( ( ) => {
@@ -226,7 +222,7 @@ describe('NextRouter', () => {
226222 const greeting = 'hello'
227223 router . query = { greeting }
228224
229- const { result } = renderHook ( ( ) => useQuery ( { search : '' } , searchSchema ) )
225+ const { result } = renderHook ( ( ) => useQuery ( searchSchema , { search : '' } ) )
230226
231227 act ( ( ) => {
232228 result . current . setQuery ( { search : 'Max' } )
@@ -242,7 +238,7 @@ describe('NextRouter', () => {
242238 router . query = { search : 'Max' }
243239
244240 const { result } = renderHook ( ( ) =>
245- useQuery ( { search : defaultSearch } , searchSchema ) ,
241+ useQuery ( searchSchema , { search : defaultSearch } ) ,
246242 )
247243
248244 act ( ( ) => {
@@ -257,7 +253,7 @@ describe('NextRouter', () => {
257253 const search = ''
258254
259255 const { result } = renderHook ( ( ) =>
260- useQuery ( { search : 'Default search' } , searchSchema ) ,
256+ useQuery ( searchSchema , { search : 'Default search' } ) ,
261257 )
262258
263259 act ( ( ) => {
@@ -267,4 +263,25 @@ describe('NextRouter', () => {
267263 expect ( result . current . query . search ) . toBe ( search )
268264 expect ( router . query . search ) . toBe ( search )
269265 } )
266+
267+ test ( 'passing no default query should return undefined for a property that is not in the query' , ( ) => {
268+ const { result } = renderHook ( ( ) =>
269+ useQuery ( z . object ( { department : z . string ( ) } ) ) ,
270+ )
271+
272+ expect ( result . current . query . department ) . toBeUndefined ( )
273+ } )
274+
275+ test ( 'the query should be a merge of the default query and the current query' , ( ) => {
276+ const defaultRole = 'ADMIN'
277+
278+ const { result } = renderHook ( ( ) =>
279+ useQuery ( z . object ( { department : z . string ( ) , role : z . string ( ) } ) , {
280+ role : defaultRole ,
281+ } ) ,
282+ )
283+
284+ expect ( result . current . query . department ) . toBeUndefined ( )
285+ expect ( result . current . query . role ) . toBe ( defaultRole )
286+ } )
270287} )
0 commit comments