Open History to see the last commit
1import { describe, expectTypeOf, test } from 'vitest'
2import { createRootRoute, createRoute, createRouter, useSearch } from '../src'
3import type { SearchSchemaInput } from '../src'
4β
5describe('useSearch', () => {
6 test('when there are no search params', () => {
7 const rootRoute = createRootRoute()
8β
9 const indexRoute = createRoute({
10 getParentRoute: () => rootRoute,
11 path: '/',
12 })
13β
14 const invoicesRoute = createRoute({
15 getParentRoute: () => rootRoute,
16 path: 'invoices',
17 })
18β
19 const invoicesIndexRoute = createRoute({
20 getParentRoute: () => invoicesRoute,
21 path: '/',
22 })
23β
24 const invoiceRoute = createRoute({
25 getParentRoute: () => invoicesRoute,
26 path: '$invoiceId',
27 })
28β
29 const routeTree = rootRoute.addChildren([
30 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
31 indexRoute,
32 ])
33β
34 // eslint-disable-next-line unused-imports/no-unused-vars
35 const defaultRouter = createRouter({
36 routeTree,
37 })
38β
39 type DefaultRouter = typeof defaultRouter
40β
41 expectTypeOf(useSearch<DefaultRouter>)
42 .parameter(0)
43 .toHaveProperty('from')
44 .toEqualTypeOf<
45 '/invoices' | '__root__' | '/invoices/$invoiceId' | '/invoices/' | '/'
46 >()
47β
48 expectTypeOf(useSearch<DefaultRouter>)
49 .parameter(0)
50 .toHaveProperty('strict')
51 .toEqualTypeOf<true | undefined>()
52β
53 expectTypeOf(useSearch<DefaultRouter, '/'>)
54 .parameter(0)
55 .toHaveProperty('select')
56 .parameter(0)
57 .toEqualTypeOf<{}>()
58β
59 expectTypeOf(useSearch<DefaultRouter, '/'>)
60 .parameter(0)
61 .toHaveProperty('select')
62 .returns.toEqualTypeOf<unknown>()
63β
64 expectTypeOf(useSearch<DefaultRouter, '/'>).returns.toEqualTypeOf<{}>()
65β
66 expectTypeOf(
67 useSearch<DefaultRouter, '/', false>({
68 strict: false,
69 }),
70 ).toEqualTypeOf<{}>()
71 })
72β
73 test('when there is one search params', () => {
74 const rootRoute = createRootRoute()
75β
76 const indexRoute = createRoute({
77 getParentRoute: () => rootRoute,
78 path: '/',
79 })
80β
81 const invoicesRoute = createRoute({
82 getParentRoute: () => rootRoute,
83 path: 'invoices',
84 validateSearch: () => ({ page: 0 }),
85 })
86β
87 const invoicesIndexRoute = createRoute({
88 getParentRoute: () => invoicesRoute,
89 path: '/',
90 })
91β
92 const invoiceRoute = createRoute({
93 getParentRoute: () => invoicesRoute,
94 path: '$invoiceId',
95 })
96β
97 const routeTree = rootRoute.addChildren([
98 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
99 indexRoute,
100 ])
101β
102 // eslint-disable-next-line unused-imports/no-unused-vars
103 const defaultRouter = createRouter({
104 routeTree,
105 })
106β
107 type DefaultRouter = typeof defaultRouter
108β
109 expectTypeOf(useSearch<DefaultRouter, '/'>).returns.toEqualTypeOf<{}>()
110β
111 expectTypeOf(useSearch<DefaultRouter, '/invoices'>).returns.toEqualTypeOf<{
112 page: number
113 }>()
114β
115 expectTypeOf(useSearch<DefaultRouter, '/invoices'>)
116 .parameter(0)
117 .toHaveProperty('select')
118 .toEqualTypeOf<((search: { page: number }) => unknown) | undefined>()
119β
120 expectTypeOf(
121 useSearch<DefaultRouter, '/invoices', false>,
122 ).returns.toEqualTypeOf<{ page?: number }>()
123β
124 expectTypeOf(useSearch<DefaultRouter, '/invoices', false>)
125 .parameter(0)
126 .toHaveProperty('select')
127 .toEqualTypeOf<((search: { page?: number }) => unknown) | undefined>()
128β
129 expectTypeOf(
130 useSearch<
131 DefaultRouter,
132 '/invoices',
133 /* strict */ false,
134 /* shouldThrow */ true,
135 number
136 >,
137 ).returns.toEqualTypeOf<number>()
138β
139 expectTypeOf(
140 useSearch<
141 DefaultRouter,
142 '/invoices',
143 /* strict */ false,
144 /* shouldThrow */ true,
145 { func: () => void }
146 >,
147 )
148 .parameter(0)
149 .toHaveProperty('select')
150 .toEqualTypeOf<
151 ((search: { page?: number }) => { func: () => void }) | undefined
152 >()
153β
154 expectTypeOf(
155 useSearch<
156 DefaultRouter,
157 '/invoices',
158 /* strict */ false,
159 /* shouldThrow */ true,
160 { func: () => void }
161 >,
162 )
163 .parameter(0)
164 .toHaveProperty('structuralSharing')
165 .toEqualTypeOf<false | undefined>()
166β
167 expectTypeOf(
168 useSearch<
169 DefaultRouter,
170 '/invoices',
171 /* strict */ false,
172 /* shouldThrow */ true,
173 { func: () => void },
174 true
175 >,
176 )
177 .parameter(0)
178 .toHaveProperty('select')
179 .toEqualTypeOf<
180 | ((search: { page?: number }) => {
181 func: 'Function is not serializable'
182 })
183 | undefined
184 >()
185β
186 expectTypeOf(
187 useSearch<
188 DefaultRouter,
189 '/invoices',
190 /* strict */ false,
191 /* shouldThrow */ true,
192 { func: () => void },
193 true
194 >,
195 )
196 .parameter(0)
197 .toHaveProperty('structuralSharing')
198 .toEqualTypeOf<false | undefined>()
199β
200 expectTypeOf(
201 useSearch<
202 DefaultRouter,
203 '/invoices',
204 /* strict */ false,
205 /* shouldThrow */ true,
206 { hi: any },
207 true
208 >,
209 )
210 .parameter(0)
211 .toHaveProperty('select')
212 .toEqualTypeOf<
213 | ((search: { page?: number }) => {
214 hi: never
215 })
216 | undefined
217 >()
218β
219 expectTypeOf(
220 useSearch<
221 DefaultRouter,
222 '/invoices',
223 /* strict */ false,
224 /* shouldThrow */ true,
225 { hi: any },
226 true
227 >,
228 )
229 .parameter(0)
230 .toHaveProperty('structuralSharing')
231 .toEqualTypeOf<false | undefined>()
232β
233 // eslint-disable-next-line unused-imports/no-unused-vars
234 const routerWithStructuralSharing = createRouter({
235 routeTree,
236 defaultStructuralSharing: true,
237 })
238β
239 expectTypeOf(
240 useSearch<
241 typeof routerWithStructuralSharing,
242 '/invoices',
243 /* strict */ false,
244 /* shouldThrow */ true,
245 { func: () => void }
246 >,
247 )
248 .parameter(0)
249 .toHaveProperty('select')
250 .toEqualTypeOf<
251 | ((search: { page?: number }) => {
252 func: 'Function is not serializable'
253 })
254 | undefined
255 >()
256β
257 expectTypeOf(
258 useSearch<
259 typeof routerWithStructuralSharing,
260 '/invoices',
261 /* strict */ false,
262 /* shouldThrow */ true,
263 { date: () => void },
264 true
265 >,
266 )
267 .parameter(0)
268 .toHaveProperty('structuralSharing')
269 .toEqualTypeOf<false>()
270β
271 expectTypeOf(
272 useSearch<
273 typeof routerWithStructuralSharing,
274 '/invoices',
275 /* strict */ false,
276 /* shouldThrow */ true,
277 { hi: any },
278 true
279 >,
280 )
281 .parameter(0)
282 .toHaveProperty('select')
283 .toEqualTypeOf<
284 | ((search: { page?: number }) => {
285 hi: never
286 })
287 | undefined
288 >()
289β
290 expectTypeOf(
291 useSearch<
292 typeof routerWithStructuralSharing,
293 '/invoices',
294 /* strict */ false,
295 /* shouldThrow */ true,
296 { hi: any },
297 true
298 >,
299 )
300 .parameter(0)
301 .toHaveProperty('structuralSharing')
302 .toEqualTypeOf<false>()
303 })
304β
305 test('when there are multiple search params', () => {
306 const rootRoute = createRootRoute()
307β
308 const indexRoute = createRoute({
309 getParentRoute: () => rootRoute,
310 path: '/',
311 })
312β
313 const invoicesRoute = createRoute({
314 getParentRoute: () => rootRoute,
315 path: 'invoices',
316 validateSearch: () => ({ page: 0 }),
317 })
318β
319 const invoicesIndexRoute = createRoute({
320 getParentRoute: () => invoicesRoute,
321 path: '/',
322 })
323β
324 const invoiceRoute = createRoute({
325 getParentRoute: () => invoicesRoute,
326 path: '$invoiceId',
327 validateSearch: () => ({ detail: 'detail' }),
328 })
329β
330 const routeTree = rootRoute.addChildren([
331 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
332 indexRoute,
333 ])
334β
335 // eslint-disable-next-line unused-imports/no-unused-vars
336 const defaultRouter = createRouter({
337 routeTree,
338 })
339β
340 type DefaultRouter = typeof defaultRouter
341β
342 expectTypeOf(useSearch<DefaultRouter, '/'>).returns.toEqualTypeOf<{}>()
343β
344 expectTypeOf(useSearch<DefaultRouter, '/invoices'>).returns.toEqualTypeOf<{
345 page: number
346 }>()
347β
348 expectTypeOf(useSearch<DefaultRouter, '/invoices'>)
349 .parameter(0)
350 .toHaveProperty('select')
351 .toEqualTypeOf<((search: { page: number }) => unknown) | undefined>()
352β
353 expectTypeOf(
354 useSearch<DefaultRouter, '/invoices', false>,
355 ).returns.toEqualTypeOf<{ page?: number; detail?: string }>()
356β
357 expectTypeOf(useSearch<DefaultRouter, '/invoices', false>)
358 .parameter(0)
359 .toHaveProperty('select')
360 .toEqualTypeOf<
361 ((search: { page?: number; detail?: string }) => unknown) | undefined
362 >()
363 })
364β
365 test('when there are overlapping search params', () => {
366 const rootRoute = createRootRoute()
367β
368 const indexRoute = createRoute({
369 getParentRoute: () => rootRoute,
370 path: '/',
371 })
372β
373 const invoicesRoute = createRoute({
374 getParentRoute: () => rootRoute,
375 path: 'invoices',
376 validateSearch: () => ({ page: 0 }),
377 })
378β
379 const invoicesIndexRoute = createRoute({
380 getParentRoute: () => invoicesRoute,
381 path: '/',
382 validateSearch: () => ({ detail: 50 }) as const,
383 })
384β
385 const invoiceRoute = createRoute({
386 getParentRoute: () => invoicesRoute,
387 path: '$invoiceId',
388 validateSearch: () => ({ detail: 'detail' }) as const,
389 })
390β
391 const routeTree = rootRoute.addChildren([
392 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
393 indexRoute,
394 ])
395β
396 // eslint-disable-next-line unused-imports/no-unused-vars
397 const defaultRouter = createRouter({
398 routeTree,
399 })
400β
401 type DefaultRouter = typeof defaultRouter
402β
403 expectTypeOf(useSearch<DefaultRouter, '/'>).returns.toEqualTypeOf<{}>()
404β
405 expectTypeOf(useSearch<DefaultRouter, '/invoices'>).returns.toEqualTypeOf<{
406 page: number
407 }>()
408β
409 expectTypeOf(useSearch<DefaultRouter, '/invoices'>)
410 .parameter(0)
411 .toHaveProperty('select')
412 .toEqualTypeOf<((search: { page: number }) => unknown) | undefined>()
413β
414 expectTypeOf(
415 useSearch<DefaultRouter, '/invoices', false>,
416 ).returns.toEqualTypeOf<{ page?: number; detail?: 'detail' | 50 }>()
417β
418 expectTypeOf(useSearch<DefaultRouter, '/invoices', false>)
419 .parameter(0)
420 .toHaveProperty('select')
421 .toEqualTypeOf<
422 | ((search: { page?: number; detail?: 'detail' | 50 }) => unknown)
423 | undefined
424 >()
425 })
426β
427 test('when the root has no search params but the index route does', () => {
428 const rootRoute = createRootRoute()
429β
430 const indexRoute = createRoute({
431 getParentRoute: () => rootRoute,
432 path: '/',
433 validateSearch: () => ({ indexPage: 0 }),
434 })
435β
436 const invoicesRoute = createRoute({
437 getParentRoute: () => rootRoute,
438 path: 'invoices',
439 })
440β
441 const invoicesIndexRoute = createRoute({
442 getParentRoute: () => invoicesRoute,
443 path: '/',
444 })
445β
446 const invoiceRoute = createRoute({
447 getParentRoute: () => invoicesRoute,
448 path: '$invoiceId',
449 })
450β
451 const routeTree = rootRoute.addChildren([
452 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
453 indexRoute,
454 ])
455β
456 // eslint-disable-next-line unused-imports/no-unused-vars
457 const defaultRouter = createRouter({
458 routeTree,
459 })
460β
461 type DefaultRouter = typeof defaultRouter
462β
463 expectTypeOf(useSearch<DefaultRouter, '/'>).returns.toEqualTypeOf<{
464 indexPage: number
465 }>()
466β
467 expectTypeOf(
468 useSearch<DefaultRouter, '__root__'>,
469 ).returns.toEqualTypeOf<{}>()
470β
471 expectTypeOf(
472 useSearch<DefaultRouter, '/invoices'>,
473 ).returns.toEqualTypeOf<{}>()
474β
475 expectTypeOf(useSearch<DefaultRouter, '/invoices'>)
476 .parameter(0)
477 .toHaveProperty('select')
478 .toEqualTypeOf<((search: {}) => unknown) | undefined>()
479β
480 expectTypeOf(
481 useSearch<DefaultRouter, '/invoices', false>,
482 ).returns.toEqualTypeOf<{ indexPage?: number }>()
483β
484 expectTypeOf(useSearch<DefaultRouter, '/invoices', false>)
485 .parameter(0)
486 .toHaveProperty('select')
487 .toEqualTypeOf<
488 ((search: { indexPage?: number }) => unknown) | undefined
489 >()
490 })
491β
492 test('when the root has search params but the index route does not', () => {
493 const rootRoute = createRootRoute({
494 validateSearch: () => ({ rootPage: 0 }),
495 })
496β
497 const indexRoute = createRoute({
498 getParentRoute: () => rootRoute,
499 path: '/',
500 })
501β
502 const invoicesRoute = createRoute({
503 getParentRoute: () => rootRoute,
504 path: 'invoices',
505 })
506β
507 const invoicesIndexRoute = createRoute({
508 getParentRoute: () => invoicesRoute,
509 path: '/',
510 })
511β
512 const invoiceRoute = createRoute({
513 getParentRoute: () => invoicesRoute,
514 path: '$invoiceId',
515 })
516β
517 const routeTree = rootRoute.addChildren([
518 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
519 indexRoute,
520 ])
521β
522 // eslint-disable-next-line unused-imports/no-unused-vars
523 const defaultRouter = createRouter({
524 routeTree,
525 })
526β
527 type DefaultRouter = typeof defaultRouter
528β
529 expectTypeOf(useSearch<DefaultRouter, '/'>).returns.toEqualTypeOf<{
530 rootPage: number
531 }>()
532β
533 expectTypeOf(useSearch<DefaultRouter, '__root__'>).returns.toEqualTypeOf<{
534 rootPage: number
535 }>()
536β
537 expectTypeOf(useSearch<DefaultRouter, '/invoices'>).returns.toEqualTypeOf<{
538 rootPage: number
539 }>()
540β
541 expectTypeOf(useSearch<DefaultRouter, '/invoices'>)
542 .parameter(0)
543 .toHaveProperty('select')
544 .toEqualTypeOf<((search: { rootPage: number }) => unknown) | undefined>()
545β
546 expectTypeOf(
547 useSearch<DefaultRouter, '/invoices', false>,
548 ).returns.toEqualTypeOf<{ rootPage?: number }>()
549β
550 expectTypeOf(useSearch<DefaultRouter, '/invoices', false>)
551 .parameter(0)
552 .toHaveProperty('select')
553 .toEqualTypeOf<((search: { rootPage?: number }) => unknown) | undefined>()
554 })
555β
556 test('when the root has search params but the index does', () => {
557 const rootRoute = createRootRoute({
558 validateSearch: () => ({ rootPage: 0 }),
559 })
560β
561 const indexRoute = createRoute({
562 getParentRoute: () => rootRoute,
563 path: '/',
564 validateSearch: () => ({ indexPage: 0 }),
565 })
566β
567 const invoicesRoute = createRoute({
568 getParentRoute: () => rootRoute,
569 path: 'invoices',
570 })
571β
572 const invoicesIndexRoute = createRoute({
573 getParentRoute: () => invoicesRoute,
574 path: '/',
575 })
576β
577 const invoiceRoute = createRoute({
578 getParentRoute: () => invoicesRoute,
579 path: '$invoiceId',
580 })
581β
582 const routeTree = rootRoute.addChildren([
583 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
584 indexRoute,
585 ])
586β
587 // eslint-disable-next-line unused-imports/no-unused-vars
588 const defaultRouter = createRouter({
589 routeTree,
590 })
591β
592 type DefaultRouter = typeof defaultRouter
593β
594 expectTypeOf(useSearch<DefaultRouter, '/'>).returns.toEqualTypeOf<{
595 rootPage: number
596 indexPage: number
597 }>()
598β
599 expectTypeOf(useSearch<DefaultRouter, '__root__'>).returns.toEqualTypeOf<{
600 rootPage: number
601 }>()
602β
603 expectTypeOf(useSearch<DefaultRouter, '/invoices'>).returns.toEqualTypeOf<{
604 rootPage: number
605 }>()
606β
607 expectTypeOf(useSearch<DefaultRouter, '/invoices'>)
608 .parameter(0)
609 .toHaveProperty('select')
610 .toEqualTypeOf<((search: { rootPage: number }) => unknown) | undefined>()
611β
612 expectTypeOf(
613 useSearch<DefaultRouter, '/invoices', false>,
614 ).returns.toEqualTypeOf<{ indexPage?: number; rootPage?: number }>()
615β
616 expectTypeOf(useSearch<DefaultRouter, '/invoices', false>)
617 .parameter(0)
618 .toHaveProperty('select')
619 .toEqualTypeOf<
620 | ((search: { indexPage?: number; rootPage?: number }) => unknown)
621 | undefined
622 >()
623 })
624β
625 test('when a route has search params using SearchSchemaInput', () => {
626 const rootRoute = createRootRoute()
627β
628 const indexRoute = createRoute({
629 getParentRoute: () => rootRoute,
630 path: '/',
631 validateSearch: (input: { page?: number } & SearchSchemaInput) => {
632 return { page: input.page ?? 0 }
633 },
634 })
635β
636 const routeTree = rootRoute.addChildren([indexRoute])
637 // eslint-disable-next-line unused-imports/no-unused-vars
638 const router = createRouter({ routeTree })
639 expectTypeOf(useSearch<typeof router, '/'>).returns.toEqualTypeOf<{
640 page: number
641 }>
642 })
643β
644 test('when route has a union of search params', () => {
645 const rootRoute = createRootRoute()
646β
647 const postRoute = createRoute({
648 getParentRoute: () => rootRoute,
649 path: '/',
650 validateSearch: (): { status: 'in' } | { status: 'out' } => {
651 return { status: 'in' }
652 },
653 })
654β
655 const indexRoute = createRoute({
656 getParentRoute: () => postRoute,
657 path: '/',
658 validateSearch: (): { detail: string } => {
659 return { detail: 'detail' }
660 },
661 })
662β
663 const routeTree = rootRoute.addChildren([
664 indexRoute.addChildren([indexRoute]),
665 ])
666 // eslint-disable-next-line unused-imports/no-unused-vars
667 const router = createRouter({ routeTree })
668 expectTypeOf(useSearch<typeof router, '/'>).returns.toEqualTypeOf<
669 { status: 'in'; detail: string } | { status: 'out'; detail: string }
670 >
671 })
672β
673 describe('shouldThrow', () => {
674 const rootRoute = createRootRoute()
675β
676 const indexRoute = createRoute({
677 getParentRoute: () => rootRoute,
678 path: '/',
679 })
680β
681 const invoicesRoute = createRoute({
682 getParentRoute: () => rootRoute,
683 path: 'invoices',
684 validateSearch: () => ({ page: 0 }),
685 })
686β
687 const routeTree = rootRoute.addChildren([invoicesRoute, indexRoute])
688β
689 // eslint-disable-next-line unused-imports/no-unused-vars
690 const router = createRouter({ routeTree })
691β
692 const from = '/invoices'
693 test('return type is `{ page: number }` when shouldThrow = true', () => {
694 const shouldThrow = true
695 const search = useSearch<
696 typeof router,
697 typeof from,
698 /* strict */ true,
699 typeof shouldThrow
700 >({ from, shouldThrow })
701β
702 expectTypeOf(search).toEqualTypeOf<{ page: number }>()
703 })
704β
705 test('return type is `{page: number} | undefined` when shouldThrow = false', () => {
706 const shouldThrow = false
707 const search = useSearch<
708 typeof router,
709 typeof from,
710 /* strict */ true,
711 typeof shouldThrow
712 >({ from, shouldThrow })
713β
714 expectTypeOf(search).toEqualTypeOf<{ page: number } | undefined>()
715 })
716 })
717})