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

path.test.ts

1,024 lines28.2 KB

1import { describe, expect, it } from 'vitest'
2import {
3 SEGMENT_TYPE_PARAM,
4 SEGMENT_TYPE_PATHNAME,
5 SEGMENT_TYPE_WILDCARD,
6 exactPathTest,
7 interpolatePath,
8 matchPathname,
9 parsePathname,
10 removeTrailingSlash,
11 resolvePath,
12 trimPathLeft,
13} from '../src/path'
14import type { Segment as PathSegment } from '../src/path'
15​
16describe.each([{ basepath: '/' }, { basepath: '/app' }, { basepath: '/app/' }])(
17 'removeTrailingSlash with basepath $basepath',
18 ({ basepath }) => {
19 it('should remove trailing slash if present', () => {
20 const input = 'https://example.com/'
21 const expectedOutput = 'https://example.com'
22 const result = removeTrailingSlash(input, basepath)
23 expect(result).toBe(expectedOutput)
24 })
25 it('should not modify the string if no trailing slash present', () => {
26 const input = 'https://example.com'
27 const result = removeTrailingSlash(input, basepath)
28 expect(result).toBe(input)
29 })
30 it('should handle empty string', () => {
31 const input = ''
32 const result = removeTrailingSlash(input, basepath)
33 expect(result).toBe(input)
34 })
35 it('should handle strings with only a slash', () => {
36 const input = '/'
37 const result = removeTrailingSlash(input, basepath)
38 expect(result).toBe(input)
39 })
40 it('should handle strings with multiple slashes', () => {
41 const input = 'https://example.com/path/to/resource/'
42 const expectedOutput = 'https://example.com/path/to/resource'
43 const result = removeTrailingSlash(input, basepath)
44 expect(result).toBe(expectedOutput)
45 })
46 },
48​
49describe.each([{ basepath: '/' }, { basepath: '/app' }, { basepath: '/app/' }])(
50 'exactPathTest with basepath $basepath',
51 ({ basepath }) => {
52 it('should return true when two paths are exactly the same', () => {
53 const path1 = 'some-path/additional-path'
54 const path2 = 'some-path/additional-path'
55 const result = exactPathTest(path1, path2, basepath)
56 expect(result).toBe(true)
57 })
58 it('should return true when two paths are the same with or without trailing slash', () => {
59 const path1 = 'some-path/additional-path'
60 const path2 = 'some-path/additional-path/'
61 const result = exactPathTest(path1, path2, basepath)
62 expect(result).toBe(true)
63 })
64 it('should return true when two paths are the same with or without trailing slash 2', () => {
65 const path1 = 'some-path/additional-path'
66 const path2 = 'some-path/additional-path/'
67 const result = exactPathTest(path1, path2, basepath)
68 expect(result).toBe(true)
69 })
70 it('should return false when two paths are different', () => {
71 const path1 = 'some-path/additional-path/'
72 const path2 = 'some-path2/additional-path/'
73 const result = exactPathTest(path1, path2, basepath)
74 expect(result).toBe(false)
75 })
76 it('should return true when both paths are just a slash', () => {
77 const path1 = '/'
78 const path2 = '/'
79 const result = exactPathTest(path1, path2, basepath)
80 expect(result).toBe(true)
81 })
82 },
84​
85describe('resolvePath', () => {
86 describe.each([
87 ['/', '/', '/'],
88 ['/', '/a', '/a'],
89 ['/', 'a/', '/a'],
90 ['/', '/a/b', '/a/b'],
91 ['/a', 'b', '/a/b'],
92 ['/', 'a/b', '/a/b'],
93 ['/', './a/b', '/a/b'],
94 ['/a/b/c', 'd', '/a/b/c/d'],
95 ['/a/b/c', './d', '/a/b/c/d'],
96 ['/a/b/c', './../d', '/a/b/d'],
97 ['/a/b/c/d', './../d', '/a/b/c/d'],
98 ['/a/b/c', '../../d', '/a/d'],
99 ['/a/b/c', '../d', '/a/b/d'],
100 ['/a/b/c', '..', '/a/b'],
101 ['/a/b/c', '../..', '/a'],
102 ['/a/b/c', '../../..', '/'],
103 ['/a/b/c/', '../../..', '/'],
104 ])('resolves correctly', (a, b, eq) => {
105 it(`${a} to ${b} === ${eq}`, () => {
106 expect(resolvePath({ base: a, to: b })).toEqual(eq)
107 })
108 it(`${a}/ to ${b} === ${eq} (trailing slash)`, () => {
109 expect(resolvePath({ base: a + '/', to: b })).toEqual(eq)
110 })
111 it(`${a}/ to ${b}/ === ${eq} (trailing slash + trailing slash)`, () => {
112 expect(resolvePath({ base: a + '/', to: b + '/' })).toEqual(eq)
113 })
114 })
115​
116 describe('trailingSlash', () => {
117 describe(`'always'`, () => {
118 it('keeps trailing slash', () => {
119 expect(
120 resolvePath({
121 base: '/a/b/c',
122 to: 'd/',
123 trailingSlash: 'always',
124 }),
125 ).toBe('/a/b/c/d/')
126 })
127 it('adds trailing slash', () => {
128 expect(
129 resolvePath({
130 base: '/a/b/c',
131 to: 'd',
132 trailingSlash: 'always',
133 }),
134 ).toBe('/a/b/c/d/')
135 })
136 })
137 describe(`'never'`, () => {
138 it('removes trailing slash', () => {
139 expect(
140 resolvePath({
141 base: '/a/b/c',
142 to: 'd/',
143 trailingSlash: 'never',
144 }),
145 ).toBe('/a/b/c/d')
146 })
147 it('does not add trailing slash', () => {
148 expect(
149 resolvePath({
150 base: '/a/b/c',
151 to: 'd',
152 trailingSlash: 'never',
153 }),
154 ).toBe('/a/b/c/d')
155 })
156 })
157 describe(`'preserve'`, () => {
158 it('keeps trailing slash', () => {
159 expect(
160 resolvePath({
161 base: '/a/b/c',
162 to: 'd/',
163 trailingSlash: 'preserve',
164 }),
165 ).toBe('/a/b/c/d/')
166 })
167 it('does not add trailing slash', () => {
168 expect(
169 resolvePath({
170 base: '/a/b/c',
171 to: 'd',
172 trailingSlash: 'preserve',
173 }),
174 ).toBe('/a/b/c/d')
175 })
176 })
177 })
178​
179 describe.each([{ base: '/' }, { base: '/nested' }])(
180 'param routes w/ base=$base',
181 ({ base }) => {
182 describe('wildcard (prefix + suffix)', () => {
183 it.each([
184 { name: 'regular top-level', to: '/$' },
185 { name: 'regular nested', to: '/params/wildcard/$' },
186 { name: 'with top-level prefix', to: '/prefix{$}' },
187 { name: 'with nested prefix', to: '/params/wildcard/prefix{$}' },
188 { name: 'with top-level suffix', to: '/{$}suffix' },
189 { name: 'with nested suffix', to: '/params/wildcard/{$}suffix' },
190 {
191 name: 'with top-level prefix + suffix',
192 to: '/prefix{$}suffix',
193 },
194 {
195 name: 'with nested prefix + suffix',
196 to: '/params/wildcard/prefix{$}suffix',
197 },
198 ])('$name', ({ to }) => {
199 const candidate = base + trimPathLeft(to)
200 expect(
201 resolvePath({
202 base,
203 to: candidate,
204 trailingSlash: 'never',
205 }),
206 ).toEqual(candidate)
207 })
208 })
209​
210 describe('named (prefix + suffix)', () => {
211 it.each([
212 { name: 'regular top-level', to: '/$foo' },
213 { name: 'regular nested', to: '/params/named/$foo' },
214 { name: 'with top-level prefix', to: '/prefix{$foo}' },
215 { name: 'with nested prefix', to: '/params/named/prefix{$foo}' },
216 { name: 'with top-level suffix', to: '/{$foo}suffix' },
217 { name: 'with nested suffix', to: '/params/named/{$foo}suffix' },
218 {
219 name: 'with top-level prefix + suffix',
220 to: '/prefix{$foo}suffix',
221 },
222 {
223 name: 'with nested prefix + suffix',
224 to: '/params/named/prefix{$foo}suffix',
225 },
226 ])('$name', ({ to }) => {
227 const candidate = base + trimPathLeft(to)
228 expect(
229 resolvePath({
230 base,
231 to: candidate,
232 trailingSlash: 'never',
233 }),
234 ).toEqual(candidate)
235 })
236 })
237 },
238 )
239})
240​
241describe('interpolatePath', () => {
242 describe('regular usage', () => {
243 it.each([
244 {
245 name: 'should interpolate the path',
246 path: '/users/$id',
247 params: { id: '123' },
248 result: '/users/123',
249 },
250 {
251 name: 'should interpolate the path',
252 path: '/users/$id',
253 params: { id: '123_' },
254 result: '/users/123_',
255 },
256 {
257 name: 'should interpolate the path with multiple params',
258 path: '/users/$id/$name',
259 params: { id: '123', name: 'tanner' },
260 result: '/users/123/tanner',
261 },
262 {
263 name: 'should interpolate the path with multiple params',
264 path: '/users/$id/$name',
265 params: { id: '123_', name: 'tanner' },
266 result: '/users/123_/tanner',
267 },
268 {
269 name: 'should interpolate the path with extra params',
270 path: '/users/$id',
271 params: { id: '123', name: 'tanner' },
272 result: '/users/123',
273 },
274 {
275 name: 'should interpolate the path with missing params',
276 path: '/users/$id/$name',
277 params: { id: '123' },
278 result: '/users/123/undefined',
279 },
280 {
281 name: 'should interpolate the path with missing params and extra params',
282 path: '/users/$id',
283 params: { name: 'john' },
284 result: '/users/undefined',
285 },
286 {
287 name: 'should interpolate the path with the param being a number',
288 path: '/users/$id',
289 params: { id: 123 },
290 result: '/users/123',
291 },
292 {
293 name: 'should interpolate the path with the param being a falsey number',
294 path: '/users/$id',
295 params: { id: 0 },
296 result: '/users/0',
297 },
298 {
299 name: 'should interpolate the path with URI component encoding',
300 path: '/users/$id',
301 params: { id: '?#@john+smith' },
302 result: '/users/%3F%23%40john%2Bsmith',
303 },
304 {
305 name: 'should interpolate the path without URI encoding characters in decodeCharMap',
306 path: '/users/$id',
307 params: { id: '?#@john+smith' },
308 result: '/users/%3F%23@john+smith',
309 decodeCharMap: new Map(
310 ['@', '+'].map((char) => [encodeURIComponent(char), char]),
311 ),
312 },
313 {
314 name: 'should interpolate the path with the splat param at the end',
315 path: '/users/$',
316 params: { _splat: '123' },
317 result: '/users/123',
318 },
319 {
320 name: 'should interpolate the path with a single named path param and the splat param at the end',
321 path: '/users/$username/$',
322 params: { username: 'seancassiere', _splat: '123' },
323 result: '/users/seancassiere/123',
324 },
325 {
326 name: 'should interpolate the path with 2 named path params with the splat param at the end',
327 path: '/users/$username/$id/$',
328 params: { username: 'seancassiere', id: '123', _splat: '456' },
329 result: '/users/seancassiere/123/456',
330 },
331 {
332 name: 'should interpolate the path with multiple named path params with the splat param at the end',
333 path: '/$username/settings/$repo/$id/$',
334 params: {
335 username: 'sean-cassiere',
336 repo: 'my-repo',
337 id: '123',
338 _splat: '456',
339 },
340 result: '/sean-cassiere/settings/my-repo/123/456',
341 },
342 {
343 name: 'should interpolate the path with the splat param containing slashes',
344 path: '/users/$',
345 params: { _splat: 'sean/cassiere' },
346 result: '/users/sean/cassiere',
347 },
348 ])('$name', ({ path, params, decodeCharMap, result }) => {
349 expect(
350 interpolatePath({
351 path,
352 params,
353 decodeCharMap,
354 }).interpolatedPath,
355 ).toBe(result)
356 })
357 })
358​
359 describe('wildcard (prefix + suffix)', () => {
360 it.each([
361 {
362 name: 'regular',
363 to: '/$',
364 params: { _splat: 'bar/foo/me' },
365 result: '/bar/foo/me',
366 },
367 {
368 name: 'regular curly braces',
369 to: '/{$}',
370 params: { _splat: 'bar/foo/me' },
371 result: '/bar/foo/me',
372 },
373 {
374 name: 'with prefix',
375 to: '/prefix{$}',
376 params: { _splat: 'bar' },
377 result: '/prefixbar',
378 },
379 {
380 name: 'with suffix',
381 to: '/{$}-suffix',
382 params: { _splat: 'bar' },
383 result: '/bar-suffix',
384 },
385 {
386 name: 'with prefix + suffix',
387 to: '/prefix{$}-suffix',
388 params: { _splat: 'bar' },
389 result: '/prefixbar-suffix',
390 },
391 ])('$name', ({ to, params, result }) => {
392 expect(
393 interpolatePath({
394 path: to,
395 params,
396 }).interpolatedPath,
397 ).toBe(result)
398 })
399 })
400​
401 describe('named params (prefix + suffix)', () => {
402 it.each([
403 {
404 name: 'regular',
405 to: '/$foo',
406 params: { foo: 'bar' },
407 result: '/bar',
408 },
409 {
410 name: 'regular curly braces',
411 to: '/{$foo}',
412 params: { foo: 'bar' },
413 result: '/bar',
414 },
415 {
416 name: 'with prefix',
417 to: '/prefix{$bar}',
418 params: { bar: 'baz' },
419 result: '/prefixbaz',
420 },
421 {
422 name: 'with suffix',
423 to: '/{$foo}.suffix',
424 params: { foo: 'bar' },
425 result: '/bar.suffix',
426 },
427 {
428 name: 'with suffix',
429 to: '/{$foo}.suffix',
430 params: { foo: 'bar_' },
431 result: '/bar_.suffix',
432 },
433 {
434 name: 'with prefix and suffix',
435 to: '/prefix{$param}.suffix',
436 params: { param: 'foobar' },
437 result: '/prefixfoobar.suffix',
438 },
439 ])('$name', ({ to, params, result }) => {
440 expect(
441 interpolatePath({
442 path: to,
443 params,
444 }).interpolatedPath,
445 ).toBe(result)
446 })
447 })
448​
449 describe('should handle missing _splat parameter for', () => {
450 it.each([
451 {
452 name: 'basic splat route',
453 path: '/hello/$',
454 params: {},
455 expectedResult: '/hello',
456 },
457 {
458 name: 'splat route with prefix',
459 path: '/hello/prefix{$}',
460 params: {},
461 expectedResult: '/hello/prefix',
462 },
463 {
464 name: 'splat route with suffix',
465 path: '/hello/{$}suffix',
466 params: {},
467 expectedResult: '/hello/suffix',
468 },
469 {
470 name: 'splat route with prefix and suffix',
471 path: '/hello/prefix{$}suffix',
472 params: {},
473 expectedResult: '/hello/prefixsuffix',
474 },
475 {
476 name: 'splat route with empty splat',
477 path: '/hello/$',
478 params: {
479 _splat: '',
480 },
481 expectedResult: '/hello',
482 },
483 {
484 name: 'splat route with undefined splat',
485 path: '/hello/$',
486 params: {
487 _splat: undefined,
488 },
489 expectedResult: '/hello',
490 },
491 ])('$name', ({ path, params, expectedResult }) => {
492 const result = interpolatePath({
493 path,
494 params,
495 })
496 expect(result.interpolatedPath).toBe(expectedResult)
497 expect(result.isMissingParams).toBe(true)
498 })
499 })
500})
501​
502describe('matchPathname', () => {
503 describe('path param(s) matching', () => {
504 it.each([
505 {
506 name: 'should not match since `to` does not match the input',
507 input: '/',
508 matchingOptions: {
509 to: '/users',
510 },
511 expectedMatchedParams: undefined,
512 },
513 {
514 name: 'should match since `to` matches the input',
515 input: '/users',
516 matchingOptions: {
517 to: '/users',
518 },
519 expectedMatchedParams: {},
520 },
521 {
522 name: 'should match and return the named path params',
523 input: '/users/123',
524 matchingOptions: {
525 to: '/users/$id',
526 },
527 expectedMatchedParams: { id: '123' },
528 },
529 {
530 name: 'should match and return the the splat param',
531 input: '/users/123',
532 matchingOptions: {
533 to: '/users/$',
534 },
535 expectedMatchedParams: {
536 '*': '123',
537 _splat: '123',
538 },
539 },
540 {
541 name: 'should match and return the named path and splat params',
542 input: '/users/123/456',
543 matchingOptions: {
544 to: '/users/$id/$',
545 },
546 expectedMatchedParams: {
547 id: '123',
548 '*': '456',
549 _splat: '456',
550 },
551 },
552 {
553 name: 'should match and return the multiple named path params and splat param',
554 input: '/sean-cassiere/settings/my-repo/123/456',
555 matchingOptions: {
556 to: '/$username/settings/$repo/$id/$',
557 },
558 expectedMatchedParams: {
559 username: 'sean-cassiere',
560 repo: 'my-repo',
561 id: '123',
562 '*': '456',
563 _splat: '456',
564 },
565 },
566 {
567 name: 'should match and return the splat params when multiple subsequent segments are present',
568 input: '/docs/tanner/sean/manuel',
569 matchingOptions: {
570 to: '/docs/$',
571 },
572 expectedMatchedParams: {
573 '*': 'tanner/sean/manuel',
574 _splat: 'tanner/sean/manuel',
575 },
576 },
577 ])('$name', ({ input, matchingOptions, expectedMatchedParams }) => {
578 expect(matchPathname(input, matchingOptions)).toStrictEqual(
579 expectedMatchedParams,
580 )
581 })
582 })
583​
584 describe('wildcard (prefix + suffix)', () => {
585 it.each([
586 {
587 name: 'regular',
588 input: '/docs/foo/bar',
589 matchingOptions: {
590 to: '/docs/$',
591 },
592 expectedMatchedParams: {
593 '*': 'foo/bar',
594 _splat: 'foo/bar',
595 },
596 },
597 {
598 name: 'regular curly braces',
599 input: '/docs/foo/bar',
600 matchingOptions: {
601 to: '/docs/{$}',
602 },
603 expectedMatchedParams: {
604 '*': 'foo/bar',
605 _splat: 'foo/bar',
606 },
607 },
608 {
609 name: 'with prefix',
610 input: '/docs/prefixbar/baz',
611 matchingOptions: {
612 to: '/docs/prefix{$}',
613 },
614 expectedMatchedParams: {
615 '*': 'bar/baz',
616 _splat: 'bar/baz',
617 },
618 },
619 {
620 name: 'with suffix',
621 input: '/docs/bar/baz.suffix',
622 matchingOptions: {
623 to: '/docs/{$}.suffix',
624 },
625 expectedMatchedParams: {
626 '*': 'bar/baz',
627 _splat: 'bar/baz',
628 },
629 },
630 {
631 name: 'with prefix + suffix',
632 input: '/docs/prefixbar/baz-suffix',
633 matchingOptions: {
634 to: '/docs/prefix{$}-suffix',
635 },
636 expectedMatchedParams: {
637 '*': 'bar/baz',
638 _splat: 'bar/baz',
639 },
640 },
641 ])('$name', ({ input, matchingOptions, expectedMatchedParams }) => {
642 expect(matchPathname(input, matchingOptions)).toStrictEqual(
643 expectedMatchedParams,
644 )
645 })
646 })
647​
648 describe('named params (prefix + suffix)', () => {
649 it.each([
650 {
651 name: 'regular',
652 input: '/docs/foo',
653 matchingOptions: {
654 to: '/docs/$bar',
655 },
656 expectedMatchedParams: {
657 bar: 'foo',
658 },
659 },
660 {
661 name: 'regular',
662 input: '/docs/foo_',
663 matchingOptions: {
664 to: '/docs/$bar',
665 },
666 expectedMatchedParams: {
667 bar: 'foo_',
668 },
669 },
670 {
671 name: 'regular curly braces',
672 input: '/docs/foo',
673 matchingOptions: {
674 to: '/docs/{$bar}',
675 },
676 expectedMatchedParams: {
677 bar: 'foo',
678 },
679 },
680 {
681 name: 'with prefix',
682 input: '/docs/prefixfoo_',
683 matchingOptions: {
684 to: '/docs/prefix{$bar}',
685 },
686 expectedMatchedParams: {
687 bar: 'foo_',
688 },
689 },
690 {
691 name: 'with prefix',
692 input: '/docs/prefixfoo',
693 matchingOptions: {
694 to: '/docs/prefix{$bar}',
695 },
696 expectedMatchedParams: {
697 bar: 'foo',
698 },
699 },
700 {
701 name: 'with suffix',
702 input: '/docs/foo.suffix',
703 matchingOptions: {
704 to: '/docs/{$bar}.suffix',
705 },
706 expectedMatchedParams: {
707 bar: 'foo',
708 },
709 },
710 {
711 name: 'with prefix + suffix',
712 input: '/docs/prefixfoobar-suffix',
713 matchingOptions: {
714 to: '/docs/prefix{$param}-suffix',
715 },
716 expectedMatchedParams: {
717 param: 'foobar',
718 },
719 },
720 ])('$name', ({ input, matchingOptions, expectedMatchedParams }) => {
721 expect(matchPathname(input, matchingOptions)).toStrictEqual(
722 expectedMatchedParams,
723 )
724 })
725 })
726})
727​
728describe('parsePathname', () => {
729 type ParsePathnameTestScheme = Array<{
730 name: string
731 to: string | undefined
732 expected: Array<PathSegment>
733 }>
734​
735 describe('regular usage', () => {
736 it.each([
737 {
738 name: 'should handle pathname being undefined',
739 to: undefined,
740 expected: [],
741 },
742 {
743 name: 'should handle pathname being empty',
744 to: '',
745 expected: [],
746 },
747 {
748 name: 'should handle pathname at root',
749 to: '/',
750 expected: [{ type: SEGMENT_TYPE_PATHNAME, value: '/' }],
751 },
752 {
753 name: 'should handle pathname with a single segment',
754 to: '/foo',
755 expected: [
756 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
757 { type: SEGMENT_TYPE_PATHNAME, value: 'foo' },
758 ],
759 },
760 {
761 name: 'should handle pathname with multiple segments',
762 to: '/foo/bar/baz',
763 expected: [
764 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
765 { type: SEGMENT_TYPE_PATHNAME, value: 'foo' },
766 { type: SEGMENT_TYPE_PATHNAME, value: 'bar' },
767 { type: SEGMENT_TYPE_PATHNAME, value: 'baz' },
768 ],
769 },
770 {
771 name: 'should handle pathname with a trailing slash',
772 to: '/foo/',
773 expected: [
774 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
775 { type: SEGMENT_TYPE_PATHNAME, value: 'foo' },
776 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
777 ],
778 },
779 {
780 name: 'should handle named params',
781 to: '/foo/$bar',
782 expected: [
783 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
784 { type: SEGMENT_TYPE_PATHNAME, value: 'foo' },
785 { type: SEGMENT_TYPE_PARAM, value: '$bar' },
786 ],
787 },
788 {
789 name: 'should handle named params at the root',
790 to: '/$bar',
791 expected: [
792 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
793 { type: SEGMENT_TYPE_PARAM, value: '$bar' },
794 ],
795 },
796 {
797 name: 'should handle named params followed by a segment',
798 to: '/foo/$bar/baz',
799 expected: [
800 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
801 { type: SEGMENT_TYPE_PATHNAME, value: 'foo' },
802 { type: SEGMENT_TYPE_PARAM, value: '$bar' },
803 { type: SEGMENT_TYPE_PATHNAME, value: 'baz' },
804 ],
805 },
806 {
807 name: 'should handle multiple named params',
808 to: '/foo/$bar/$baz/qux/$quux',
809 expected: [
810 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
811 { type: SEGMENT_TYPE_PATHNAME, value: 'foo' },
812 { type: SEGMENT_TYPE_PARAM, value: '$bar' },
813 { type: SEGMENT_TYPE_PARAM, value: '$baz' },
814 { type: SEGMENT_TYPE_PATHNAME, value: 'qux' },
815 { type: SEGMENT_TYPE_PARAM, value: '$quux' },
816 ],
817 },
818 {
819 name: 'should handle splat params',
820 to: '/foo/$',
821 expected: [
822 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
823 { type: SEGMENT_TYPE_PATHNAME, value: 'foo' },
824 { type: SEGMENT_TYPE_WILDCARD, value: '$' },
825 ],
826 },
827 {
828 name: 'should handle splat params at the root',
829 to: '/$',
830 expected: [
831 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
832 { type: SEGMENT_TYPE_WILDCARD, value: '$' },
833 ],
834 },
835 ] satisfies ParsePathnameTestScheme)('$name', ({ to, expected }) => {
836 const result = parsePathname(to)
837 expect(result).toEqual(expected)
838 })
839 })
840​
841 describe('wildcard (prefix + suffix)', () => {
842 it.each([
843 {
844 name: 'regular',
845 to: '/$',
846 expected: [
847 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
848 { type: SEGMENT_TYPE_WILDCARD, value: '$' },
849 ],
850 },
851 {
852 name: 'regular curly braces',
853 to: '/{$}',
854 expected: [
855 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
856 { type: SEGMENT_TYPE_WILDCARD, value: '$' },
857 ],
858 },
859 {
860 name: 'with prefix (regular text)',
861 to: '/foo{$}',
862 expected: [
863 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
864 {
865 type: SEGMENT_TYPE_WILDCARD,
866 value: '$',
867 prefixSegment: 'foo',
868 },
869 ],
870 },
871 {
872 name: 'with prefix + followed by special character',
873 to: '/foo.{$}',
874 expected: [
875 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
876 {
877 type: SEGMENT_TYPE_WILDCARD,
878 value: '$',
879 prefixSegment: 'foo.',
880 },
881 ],
882 },
883 {
884 name: 'with suffix',
885 to: '/{$}-foo',
886 expected: [
887 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
888 {
889 type: SEGMENT_TYPE_WILDCARD,
890 value: '$',
891 suffixSegment: '-foo',
892 },
893 ],
894 },
895 {
896 name: 'with prefix + suffix',
897 to: '/foo{$}-bar',
898 expected: [
899 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
900 {
901 type: SEGMENT_TYPE_WILDCARD,
902 value: '$',
903 prefixSegment: 'foo',
904 suffixSegment: '-bar',
905 },
906 ],
907 },
908 {
909 name: 'with prefix + followed by special character and a segment',
910 to: '/foo.{$}/bar',
911 expected: [
912 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
913 {
914 type: SEGMENT_TYPE_WILDCARD,
915 value: '$',
916 prefixSegment: 'foo.',
917 },
918 { type: SEGMENT_TYPE_PATHNAME, value: 'bar' },
919 ],
920 },
921 ] satisfies ParsePathnameTestScheme)('$name', ({ to, expected }) => {
922 const result = parsePathname(to)
923 expect(result).toEqual(expected)
924 })
925 })
926​
927 describe('named params (prefix + suffix)', () => {
928 it.each([
929 {
930 name: 'regular',
931 to: '/$bar',
932 expected: [
933 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
934 { type: SEGMENT_TYPE_PARAM, value: '$bar' },
935 ],
936 },
937 {
938 name: 'regular curly braces',
939 to: '/{$bar}',
940 expected: [
941 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
942 { type: SEGMENT_TYPE_PARAM, value: '$bar' },
943 ],
944 },
945 {
946 name: 'with prefix (regular text)',
947 to: '/foo{$bar}',
948 expected: [
949 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
950 {
951 type: SEGMENT_TYPE_PARAM,
952 value: '$bar',
953 prefixSegment: 'foo',
954 },
955 ],
956 },
957 {
958 name: 'with prefix + followed by special character',
959 to: '/foo.{$bar}',
960 expected: [
961 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
962 {
963 type: SEGMENT_TYPE_PARAM,
964 value: '$bar',
965 prefixSegment: 'foo.',
966 },
967 ],
968 },
969 {
970 name: 'with suffix',
971 to: '/{$bar}.foo',
972 expected: [
973 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
974 {
975 type: SEGMENT_TYPE_PARAM,
976 value: '$bar',
977 suffixSegment: '.foo',
978 },
979 ],
980 },
981 {
982 name: 'with suffix + started by special character',
983 to: '/{$bar}.foo',
984 expected: [
985 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
986 {
987 type: SEGMENT_TYPE_PARAM,
988 value: '$bar',
989 suffixSegment: '.foo',
990 },
991 ],
992 },
993 {
994 name: 'with suffix + started by special character and followed by segment',
995 to: '/{$bar}.foo/baz',
996 expected: [
997 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
998 {
999 type: SEGMENT_TYPE_PARAM,
1000 value: '$bar',
1001 suffixSegment: '.foo',
1002 },
1003 { type: SEGMENT_TYPE_PATHNAME, value: 'baz' },
1004 ],
1005 },
1007 name: 'with suffix + prefix',
1008 to: '/foo{$bar}.baz',
1009 expected: [
1010 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
1012 type: SEGMENT_TYPE_PARAM,
1013 value: '$bar',
1014 prefixSegment: 'foo',
1015 suffixSegment: '.baz',
1016 },
1017 ],
1018 },
1019 ] satisfies ParsePathnameTestScheme)('$name', ({ to, expected }) => {
1020 const result = parsePathname(to)
1021 expect(result).toEqual(expected)
1022 })
1023 })