Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2β
3import { trailingSlashOptions } from '@tanstack/router-core'
4import { waitFor } from '@solidjs/testing-library'
5import {
6 createMemoryHistory,
7 createRootRoute,
8 createRoute,
9 createRouter,
10} from '../src'
11import type { RouterHistory } from '../src'
12β
13afterEach(() => {
14 vi.clearAllMocks()
15})
16β
17function createTestRouter(initialHistory?: RouterHistory) {
18 const history =
19 initialHistory ?? createMemoryHistory({ initialEntries: ['/'] })
20β
21 const rootRoute = createRootRoute({})
22 const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/' })
23 const postsRoute = createRoute({
24 getParentRoute: () => rootRoute,
25 path: '/posts',
26 })
27 const postIdRoute = createRoute({
28 getParentRoute: () => postsRoute,
29 path: '/$slug',
30 })
31 const projectRoute = createRoute({
32 getParentRoute: () => rootRoute,
33 path: '/p',
34 })
35 const projectIdRoute = createRoute({
36 getParentRoute: () => projectRoute,
37 path: '/$projectId',
38 })
39 const projectVersionRoute = createRoute({
40 getParentRoute: () => projectIdRoute,
41 path: '/$version',
42 })
43 const projectFrameRoute = createRoute({
44 getParentRoute: () => projectVersionRoute,
45 path: '/$framework',
46 })
47β
48 const uRoute = createRoute({
49 getParentRoute: () => rootRoute,
50 path: '/u',
51 })
52 const uLayoutRoute = createRoute({
53 id: '_layout',
54 getParentRoute: () => uRoute,
55 })
56 const uUsernameRoute = createRoute({
57 getParentRoute: () => uLayoutRoute,
58 path: '$username',
59 })
60β
61 const gRoute = createRoute({
62 getParentRoute: () => rootRoute,
63 path: '/g',
64 })
65 const gLayoutRoute = createRoute({
66 id: 'layout',
67 getParentRoute: () => gRoute,
68 })
69 const gUsernameRoute = createRoute({
70 getParentRoute: () => gLayoutRoute,
71 path: '$username',
72 })
73 const searchRoute = createRoute({
74 getParentRoute: () => rootRoute,
75 path: 'search',
76 validateSearch: (search: Record<string, unknown>) => {
77 return {
78 ['foo=bar']: Number(search['foo=bar'] ?? 1),
79 }
80 },
81 })
82β
83 const projectTree = projectRoute.addChildren([
84 projectIdRoute.addChildren([
85 projectVersionRoute.addChildren([projectFrameRoute]),
86 ]),
87 ])
88 const uTree = uRoute.addChildren([uLayoutRoute.addChildren([uUsernameRoute])])
89 const gTree = gRoute.addChildren([gLayoutRoute.addChildren([gUsernameRoute])])
90β
91 const routeTree = rootRoute.addChildren([
92 indexRoute,
93 postsRoute.addChildren([postIdRoute]),
94 projectTree,
95 uTree,
96 gTree,
97 ])
98 const router = createRouter({ routeTree, history })
99β
100 return {
101 router,
102 routes: {
103 indexRoute,
104 postsRoute,
105 postIdRoute,
106 projectRoute,
107 projectIdRoute,
108 projectVersionRoute,
109 projectFrameRoute,
110 },
111 }
112}
113β
114describe('router.navigate navigation using a single path param - object syntax for updates', () => {
115 it('should change $slug in "/posts/$slug" from "tanner" to "tkdodo"', async () => {
116 const { router } = createTestRouter(
117 createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
118 )
119β
120 await router.load()
121β
122 expect(router.state.location.pathname).toBe('/posts/tanner')
123β
124 await router.navigate({
125 to: '/posts/$slug',
126 params: { slug: 'tkdodo' },
127 })
128 await router.invalidate()
129β
130 expect(router.state.location.pathname).toBe('/posts/tkdodo')
131 })
132β
133 it('should change $slug in "/posts/$slug" from "tanner" to "tkdodo" w/o "to" path being provided', async () => {
134 const { router } = createTestRouter(
135 createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
136 )
137β
138 await router.load()
139β
140 expect(router.state.location.pathname).toBe('/posts/tanner')
141β
142 await router.navigate({
143 params: { slug: 'tkdodo' },
144 } as any)
145 await router.invalidate()
146β
147 expect(router.state.location.pathname).toBe('/posts/tkdodo')
148 })
149})
150β
151describe('router.navigate navigation using a single path param - function syntax for updates', () => {
152 it('should change $slug in "/posts/$slug" from "tanner" to "tkdodo"', async () => {
153 const { router } = createTestRouter(
154 createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
155 )
156β
157 await router.load()
158β
159 expect(router.state.location.pathname).toBe('/posts/tanner')
160β
161 await router.navigate({
162 to: '/posts/$slug',
163 params: (p: any) => ({ ...p, slug: 'tkdodo' }),
164 })
165 await router.invalidate()
166β
167 expect(router.state.location.pathname).toBe('/posts/tkdodo')
168 })
169β
170 it('should change $slug in "/posts/$slug" from "tanner" to "tkdodo" w/o "to" path being provided', async () => {
171 const { router } = createTestRouter(
172 createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
173 )
174β
175 await router.load()
176β
177 expect(router.state.location.pathname).toBe('/posts/tanner')
178β
179 await router.navigate({
180 params: (p: any) => ({ ...p, slug: 'tkdodo' }),
181 } as any)
182 await router.invalidate()
183β
184 expect(router.state.location.pathname).toBe('/posts/tkdodo')
185 })
186})
187β
188describe('router.navigate navigation using multiple path params - object syntax for updates', () => {
189 it('should change $projectId in "/p/$projectId/$version/$framework" from "router" to "query"', async () => {
190 const { router } = createTestRouter(
191 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
192 )
193β
194 await router.load()
195β
196 expect(router.state.location.pathname).toBe('/p/router/v1/react')
197β
198 await router.navigate({
199 to: '/p/$projectId/$version/$framework',
200 params: { projectId: 'query' },
201 })
202 await router.invalidate()
203β
204 expect(router.state.location.pathname).toBe('/p/query/v1/react')
205 })
206β
207 it('should change $projectId in "/p/$projectId/$version/$framework" from "router" to "query" w/o "to" path being provided', async () => {
208 const { router } = createTestRouter(
209 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
210 )
211β
212 await router.load()
213β
214 expect(router.state.location.pathname).toBe('/p/router/v1/react')
215β
216 await router.navigate({
217 params: { projectId: 'query' },
218 } as any)
219 await router.invalidate()
220β
221 expect(router.state.location.pathname).toBe('/p/query/v1/react')
222 })
223β
224 it('should change $version in "/p/$projectId/$version/$framework" from "v1" to "v3"', async () => {
225 const { router } = createTestRouter(
226 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
227 )
228β
229 await router.load()
230β
231 expect(router.state.location.pathname).toBe('/p/router/v1/react')
232β
233 await router.navigate({
234 to: '/p/$projectId/$version/$framework',
235 params: { version: 'v3' },
236 })
237 await router.invalidate()
238β
239 expect(router.state.location.pathname).toBe('/p/router/v3/react')
240 })
241β
242 it('should change $version in "/p/$projectId/$version/$framework" from "v1" to "v3" w/o "to" path being provided', async () => {
243 const { router } = createTestRouter(
244 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
245 )
246β
247 await router.load()
248β
249 expect(router.state.location.pathname).toBe('/p/router/v1/react')
250β
251 await router.navigate({
252 params: { version: 'v3' },
253 } as any)
254 await router.invalidate()
255β
256 expect(router.state.location.pathname).toBe('/p/router/v3/react')
257 })
258β
259 it('should change $framework in "/p/$projectId/$version/$framework" from "react" to "vue"', async () => {
260 const { router } = createTestRouter(
261 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
262 )
263β
264 await router.load()
265β
266 expect(router.state.location.pathname).toBe('/p/router/v1/react')
267β
268 await router.navigate({
269 to: '/p/$projectId/$version/$framework',
270 params: { framework: 'vue' },
271 })
272 await router.invalidate()
273β
274 expect(router.state.location.pathname).toBe('/p/router/v1/vue')
275 })
276β
277 it('should change $framework in "/p/$projectId/$version/$framework" from "react" to "vue" w/o "to" path being provided', async () => {
278 const { router } = createTestRouter(
279 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
280 )
281β
282 await router.load()
283β
284 expect(router.state.location.pathname).toBe('/p/router/v1/react')
285β
286 await router.navigate({
287 params: { framework: 'vue' },
288 } as any)
289 await router.invalidate()
290β
291 expect(router.state.location.pathname).toBe('/p/router/v1/vue')
292 })
293})
294β
295describe('router.navigate navigation using multiple path params - function syntax for updates', () => {
296 it('should change $projectId in "/p/$projectId/$version/$framework" from "router" to "query"', async () => {
297 const { router } = createTestRouter(
298 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
299 )
300β
301 await router.load()
302β
303 expect(router.state.location.pathname).toBe('/p/router/v1/react')
304β
305 await router.navigate({
306 to: '/p/$projectId/$version/$framework',
307 params: (p: any) => ({ ...p, projectId: 'query' }),
308 })
309 await router.invalidate()
310β
311 expect(router.state.location.pathname).toBe('/p/query/v1/react')
312 })
313β
314 it('should change $projectId in "/p/$projectId/$version/$framework" from "router" to "query" w/o "to" path being provided', async () => {
315 const { router } = createTestRouter(
316 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
317 )
318β
319 await router.load()
320β
321 expect(router.state.location.pathname).toBe('/p/router/v1/react')
322β
323 await router.navigate({
324 params: (p: any) => ({ ...p, projectId: 'query' }),
325 } as any)
326 await router.invalidate()
327β
328 expect(router.state.location.pathname).toBe('/p/query/v1/react')
329 })
330β
331 it('should change $version in "/p/$projectId/$version/$framework" from "v1" to "v3"', async () => {
332 const { router } = createTestRouter(
333 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
334 )
335β
336 await router.load()
337β
338 expect(router.state.location.pathname).toBe('/p/router/v1/react')
339β
340 await router.navigate({
341 to: '/p/$projectId/$version/$framework',
342 params: (p: any) => ({ ...p, version: 'v3' }),
343 })
344 await router.invalidate()
345β
346 expect(router.state.location.pathname).toBe('/p/router/v3/react')
347 })
348β
349 it('should change $version in "/p/$projectId/$version/$framework" from "v1" to "v3" w/o "to" path being provided', async () => {
350 const { router } = createTestRouter(
351 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
352 )
353β
354 await router.load()
355β
356 expect(router.state.location.pathname).toBe('/p/router/v1/react')
357β
358 await router.navigate({
359 params: (p: any) => ({ ...p, version: 'v3' }),
360 } as any)
361 await router.invalidate()
362β
363 expect(router.state.location.pathname).toBe('/p/router/v3/react')
364 })
365β
366 it('should change $framework in "/p/$projectId/$version/$framework" from "react" to "vue"', async () => {
367 const { router } = createTestRouter(
368 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
369 )
370β
371 await router.load()
372β
373 expect(router.state.location.pathname).toBe('/p/router/v1/react')
374β
375 await router.navigate({
376 to: '/p/$projectId/$version/$framework',
377 params: (p: any) => ({ ...p, framework: 'vue' }),
378 })
379 await router.invalidate()
380β
381 expect(router.state.location.pathname).toBe('/p/router/v1/vue')
382 })
383β
384 it('should change $framework in "/p/$projectId/$version/$framework" from "react" to "vue" w/o "to" path being provided', async () => {
385 const { router } = createTestRouter(
386 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
387 )
388β
389 await router.load()
390β
391 expect(router.state.location.pathname).toBe('/p/router/v1/react')
392β
393 await router.navigate({
394 params: (p: any) => ({ ...p, framework: 'vue' }),
395 } as any)
396 await router.invalidate()
397β
398 expect(router.state.location.pathname).toBe('/p/router/v1/vue')
399 })
400})
401β
402describe('router.navigate navigation using layout routes resolves correctly', () => {
403 it('should resolve "/u/tanner" in "/u/_layout/$username" to "/u/tkdodo"', async () => {
404 const { router } = createTestRouter(
405 createMemoryHistory({ initialEntries: ['/u/tanner'] }),
406 )
407β
408 await router.load()
409β
410 expect(router.state.location.pathname).toBe('/u/tanner')
411β
412 await router.navigate({
413 to: '/u/$username',
414 params: { username: 'tkdodo' },
415 })
416β
417 await router.invalidate()
418β
419 expect(router.state.location.pathname).toBe('/u/tkdodo')
420 })
421β
422 it('should resolve "/u/tanner" in "/u/_layout/$username" to "/u/tkdodo" w/o "to" path being provided', async () => {
423 const { router } = createTestRouter(
424 createMemoryHistory({ initialEntries: ['/u/tanner'] }),
425 )
426β
427 await router.load()
428β
429 expect(router.state.location.pathname).toBe('/u/tanner')
430β
431 await router.navigate({
432 params: { username: 'tkdodo' },
433 } as any)
434 await router.invalidate()
435β
436 expect(router.state.location.pathname).toBe('/u/tkdodo')
437 })
438β
439 it('should resolve "/g/tanner" in "/g/layout/$username" to "/g/tkdodo"', async () => {
440 const { router } = createTestRouter(
441 createMemoryHistory({ initialEntries: ['/g/tanner'] }),
442 )
443β
444 await router.load()
445β
446 expect(router.state.location.pathname).toBe('/g/tanner')
447β
448 await router.navigate({
449 to: '/g/$username',
450 params: { username: 'tkdodo' },
451 })
452β
453 await router.invalidate()
454β
455 expect(router.state.location.pathname).toBe('/g/tkdodo')
456 })
457β
458 it('should resolve "/g/tanner" in "/g/layout/$username" to "/g/tkdodo" w/o "to" path being provided', async () => {
459 const { router } = createTestRouter(
460 createMemoryHistory({ initialEntries: ['/g/tanner'] }),
461 )
462β
463 await router.load()
464β
465 expect(router.state.location.pathname).toBe('/g/tanner')
466β
467 await router.navigate({
468 params: { username: 'tkdodo' },
469 } as any)
470 await router.invalidate()
471β
472 expect(router.state.location.pathname).toBe('/g/tkdodo')
473 })
474β
475 it('should handle search params with special characters', async () => {
476 const { router } = createTestRouter(
477 createMemoryHistory({ initialEntries: ['/search?foo%3Dbar=2'] }),
478 )
479β
480 await router.load()
481β
482 expect(router.state.location.pathname).toBe('/search')
483 expect(router.state.location.search).toStrictEqual({ 'foo=bar': 2 })
484β
485 await router.navigate({
486 search: { 'foo=bar': 3 },
487 } as any)
488 await router.invalidate()
489β
490 expect(router.state.location.search).toStrictEqual({ 'foo=bar': 3 })
491 })
492})
493β
494describe('relative navigation', () => {
495 it('should navigate to a child route', async () => {
496 const { router } = createTestRouter(
497 createMemoryHistory({ initialEntries: ['/posts'] }),
498 )
499β
500 await router.load()
501β
502 expect(router.state.location.pathname).toBe('/posts')
503β
504 await router.navigate({
505 from: '/posts',
506 to: './$slug',
507 params: { slug: 'tkdodo' },
508 })
509β
510 await router.invalidate()
511β
512 expect(router.state.location.pathname).toBe('/posts/tkdodo')
513 })
514β
515 it('should navigate to a parent route', async () => {
516 const { router } = createTestRouter(
517 createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
518 )
519β
520 await router.load()
521β
522 expect(router.state.location.pathname).toBe('/posts/tanner')
523β
524 await router.navigate({
525 to: '..',
526 })
527β
528 await router.invalidate()
529β
530 expect(router.state.location.pathname).toBe('/posts')
531 })
532β
533 it('should navigate to a sibling route', async () => {
534 const { router } = createTestRouter(
535 createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
536 )
537β
538 await router.load()
539β
540 expect(router.state.location.pathname).toBe('/posts/tanner')
541β
542 await router.navigate({
543 from: '/posts/$slug',
544 to: '.',
545 params: { slug: 'tkdodo' },
546 })
547β
548 await router.invalidate()
549β
550 expect(router.state.location.pathname).toBe('/posts/tkdodo')
551 })
552β
553 it('should navigate to a sibling route without from', async () => {
554 const { router } = createTestRouter(
555 createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
556 )
557β
558 await router.load()
559β
560 expect(router.state.location.pathname).toBe('/posts/tanner')
561β
562 await router.navigate({
563 to: '.',
564 params: { slug: 'tkdodo' },
565 })
566β
567 await router.invalidate()
568β
569 expect(router.state.location.pathname).toBe('/posts/tkdodo')
570 })
571})
572β
573describe('splat routes with empty splat', () => {
574 it.each(Object.values(trailingSlashOptions))(
575 'should handle empty _splat parameter with trailingSlash: %s',
576 async (trailingSlash) => {
577 const tail = trailingSlash === 'always' ? '/' : ''
578β
579 const history = createMemoryHistory({ initialEntries: ['/'] })
580β
581 const rootRoute = createRootRoute()
582 const indexRoute = createRoute({
583 getParentRoute: () => rootRoute,
584 path: '/',
585 })
586β
587 const splatRoute = createRoute({
588 getParentRoute: () => rootRoute,
589 path: 'splat/$',
590 })
591β
592 const router = createRouter({
593 routeTree: rootRoute.addChildren([indexRoute, splatRoute]),
594 history,
595 trailingSlash,
596 })
597β
598 await router.load()
599β
600 // All of these route params should navigate to the same location
601 const paramSets = [
602 {
603 _splat: '',
604 },
605 {
606 _splat: undefined,
607 },
608 {},
609 ]
610β
611 for (const params of paramSets) {
612 await router.navigate({
613 to: '/splat/$',
614 params,
615 })
616 await router.invalidate()
617β
618 await waitFor(() => {
619 expect(router.state.location.pathname).toBe(`/splat${tail}`)
620 })
621β
622 // Navigate back to index
623 await router.navigate({ to: '/' })
624 await router.invalidate()
625 }
626 },
627 )
628})
629β
630describe('router.navigate navigation using optional path parameters - object syntax for updates', () => {
631 function createOptionalParamTestRouter(initialHistory?: RouterHistory) {
632 const history =
633 initialHistory ?? createMemoryHistory({ initialEntries: ['/'] })
634β
635 const rootRoute = createRootRoute({})
636 const indexRoute = createRoute({
637 getParentRoute: () => rootRoute,
638 path: '/',
639 })
640β
641 // Single optional parameter
642 const postsRoute = createRoute({
643 getParentRoute: () => rootRoute,
644 path: '/posts/{-$category}',
645 })
646β
647 // Multiple optional parameters
648 const articlesRoute = createRoute({
649 getParentRoute: () => rootRoute,
650 path: '/articles/{-$category}/{-$slug}',
651 })
652β
653 // Mixed required and optional parameters
654 const projectRoute = createRoute({
655 getParentRoute: () => rootRoute,
656 path: '/p/$projectId/{-$version}/{-$framework}',
657 })
658β
659 const routeTree = rootRoute.addChildren([
660 indexRoute,
661 postsRoute,
662 articlesRoute,
663 projectRoute,
664 ])
665 const router = createRouter({ routeTree, history })
666β
667 return {
668 router,
669 routes: {
670 indexRoute,
671 postsRoute,
672 articlesRoute,
673 projectRoute,
674 },
675 }
676 }
677β
678 it('should navigate from "/posts/tech" to "/posts" by setting category to undefined', async () => {
679 const { router } = createOptionalParamTestRouter(
680 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
681 )
682β
683 await router.load()
684 expect(router.state.location.pathname).toBe('/posts/tech')
685β
686 await router.navigate({
687 to: '/posts/{-$category}',
688 params: { category: undefined },
689 })
690 await router.invalidate()
691β
692 expect(router.state.location.pathname).toBe('/posts')
693 })
694β
695 it('should navigate from "/posts" to "/posts/tech" by setting category', async () => {
696 const { router } = createOptionalParamTestRouter(
697 createMemoryHistory({ initialEntries: ['/posts'] }),
698 )
699β
700 await router.load()
701 expect(router.state.location.pathname).toBe('/posts')
702β
703 await router.navigate({
704 to: '/posts/{-$category}',
705 params: { category: 'tech' },
706 })
707 await router.invalidate()
708β
709 expect(router.state.location.pathname).toBe('/posts/tech')
710 })
711β
712 it('should navigate from "/posts/tech" to "/posts/news" by changing category', async () => {
713 const { router } = createOptionalParamTestRouter(
714 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
715 )
716β
717 await router.load()
718 expect(router.state.location.pathname).toBe('/posts/tech')
719β
720 await router.navigate({
721 to: '/posts/{-$category}',
722 params: { category: 'news' },
723 })
724 await router.invalidate()
725β
726 expect(router.state.location.pathname).toBe('/posts/news')
727 })
728β
729 it('should navigate without "to" path and set category to undefined', async () => {
730 const { router } = createOptionalParamTestRouter(
731 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
732 )
733β
734 await router.load()
735 expect(router.state.location.pathname).toBe('/posts/tech')
736β
737 await router.navigate({
738 params: { category: undefined },
739 } as any)
740 await router.invalidate()
741β
742 expect(router.state.location.pathname).toBe('/posts')
743 })
744β
745 it('should handle multiple optional parameters - set one to undefined', async () => {
746 const { router } = createOptionalParamTestRouter(
747 createMemoryHistory({ initialEntries: ['/articles/tech/hello-world'] }),
748 )
749β
750 await router.load()
751 expect(router.state.location.pathname).toBe('/articles/tech/hello-world')
752β
753 await router.navigate({
754 to: '/articles/{-$category}/{-$slug}',
755 params: { category: 'tech', slug: undefined },
756 })
757 await router.invalidate()
758β
759 expect(router.state.location.pathname).toBe('/articles/tech')
760 })
761β
762 it('should handle multiple optional parameters - set both to undefined', async () => {
763 const { router } = createOptionalParamTestRouter(
764 createMemoryHistory({ initialEntries: ['/articles/tech/hello-world'] }),
765 )
766β
767 await router.load()
768 expect(router.state.location.pathname).toBe('/articles/tech/hello-world')
769β
770 await router.navigate({
771 to: '/articles/{-$category}/{-$slug}',
772 params: { category: undefined, slug: undefined },
773 })
774 await router.invalidate()
775β
776 expect(router.state.location.pathname).toBe('/articles')
777 })
778β
779 it('should handle mixed required and optional parameters', async () => {
780 const { router } = createOptionalParamTestRouter(
781 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
782 )
783β
784 await router.load()
785 expect(router.state.location.pathname).toBe('/p/router/v1/react')
786β
787 await router.navigate({
788 to: '/p/$projectId/{-$version}/{-$framework}',
789 params: { projectId: 'router', version: undefined, framework: 'vue' },
790 })
791 await router.invalidate()
792β
793 expect(router.state.location.pathname).toBe('/p/router/vue')
794 })
795β
796 it('should carry over optional parameters from current route when using empty params', async () => {
797 const { router } = createOptionalParamTestRouter(
798 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
799 )
800β
801 await router.load()
802 expect(router.state.location.pathname).toBe('/posts/tech')
803β
804 // Navigate to a different route with optional params
805 await router.navigate({
806 to: '/articles/{-$category}/{-$slug}',
807 params: { category: 'news' },
808 })
809 await router.invalidate()
810β
811 expect(router.state.location.pathname).toBe('/articles/news')
812β
813 // Navigate back to posts - should carry over 'news' from current params
814 await router.navigate({
815 to: '/posts/{-$category}',
816 params: {},
817 })
818 await router.invalidate()
819β
820 expect(router.state.location.pathname).toBe('/posts/news')
821 })
822})
823β
824describe('router.navigate navigation using optional path parameters - function syntax for updates', () => {
825 function createOptionalParamTestRouter(initialHistory?: RouterHistory) {
826 const history =
827 initialHistory ?? createMemoryHistory({ initialEntries: ['/'] })
828β
829 const rootRoute = createRootRoute({})
830 const indexRoute = createRoute({
831 getParentRoute: () => rootRoute,
832 path: '/',
833 })
834β
835 const postsRoute = createRoute({
836 getParentRoute: () => rootRoute,
837 path: '/posts/{-$category}',
838 })
839β
840 const articlesRoute = createRoute({
841 getParentRoute: () => rootRoute,
842 path: '/articles/{-$category}/{-$slug}',
843 })
844β
845 const projectRoute = createRoute({
846 getParentRoute: () => rootRoute,
847 path: '/p/$projectId/{-$version}/{-$framework}',
848 })
849β
850 const routeTree = rootRoute.addChildren([
851 indexRoute,
852 postsRoute,
853 articlesRoute,
854 projectRoute,
855 ])
856 const router = createRouter({ routeTree, history })
857β
858 return { router }
859 }
860β
861 it('should navigate from "/posts/tech" to "/posts" by setting category to undefined using function', async () => {
862 const { router } = createOptionalParamTestRouter(
863 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
864 )
865β
866 await router.load()
867 expect(router.state.location.pathname).toBe('/posts/tech')
868β
869 await router.navigate({
870 to: '/posts/{-$category}',
871 params: (p: any) => ({ ...p, category: undefined }),
872 })
873 await router.invalidate()
874β
875 expect(router.state.location.pathname).toBe('/posts')
876 })
877β
878 it('should navigate from "/posts/tech" to "/posts" by setting category to undefined in function', async () => {
879 const { router } = createOptionalParamTestRouter(
880 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
881 )
882β
883 await router.load()
884 expect(router.state.location.pathname).toBe('/posts/tech')
885β
886 await router.navigate({
887 to: '/posts/{-$category}',
888 params: (p: any) => ({ ...p, category: undefined }),
889 })
890 await router.invalidate()
891β
892 expect(router.state.location.pathname).toBe('/posts')
893 })
894β
895 it('should navigate from "/posts" to "/posts/tech" by setting category using function', async () => {
896 const { router } = createOptionalParamTestRouter(
897 createMemoryHistory({ initialEntries: ['/posts'] }),
898 )
899β
900 await router.load()
901 expect(router.state.location.pathname).toBe('/posts')
902β
903 await router.navigate({
904 to: '/posts/{-$category}',
905 params: (p: any) => ({ ...p, category: 'tech' }),
906 })
907 await router.invalidate()
908β
909 expect(router.state.location.pathname).toBe('/posts/tech')
910 })
911β
912 it('should navigate from "/posts/tech" to "/posts/news" by changing category using function', async () => {
913 const { router } = createOptionalParamTestRouter(
914 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
915 )
916β
917 await router.load()
918 expect(router.state.location.pathname).toBe('/posts/tech')
919β
920 await router.navigate({
921 to: '/posts/{-$category}',
922 params: (p: any) => ({ ...p, category: 'news' }),
923 })
924 await router.invalidate()
925β
926 expect(router.state.location.pathname).toBe('/posts/news')
927 })
928β
929 it('should navigate without "to" path and set category to undefined using function', async () => {
930 const { router } = createOptionalParamTestRouter(
931 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
932 )
933β
934 await router.load()
935 expect(router.state.location.pathname).toBe('/posts/tech')
936β
937 await router.navigate({
938 params: (p: any) => ({ ...p, category: undefined }),
939 } as any)
940 await router.invalidate()
941β
942 expect(router.state.location.pathname).toBe('/posts')
943 })
944β
945 it('should handle multiple optional parameters - remove one using function', async () => {
946 const { router } = createOptionalParamTestRouter(
947 createMemoryHistory({ initialEntries: ['/articles/tech/hello-world'] }),
948 )
949β
950 await router.load()
951 expect(router.state.location.pathname).toBe('/articles/tech/hello-world')
952β
953 await router.navigate({
954 to: '/articles/{-$category}/{-$slug}',
955 params: (p: any) => ({ ...p, slug: undefined }),
956 })
957 await router.invalidate()
958β
959 expect(router.state.location.pathname).toBe('/articles/tech')
960 })
961β
962 it('should handle multiple optional parameters - clear all using function', async () => {
963 const { router } = createOptionalParamTestRouter(
964 createMemoryHistory({ initialEntries: ['/articles/tech/hello-world'] }),
965 )
966β
967 await router.load()
968 expect(router.state.location.pathname).toBe('/articles/tech/hello-world')
969β
970 await router.navigate({
971 to: '/articles/{-$category}/{-$slug}',
972 params: (p: any) => ({ ...p, category: undefined, slug: undefined }),
973 })
974 await router.invalidate()
975β
976 expect(router.state.location.pathname).toBe('/articles')
977 })
978β
979 it('should handle mixed required and optional parameters using function', async () => {
980 const { router } = createOptionalParamTestRouter(
981 createMemoryHistory({ initialEntries: ['/p/router/v1/react'] }),
982 )
983β
984 await router.load()
985 expect(router.state.location.pathname).toBe('/p/router/v1/react')
986β
987 await router.navigate({
988 to: '/p/$projectId/{-$version}/{-$framework}',
989 params: (p: any) => ({
990 ...p,
991 projectId: 'router',
992 version: undefined,
993 framework: 'vue',
994 }),
995 })
996 await router.invalidate()
997β
998 expect(router.state.location.pathname).toBe('/p/router/vue')
999 })
1000})
1001β
1002describe('router.navigate navigation using optional path parameters - parameter inheritance and isolation', () => {
1003 function createInheritanceTestRouter(initialHistory?: RouterHistory) {
1004 const history =
1005 initialHistory ?? createMemoryHistory({ initialEntries: ['/'] })
1006β
1007 const rootRoute = createRootRoute({})
1008 const indexRoute = createRoute({
1009 getParentRoute: () => rootRoute,
1010 path: '/',
1011 })
1012β
1013 // Route with same optional param names but different paths
1014 const postsRoute = createRoute({
1015 getParentRoute: () => rootRoute,
1016 path: '/posts/{-$category}',
1017 })
1018β
1019 const articlesRoute = createRoute({
1020 getParentRoute: () => rootRoute,
1021 path: '/articles/{-$category}',
1022 })
1023β
1024 // Route with nested optional params
1025 const docsRoute = createRoute({
1026 getParentRoute: () => rootRoute,
1027 path: '/docs/{-$version}',
1028 })
1029β
1030 const docsTopicRoute = createRoute({
1031 getParentRoute: () => docsRoute,
1032 path: '/{-$topic}',
1033 })
1034β
1035 const routeTree = rootRoute.addChildren([
1036 indexRoute,
1037 postsRoute,
1038 articlesRoute,
1039 docsRoute.addChildren([docsTopicRoute]),
1040 ])
1041 const router = createRouter({ routeTree, history })
1042β
1043 return { router }
1044 }
1045β
1046 it('should carry over optional parameters between different routes with same param names', async () => {
1047 const { router } = createInheritanceTestRouter(
1048 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
1049 )
1050β
1051 await router.load()
1052 expect(router.state.location.pathname).toBe('/posts/tech')
1053β
1054 // Navigate to articles without specifying category
1055 await router.navigate({
1056 to: '/articles/{-$category}',
1057 params: {},
1058 })
1059 await router.invalidate()
1060β
1061 // Should carry over 'tech' from current route params
1062 expect(router.state.location.pathname).toBe('/articles/tech')
1063 })
1064β
1065 it('should properly handle navigation between routes with different optional param structures', async () => {
1066 const { router } = createInheritanceTestRouter(
1067 createMemoryHistory({ initialEntries: ['/posts/tech'] }),
1068 )
1069β
1070 await router.load()
1071 expect(router.state.location.pathname).toBe('/posts/tech')
1072β
1073 // Navigate to articles with explicit category
1074 await router.navigate({
1075 to: '/articles/{-$category}',
1076 params: { category: 'news' },
1077 })
1078 await router.invalidate()
1079β
1080 expect(router.state.location.pathname).toBe('/articles/news')
1081β
1082 // Navigate back to posts without explicit category removal
1083 await router.navigate({
1084 to: '/posts/{-$category}',
1085 params: {},
1086 })
1087 await router.invalidate()
1088β
1089 expect(router.state.location.pathname).toBe('/posts/news')
1090 })
1091β
1092 it('should handle nested optional parameters correctly', async () => {
1093 const { router } = createInheritanceTestRouter(
1094 createMemoryHistory({ initialEntries: ['/docs/v1/getting-started'] }),
1095 )
1096β
1097 await router.load()
1098 expect(router.state.location.pathname).toBe('/docs/v1/getting-started')
1099β
1100 // Remove topic but keep version
1101 await router.navigate({
1102 to: '/docs/{-$version}/{-$topic}',
1103 params: { version: 'v1', topic: undefined },
1104 })
1105 await router.invalidate()
1106β
1107 expect(router.state.location.pathname).toBe('/docs/v1')
1108β
1109 // Remove version but add topic
1110 await router.navigate({
1111 to: '/docs/{-$version}/{-$topic}',
1112 params: { version: undefined, topic: 'api' },
1113 })
1114 await router.invalidate()
1115β
1116 expect(router.state.location.pathname).toBe('/docs/api')
1117β
1118 // Remove both
1119 await router.navigate({
1120 to: '/docs/{-$version}/{-$topic}',
1121 params: { version: undefined, topic: undefined },
1122 })
1123 await router.invalidate()
1124β
1125 expect(router.state.location.pathname).toBe('/docs')
1126 })
1127})
1128β
1129describe('router.navigate navigation using optional path parameters - edge cases and validations', () => {
1130 function createEdgeCaseTestRouter(initialHistory?: RouterHistory) {
1131 const history =
1132 initialHistory ?? createMemoryHistory({ initialEntries: ['/'] })
1133β
1134 const rootRoute = createRootRoute({})
1135 const indexRoute = createRoute({
1136 getParentRoute: () => rootRoute,
1137 path: '/',
1138 })
1139β
1140 // Route with prefix/suffix
1141 const filesRoute = createRoute({
1142 getParentRoute: () => rootRoute,
1143 path: '/files/prefix{-$name}.txt',
1144 })
1145β
1146 // Route with all optional params
1147 const dateRoute = createRoute({
1148 getParentRoute: () => rootRoute,
1149 path: '/date/{-$year}/{-$month}/{-$day}',
1150 })
1151β
1152 const routeTree = rootRoute.addChildren([indexRoute, filesRoute, dateRoute])
1153 const router = createRouter({ routeTree, history })
1154β
1155 return { router }
1156 }
1157β
1158 it('should handle optional parameters with prefix/suffix correctly', async () => {
1159 const { router } = createEdgeCaseTestRouter(
1160 createMemoryHistory({ initialEntries: ['/files/prefixdocument.txt'] }),
1161 )
1162β
1163 await router.load()
1164 expect(router.state.location.pathname).toBe('/files/prefixdocument.txt')
1165β
1166 // Remove the name parameter
1167 await router.navigate({
1168 to: '/files/prefix{-$name}.txt',
1169 params: { name: undefined },
1170 })
1171 await router.invalidate()
1172β
1173 expect(router.state.location.pathname).toBe('/files/prefix.txt')
1174β
1175 // Add the name parameter back
1176 await router.navigate({
1177 to: '/files/prefix{-$name}.txt',
1178 params: { name: 'report' },
1179 })
1180 await router.invalidate()
1181β
1182 expect(router.state.location.pathname).toBe('/files/prefixreport.txt')
1183 })
1184β
1185 it('should handle route with all optional parameters', async () => {
1186 const { router } = createEdgeCaseTestRouter(
1187 createMemoryHistory({ initialEntries: ['/date/2024/03/15'] }),
1188 )
1189β
1190 await router.load()
1191 expect(router.state.location.pathname).toBe('/date/2024/03/15')
1192β
1193 // Remove day only
1194 await router.navigate({
1195 to: '/date/{-$year}/{-$month}/{-$day}',
1196 params: { year: '2024', month: '03', day: undefined },
1197 })
1198 await router.invalidate()
1199β
1200 expect(router.state.location.pathname).toBe('/date/2024/03')
1201β
1202 // Remove month and day
1203 await router.navigate({
1204 to: '/date/{-$year}/{-$month}/{-$day}',
1205 params: { year: '2024', month: undefined, day: undefined },
1206 })
1207 await router.invalidate()
1208β
1209 expect(router.state.location.pathname).toBe('/date/2024')
1210β
1211 // Remove all parameters
1212 await router.navigate({
1213 to: '/date/{-$year}/{-$month}/{-$day}',
1214 params: { year: undefined, month: undefined, day: undefined },
1215 })
1216 await router.invalidate()
1217β
1218 expect(router.state.location.pathname).toBe('/date')
1219β
1220 // Add all parameters back
1221 await router.navigate({
1222 to: '/date/{-$year}/{-$month}/{-$day}',
1223 params: { year: '2025', month: '12', day: '31' },
1224 })
1225 await router.invalidate()
1226β
1227 expect(router.state.location.pathname).toBe('/date/2025/12/31')
1228 })
1229β
1230 it('should handle empty string vs undefined distinction', async () => {
1231 const { router } = createEdgeCaseTestRouter(
1232 createMemoryHistory({ initialEntries: ['/files/prefix.txt'] }),
1233 )
1234β
1235 await router.load()
1236 expect(router.state.location.pathname).toBe('/files/prefix.txt')
1237β
1238 // Set name to empty string (should still include the param)
1239 await router.navigate({
1240 to: '/files/prefix{-$name}.txt',
1241 params: { name: '' },
1242 })
1243 await router.invalidate()
1244β
1245 expect(router.state.location.pathname).toBe('/files/prefix.txt')
1246β
1247 // Set name to a value
1248 await router.navigate({
1249 to: '/files/prefix{-$name}.txt',
1250 params: { name: 'test' },
1251 })
1252 await router.invalidate()
1253β
1254 expect(router.state.location.pathname).toBe('/files/prefixtest.txt')
1255β
1256 // Set name to undefined (should remove the param)
1257 await router.navigate({
1258 to: '/files/prefix{-$name}.txt',
1259 params: { name: undefined },
1260 })
1261 await router.invalidate()
1262β
1263 expect(router.state.location.pathname).toBe('/files/prefix.txt')
1264 })
1265})