Open History to see the last commit
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 }
111}
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 }
1020 }
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'] }),
1025 )
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'] }),
1044 )
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'] }),
1071 )
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 })
1103})
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 }
1132 }
1133β
1134 it('should handle optional parameters with prefix/suffix correctly', async () => {
1135 const { router } = createEdgeCaseTestRouter(
1136 createMemoryHistory({ initialEntries: ['/files/prefixdocument.txt'] }),
1137 )
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'] }),
1164 )
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'] }),
1209 )
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 })
1241})
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 = [
1272 {
1273 _splat: '',
1274 },
1275 {
1276 _splat: undefined,
1277 },
1278 {},
1279 ]
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()
1292 }
1293 },
1294 )
1295})