Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 SEGMENT_TYPE_OPTIONAL_PARAM,
4 SEGMENT_TYPE_PATHNAME,
5 interpolatePath,
6 matchPathname,
7 parsePathname,
8} from '../src/path'
9β
10describe('Optional Path Parameters - Clean Comprehensive Tests', () => {
11 describe('Optional Dynamic Parameters {-$param}', () => {
12 describe('parsePathname', () => {
13 it('should parse single optional dynamic param', () => {
14 const result = parsePathname('/posts/{-$category}')
15 expect(result).toEqual([
16 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
17 { type: SEGMENT_TYPE_PATHNAME, value: 'posts' },
18 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: '$category' },
19 ])
20 })
21β
22 it('should parse multiple optional dynamic params', () => {
23 const result = parsePathname('/posts/{-$category}/{-$slug}')
24 expect(result).toEqual([
25 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
26 { type: SEGMENT_TYPE_PATHNAME, value: 'posts' },
27 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: '$category' },
28 { type: SEGMENT_TYPE_OPTIONAL_PARAM, value: '$slug' },
29 ])
30 })
31β
32 it('should handle prefix/suffix with optional dynamic params', () => {
33 const result = parsePathname('/api/v{-$version}/data')
34 expect(result).toEqual([
35 { type: SEGMENT_TYPE_PATHNAME, value: '/' },
36 { type: SEGMENT_TYPE_PATHNAME, value: 'api' },
37 {
38 type: SEGMENT_TYPE_OPTIONAL_PARAM,
39 value: '$version',
40 prefixSegment: 'v',
41 suffixSegment: undefined,
42 },
43 { type: SEGMENT_TYPE_PATHNAME, value: 'data' },
44 ])
45 })
46 })
47β
48 describe('interpolatePath', () => {
49 it('should interpolate optional dynamic params when present', () => {
50 const result = interpolatePath({
51 path: '/posts/{-$category}',
52 params: { category: 'tech' },
53 })
54 expect(result.interpolatedPath).toBe('/posts/tech')
55 })
56β
57 it('should omit optional dynamic params when missing', () => {
58 const result = interpolatePath({
59 path: '/posts/{-$category}',
60 params: {},
61 })
62 expect(result.interpolatedPath).toBe('/posts')
63 })
64β
65 it('should handle multiple optional dynamic params', () => {
66 const result1 = interpolatePath({
67 path: '/posts/{-$category}/{-$slug}',
68 params: { category: 'tech', slug: 'hello' },
69 })
70 expect(result1.interpolatedPath).toBe('/posts/tech/hello')
71β
72 const result2 = interpolatePath({
73 path: '/posts/{-$category}/{-$slug}',
74 params: { category: 'tech' },
75 })
76 expect(result2.interpolatedPath).toBe('/posts/tech')
77β
78 const result3 = interpolatePath({
79 path: '/posts/{-$category}/{-$slug}',
80 params: {},
81 })
82 expect(result3.interpolatedPath).toBe('/posts')
83 })
84β
85 it('should handle mixed required and optional dynamic params', () => {
86 const result = interpolatePath({
87 path: '/posts/{-$category}/user/$id',
88 params: { category: 'tech', id: '123' },
89 })
90 expect(result.interpolatedPath).toBe('/posts/tech/user/123')
91β
92 const result2 = interpolatePath({
93 path: '/posts/{-$category}/user/$id',
94 params: { id: '123' },
95 })
96 expect(result2.interpolatedPath).toBe('/posts/user/123')
97 })
98 })
99β
100 describe('matchPathname', () => {
101 it('should match optional dynamic params when present', () => {
102 const result = matchPathname('/posts/tech', {
103 to: '/posts/{-$category}',
104 })
105 expect(result).toEqual({ category: 'tech' })
106 })
107β
108 it('should match optional dynamic params when absent', () => {
109 const result = matchPathname('/posts', {
110 to: '/posts/{-$category}',
111 })
112 expect(result).toEqual({})
113 })
114β
115 it('should handle multiple optional dynamic params', () => {
116 const result1 = matchPathname('/posts/tech/hello', {
117 to: '/posts/{-$category}/{-$slug}',
118 })
119 expect(result1).toEqual({ category: 'tech', slug: 'hello' })
120β
121 const result2 = matchPathname('/posts/tech', {
122 to: '/posts/{-$category}/{-$slug}',
123 })
124 expect(result2).toEqual({ category: 'tech' })
125β
126 const result3 = matchPathname('/posts', {
127 to: '/posts/{-$category}/{-$slug}',
128 })
129 expect(result3).toEqual({})
130 })
131β
132 it('should handle mixed required and optional dynamic params', () => {
133 const result1 = matchPathname('/posts/tech/user/123', {
134 to: '/posts/{-$category}/user/$id',
135 })
136 expect(result1).toEqual({ category: 'tech', id: '123' })
137β
138 const result2 = matchPathname('/posts/user/123', {
139 to: '/posts/{-$category}/user/$id',
140 })
141 expect(result2).toEqual({ id: '123' })
142 })
143 })
144 })
145β
146 describe('Edge Cases', () => {
147 it('should handle optional params with wildcards', () => {
148 const result = interpolatePath({
149 path: '/docs/{-$version}/$',
150 params: { version: 'v1', _splat: 'guide/intro' },
151 })
152 expect(result.interpolatedPath).toBe('/docs/v1/guide/intro')
153β
154 const result2 = interpolatePath({
155 path: '/docs/{-$version}/$',
156 params: { _splat: 'guide/intro' },
157 })
158 expect(result2.interpolatedPath).toBe('/docs/guide/intro')
159 })
160β
161 it('should work with complex patterns', () => {
162 const pattern = '/app/{-$env}/api/{-$version}/users/$id/{-$tab}'
163β
164 // All params provided
165 const result1 = interpolatePath({
166 path: pattern,
167 params: { env: 'prod', version: 'v2', id: '123', tab: 'settings' },
168 })
169 expect(result1.interpolatedPath).toBe(
170 '/app/prod/api/v2/users/123/settings',
171 )
172β
173 // Only required param
174 const result2 = interpolatePath({
175 path: pattern,
176 params: { id: '123' },
177 })
178 expect(result2.interpolatedPath).toBe('/app/api/users/123')
179β
180 // Mix of optional and required
181 const result3 = interpolatePath({
182 path: pattern,
183 params: { env: 'dev', id: '456', tab: 'profile' },
184 })
185 expect(result3.interpolatedPath).toBe('/app/dev/api/users/456/profile')
186 })
187 })
188})