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