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

navigate.test.tsx

1,265 lines36.1 KB

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 }
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 })
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 }
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'] }),
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'] }),
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'] }),
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 })
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 }
1157​
1158 it('should handle optional parameters with prefix/suffix correctly', async () => {
1159 const { router } = createEdgeCaseTestRouter(
1160 createMemoryHistory({ initialEntries: ['/files/prefixdocument.txt'] }),
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'] }),
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'] }),
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 })