Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

navigate.test.tsx

1,295 lines36.9 KB

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