11import { act , renderHook } from '@testing-library/react'
22import router from 'next/router'
33import { z } from 'zod'
4- import { vi } from 'vitest'
4+ import { expectTypeOf , vi } from 'vitest'
55import { useQuery , useQueryAndPagination } from '../../zod/routers/nextRouter'
66import { usePagination } from '../nextRouter'
77
@@ -82,11 +82,43 @@ describe('NextRouter', () => {
8282 } )
8383
8484 expect ( result . current . query . search ) . toBe ( search )
85+ expectTypeOf ( result . current . query . search ) . toEqualTypeOf < string > ( )
8586 expect ( router . query . search ) . toBe ( search )
8687 expect ( result . current . page ) . toBe ( 0 )
8788 expect ( router . query . page ) . toBeUndefined ( )
8889 } )
8990
91+ test ( 'passing no default query should return query values that are possibly undefined' , ( ) => {
92+ const { result : resultQueryAndPagination } = renderHook ( ( ) =>
93+ useQueryAndPagination ( searchSchema ) ,
94+ )
95+ expectTypeOf ( resultQueryAndPagination . current . query . search ) . toEqualTypeOf <
96+ string | undefined
97+ > ( )
98+
99+ const { result : resultQuery } = renderHook ( ( ) => useQuery ( searchSchema ) )
100+ expectTypeOf ( resultQuery . current . query . search ) . toEqualTypeOf <
101+ string | undefined
102+ > ( )
103+ } )
104+
105+ test ( 'passing a default query should return query values that are not undefined' , ( ) => {
106+ const { result : resultQueryAndPagination } = renderHook ( ( ) =>
107+ useQueryAndPagination ( searchSchema , { search : '' } ) ,
108+ )
109+ expectTypeOf (
110+ resultQueryAndPagination . current . query . search ,
111+ ) . toEqualTypeOf < string > ( )
112+
113+ const { result : resultQuery } = renderHook ( ( ) =>
114+ useQuery ( searchSchema , { search : '' } ) ,
115+ )
116+ expectTypeOf ( resultQuery . current . query . search ) . toEqualTypeOf < string > ( )
117+
118+ const { result : resultPagination } = renderHook ( ( ) => usePagination ( ) )
119+ expectTypeOf ( resultPagination . current . page ) . toEqualTypeOf < number > ( )
120+ } )
121+
90122 test ( 'change default parameters' , ( ) => {
91123 const page = 1
92124 const size = 10
@@ -261,6 +293,7 @@ describe('NextRouter', () => {
261293 } )
262294
263295 expect ( result . current . query . search ) . toBe ( search )
296+ expectTypeOf ( result . current . query . search ) . toEqualTypeOf < string > ( )
264297 expect ( router . query . search ) . toBe ( search )
265298 } )
266299
@@ -270,6 +303,9 @@ describe('NextRouter', () => {
270303 )
271304
272305 expect ( result . current . query . department ) . toBeUndefined ( )
306+ expectTypeOf ( result . current . query . department ) . toEqualTypeOf <
307+ string | undefined
308+ > ( )
273309 } )
274310
275311 test ( 'the query should be a merge of the default query and the current query' , ( ) => {
0 commit comments