Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { interpolatePath } from '../src/path'
3import {
4 parseTestPathname as parsePathname,
5 processTestRouteTree as processRouteTree,
6} from './routerTestUtils'
7import {
8 SEGMENT_TYPE_OPTIONAL_PARAM,
9 SEGMENT_TYPE_PARAM,
10 SEGMENT_TYPE_PATHNAME,
11 SEGMENT_TYPE_WILDCARD,
12 findSingleMatch,
13 parseSegments,
14} from '../src/new-process-route-tree'
15import type { SegmentKind } from '../src/new-process-route-tree'
16β
17describe('Optional Path Parameters', () => {
18 type PathSegment = {
19 type: SegmentKind
20 value: string
21 prefixSegment?: string
22 suffixSegment?: string
23 }
24 type ParsePathnameTestScheme = Array<{
25 name: string
26 to: string | undefined
27 expected: Array<PathSegment>
28 }>
29β
30 describe('parsePathname with optional params', () => {
31 it.each([
32 {
33 name: 'regular optional param',
34 to: '/{-$slug}',
35 expected: [
36 { type: SEGMENT_TYPE_PATHNAME, value: '' },
37 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'slug' },
38 ],
39 },
40 {
41 name: 'optional param with prefix',
42 to: '/prefix{-$slug}',
43 expected: [
44 { type: SEGMENT_TYPE_PATHNAME, value: '' },
45 {
46 type: SEGMENT_TYPE_OPTIONAL_PARAM,
47 value: 'slug',
48 prefixSegment: 'prefix',
49 },
50 ],
51 },
52 {
53 name: 'optional param with suffix',
54 to: '/{-$slug}suffix',
55 expected: [
56 { type: SEGMENT_TYPE_PATHNAME, value: '' },
57 {
58 type: SEGMENT_TYPE_OPTIONAL_PARAM,
59 value: 'slug',
60 suffixSegment: 'suffix',
61 },
62 ],
63 },
64 {
65 name: 'optional param with prefix and suffix',
66 to: '/prefix{-$slug}suffix',
67 expected: [
68 { type: SEGMENT_TYPE_PATHNAME, value: '' },
69 {
70 type: SEGMENT_TYPE_OPTIONAL_PARAM,
71 value: 'slug',
72 prefixSegment: 'prefix',
73 suffixSegment: 'suffix',
74 },
75 ],
76 },
77 {
78 name: 'multiple optional params',
79 to: '/posts/{-$category}/{-$slug}',
80 expected: [
81 { type: SEGMENT_TYPE_PATHNAME, value: '' },
82 { type: SEGMENT_TYPE_PATHNAME, value: 'posts' },
83 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'category' },
84 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'slug' },
85 ],
86 },
87 {
88 name: 'mixed required and optional params',
89 to: '/users/$id/{-$tab}',
90 expected: [
91 { type: SEGMENT_TYPE_PATHNAME, value: '' },
92 { type: SEGMENT_TYPE_PATHNAME, value: 'users' },
93 { type: SEGMENT_TYPE_PARAM, value: 'id' },
94 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'tab' },
95 ],
96 },
97 {
98 name: 'optional param followed by required param',
99 to: '/{-$category}/$slug',
100 expected: [
101 { type: SEGMENT_TYPE_PATHNAME, value: '' },
102 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'category' },
103 { type: SEGMENT_TYPE_PARAM, value: 'slug' },
104 ],
105 },
106 {
107 name: 'optional param with wildcard',
108 to: '/docs/{-$version}/$',
109 expected: [
110 { type: SEGMENT_TYPE_PATHNAME, value: '' },
111 { type: SEGMENT_TYPE_PATHNAME, value: 'docs' },
112 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'version' },
113 { type: SEGMENT_TYPE_WILDCARD, value: '$' },
114 ],
115 },
116 {
117 name: 'complex path with all param types',
118 to: '/api/{-$version}/users/$id/{-$tab}/$',
119 expected: [
120 { type: SEGMENT_TYPE_PATHNAME, value: '' },
121 { type: SEGMENT_TYPE_PATHNAME, value: 'api' },
122 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'version' },
123 { type: SEGMENT_TYPE_PATHNAME, value: 'users' },
124 { type: SEGMENT_TYPE_PARAM, value: 'id' },
125 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'tab' },
126 { type: SEGMENT_TYPE_WILDCARD, value: '$' },
127 ],
128 },
129 {
130 name: 'optional param at root',
131 to: '/{-$slug}',
132 expected: [
133 { type: SEGMENT_TYPE_PATHNAME, value: '' },
134 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'slug' },
135 ],
136 },
137 {
138 name: 'multiple consecutive optional params',
139 to: '/{-$year}/{-$month}/{-$day}',
140 expected: [
141 { type: SEGMENT_TYPE_PATHNAME, value: '' },
142 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'year' },
143 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'month' },
144 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: 'day' },
145 ],
146 },
147 {
148 name: 'unclosed optional param is static',
149 to: '/{-$slug',
150 expected: [
151 { type: SEGMENT_TYPE_PATHNAME, value: '' },
152 { type: SEGMENT_TYPE_PATHNAME, value: '{-$slug' },
153 ],
154 },
155 {
156 name: 'empty optional param is static',
157 to: '/{-$}',
158 expected: [
159 { type: SEGMENT_TYPE_PATHNAME, value: '' },
160 { type: SEGMENT_TYPE_PATHNAME, value: '{-$}' },
161 ],
162 },
163 {
164 name: 'unmatched closing brace is static',
165 to: '/foo}bar$',
166 expected: [
167 { type: SEGMENT_TYPE_PATHNAME, value: '' },
168 { type: SEGMENT_TYPE_PATHNAME, value: 'foo}bar$' },
169 ],
170 },
171 ] satisfies ParsePathnameTestScheme)('$name', ({ to, expected }) => {
172 const result = parsePathname(to)
173 expect(result).toEqual(expected)
174 })
175 })
176β
177 describe('interpolatePath with optional params', () => {
178 it.each([
179 {
180 name: 'optional param provided',
181 path: '/posts/{-$category}',
182 params: { category: 'tech' },
183 result: '/posts/tech',
184 },
185 {
186 name: 'optional param omitted',
187 path: '/posts/{-$category}',
188 params: {},
189 result: '/posts',
190 },
191 {
192 name: 'optional param with undefined value',
193 path: '/posts/{-$category}',
194 params: { category: undefined },
195 result: '/posts',
196 },
197 {
198 name: 'optional param with prefix - provided',
199 path: '/posts/prefix{-$category}',
200 params: { category: 'tech' },
201 result: '/posts/prefixtech',
202 },
203 {
204 name: 'optional param with prefix - omitted',
205 path: '/posts/prefix{-$category}',
206 params: {},
207 result: '/posts',
208 },
209 {
210 name: 'optional param with prefix - empty string',
211 path: '/posts/prefix{-$category}',
212 params: { category: '' },
213 result: '/posts/prefix',
214 },
215 {
216 name: 'optional param with suffix - provided',
217 path: '/posts/{-$category}.html',
218 params: { category: 'tech' },
219 result: '/posts/tech.html',
220 },
221 {
222 name: 'optional param with suffix - omitted',
223 path: '/posts/{-$category}.html',
224 params: {},
225 result: '/posts',
226 },
227 {
228 name: 'optional param with suffix - empty string',
229 path: '/posts/{-$category}.html',
230 params: { category: '' },
231 result: '/posts/.html',
232 },
233 {
234 name: 'optional param with prefix and suffix - provided',
235 path: '/posts/prefix{-$category}suffix',
236 params: { category: 'tech' },
237 result: '/posts/prefixtechsuffix',
238 },
239 {
240 name: 'optional param with prefix and suffix - omitted',
241 path: '/posts/prefix{-$category}suffix',
242 params: {},
243 result: '/posts',
244 },
245 {
246 name: 'optional param with prefix and suffix - empty string',
247 path: '/posts/prefix{-$category}suffix',
248 params: { category: '' },
249 result: '/posts/prefixsuffix',
250 },
251 {
252 name: 'multiple optional params - all provided',
253 path: '/posts/{-$category}/{-$slug}',
254 params: { category: 'tech', slug: 'hello-world' },
255 result: '/posts/tech/hello-world',
256 },
257 {
258 name: 'multiple optional params - partially provided',
259 path: '/posts/{-$category}/{-$slug}',
260 params: { category: 'tech' },
261 result: '/posts/tech',
262 },
263 {
264 name: 'multiple optional params - none provided',
265 path: '/posts/{-$category}/{-$slug}',
266 params: {},
267 result: '/posts',
268 },
269 {
270 name: 'mixed required and optional params - all provided',
271 path: '/users/$id/{-$tab}',
272 params: { id: '123', tab: 'settings' },
273 result: '/users/123/settings',
274 },
275 {
276 name: 'mixed required and optional params - optional omitted',
277 path: '/users/$id/{-$tab}',
278 params: { id: '123' },
279 result: '/users/123',
280 },
281 {
282 name: 'optional param between required params',
283 path: '/users/$id/{-$section}/edit',
284 params: { id: '123', section: 'profile' },
285 result: '/users/123/profile/edit',
286 },
287 {
288 name: 'optional param between required params - omitted',
289 path: '/users/$id/{-$section}/edit',
290 params: { id: '123' },
291 result: '/users/123/edit',
292 },
293 {
294 name: 'complex path with all param types - all provided',
295 path: '/api/{-$version}/users/$id/{-$tab}/$',
296 params: {
297 version: 'v2',
298 id: '123',
299 tab: 'settings',
300 _splat: 'extra/path',
301 },
302 result: '/api/v2/users/123/settings/extra/path',
303 },
304 {
305 name: 'complex path with all param types - optionals omitted',
306 path: '/api/{-$version}/users/$id/{-$tab}/$',
307 params: { id: '123', _splat: 'extra/path' },
308 result: '/api/users/123/extra/path',
309 },
310 {
311 name: 'multiple consecutive optional params - all provided',
312 path: '/{-$year}/{-$month}/{-$day}',
313 params: { year: '2023', month: '12', day: '25' },
314 result: '/2023/12/25',
315 },
316 {
317 name: 'multiple consecutive optional params - partially provided',
318 path: '/{-$year}/{-$month}/{-$day}',
319 params: { year: '2023', month: '12' },
320 result: '/2023/12',
321 },
322 {
323 name: 'multiple consecutive optional params - first only',
324 path: '/{-$year}/{-$month}/{-$day}',
325 params: { year: '2023' },
326 result: '/2023',
327 },
328 {
329 name: 'multiple consecutive optional params - none provided',
330 path: '/{-$year}/{-$month}/{-$day}',
331 params: {},
332 result: '/',
333 },
334 {
335 name: 'optional param with special characters',
336 path: '/posts/{-$category}',
337 params: { category: 'tech & science' },
338 result: '/posts/tech%20%26%20science',
339 },
340 {
341 name: 'optional param with number',
342 path: '/posts/{-$page}',
343 params: { page: 42 },
344 result: '/posts/42',
345 },
346 ])('$name', ({ path, params, result }) => {
347 const segments = parseSegments(false, { fullPath: path }, 0)
348 expect(interpolatePath(path, segments, params)).toBe(result)
349 })
350 })
351β
352 const { processedTree } = processRouteTree({
353 id: '__root__',
354 isRoot: true,
355 fullPath: '/',
356 path: '/',
357 })
358 const matchPathname = (
359 from: string,
360 options: { to: string; caseSensitive?: boolean; fuzzy?: boolean },
361 ) => {
362 const match = findSingleMatch(
363 options.to,
364 options.caseSensitive ?? false,
365 options.fuzzy ?? false,
366 from,
367 processedTree,
368 )
369 const result = match ? match.rawParams : undefined
370 if (options.to && !result) return
371 return result ?? {}
372 }
373β
374 describe('matchPathname with optional params', () => {
375 it.each([
376 {
377 name: 'optional param present in URL',
378 input: '/posts/tech',
379 matchingOptions: { to: '/posts/{-$category}' },
380 expectedMatchedParams: { category: 'tech' },
381 },
382 {
383 name: 'optional param absent in URL',
384 input: '/posts',
385 matchingOptions: { to: '/posts/{-$category}' },
386 expectedMatchedParams: {},
387 },
388 {
389 name: 'multiple optional params - all present',
390 input: '/posts/tech/hello-world',
391 matchingOptions: { to: '/posts/{-$category}/{-$slug}' },
392 expectedMatchedParams: { category: 'tech', slug: 'hello-world' },
393 },
394 {
395 name: 'multiple optional params - partially present',
396 input: '/posts/tech',
397 matchingOptions: { to: '/posts/{-$category}/{-$slug}' },
398 expectedMatchedParams: { category: 'tech' },
399 },
400 {
401 name: 'multiple optional params - none present',
402 input: '/posts',
403 matchingOptions: { to: '/posts/{-$category}/{-$slug}' },
404 expectedMatchedParams: {},
405 },
406 {
407 name: 'mixed required and optional params - all present',
408 input: '/users/123/settings',
409 matchingOptions: { to: '/users/$id/{-$tab}' },
410 expectedMatchedParams: { id: '123', tab: 'settings' },
411 },
412 {
413 name: 'mixed required and optional params - optional absent',
414 input: '/users/123',
415 matchingOptions: { to: '/users/$id/{-$tab}' },
416 expectedMatchedParams: { id: '123' },
417 },
418 {
419 name: 'optional param with prefix and suffix - present',
420 input: '/posts/prefixtech.html',
421 matchingOptions: { to: '/posts/prefix{-$category}.html' },
422 expectedMatchedParams: { category: 'tech' },
423 },
424 {
425 name: 'optional param with prefix and suffix - absent',
426 input: '/posts',
427 matchingOptions: { to: '/posts/prefix{-$category}.html' },
428 expectedMatchedParams: {},
429 },
430 {
431 name: 'optional param between required segments',
432 input: '/users/123/settings/edit',
433 matchingOptions: { to: '/users/$id/{-$section}/edit' },
434 expectedMatchedParams: { id: '123', section: 'settings' },
435 },
436 {
437 name: 'optional param between required segments - omitted',
438 input: '/users/123/edit',
439 matchingOptions: { to: '/users/$id/{-$section}/edit' },
440 expectedMatchedParams: { id: '123' },
441 },
442 {
443 name: 'consecutive optional params - all present',
444 input: '/2023/12/25',
445 matchingOptions: { to: '/{-$year}/{-$month}/{-$day}' },
446 expectedMatchedParams: { year: '2023', month: '12', day: '25' },
447 },
448 {
449 name: 'consecutive optional params - partially present',
450 input: '/2023/12',
451 matchingOptions: { to: '/{-$year}/{-$month}/{-$day}' },
452 expectedMatchedParams: { year: '2023', month: '12' },
453 },
454 {
455 name: 'consecutive optional params - first only',
456 input: '/2023',
457 matchingOptions: { to: '/{-$year}/{-$month}/{-$day}' },
458 expectedMatchedParams: { year: '2023' },
459 },
460 {
461 name: 'consecutive optional params - none present',
462 input: '/',
463 matchingOptions: { to: '/{-$year}/{-$month}/{-$day}' },
464 expectedMatchedParams: {},
465 },
466 ])('$name', ({ input, matchingOptions, expectedMatchedParams }) => {
467 expect(matchPathname(input, matchingOptions)).toStrictEqual(
468 toNullObj(expectedMatchedParams),
469 )
470 })
471 })
472β
473 describe('edge cases', () => {
474 it('should handle optional parameters with validation', () => {
475 // This test will be expanded when we implement params.parse for optional params
476 const path = '/posts/{-$category}'
477 const segments = parseSegments(false, { fullPath: path }, 0)
478 const params = { category: 'tech' }
479 expect(interpolatePath(path, segments, params)).toBe('/posts/tech')
480 })
481β
482 it('should handle multiple consecutive optional parameters correctly', () => {
483 const tests = [
484 { input: '/', pattern: '/{-$a}/{-$b}/{-$c}', expected: {} },
485 { input: '/1', pattern: '/{-$a}/{-$b}/{-$c}', expected: { a: '1' } },
486 {
487 input: '/1/2',
488 pattern: '/{-$a}/{-$b}/{-$c}',
489 expected: { a: '1', b: '2' },
490 },
491 {
492 input: '/1/2/3',
493 pattern: '/{-$a}/{-$b}/{-$c}',
494 expected: { a: '1', b: '2', c: '3' },
495 },
496 ]
497β
498 tests.forEach(({ input, pattern, expected }) => {
499 expect(matchPathname(input, { to: pattern })).toEqual(expected)
500 })
501 })
502β
503 it('should prioritize more specific routes over optional param routes', () => {
504 // Test that /posts/featured matches a static route, not optional param route
505 const staticMatch = matchPathname('/posts/featured', {
506 to: '/posts/featured',
507 })
508 const optionalMatch = matchPathname('/posts/featured', {
509 to: '/posts/{-$category}',
510 })
511β
512 expect(staticMatch).toEqual({})
513 expect(optionalMatch).toEqual({ category: 'featured' })
514 })
515β
516 it('should handle optional parameters with wildcards', () => {
517 const input = '/docs/v2/extra/path'
518 const pattern = '/docs/{-$version}/$'
519 const expected = {
520 version: 'v2',
521 '*': 'extra/path',
522 _splat: 'extra/path',
523 }
524β
525 expect(matchPathname(input, { to: pattern })).toEqual(expected)
526 })
527 })
528})
529β
530function toNullObj<T>(obj: T): T {
531 if (typeof obj === 'object') return Object.assign(Object.create(null), obj)
532 return obj
533}