Skip to content

Commit 9e545ef

Browse files
committed
leave changes to optional param output handling for a separate PR
1 parent 8a3e366 commit 9e545ef

3 files changed

Lines changed: 167 additions & 81 deletions

File tree

packages/react-router/tests/Matches.test.tsx

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -152,160 +152,205 @@ describe('matching on different param types', () => {
152152
path: '/$id',
153153
nav: '/1',
154154
params: { id: '1' },
155+
matchParams: { id: '1' },
155156
},
156157
{
157158
name: 'param without braces',
158159
path: '/{$id}',
159160
nav: '/2',
160161
params: { id: '2' },
162+
matchParams: { id: '2' },
161163
},
162164
{
163165
name: 'param with prefix',
164166
path: '/prefix-{$id}',
165167
nav: '/prefix-3',
166168
params: { id: '3' },
169+
matchParams: { id: '3' },
167170
},
168171
{
169172
name: 'param with suffix',
170173
path: '/{$id}-suffix',
171174
nav: '/4-suffix',
172175
params: { id: '4' },
176+
matchParams: { id: '4' },
173177
},
174178
{
175179
name: 'param with prefix and suffix',
176180
path: '/prefix-{$id}-suffix',
177181
nav: '/prefix-5-suffix',
178182
params: { id: '5' },
183+
matchParams: { id: '5' },
179184
},
180185
{
181186
name: 'wildcard with no braces',
182187
path: '/abc/$',
183188
nav: '/abc/6',
184189
params: { '*': '6', _splat: '6' },
190+
matchParams: { '*': '6', _splat: '6' },
185191
},
186192
{
187193
name: 'wildcard with braces',
188194
path: '/abc/{$}',
189195
nav: '/abc/7',
190196
params: { '*': '7', _splat: '7' },
197+
matchParams: { '*': '7', _splat: '7' },
191198
},
192199
{
193200
name: 'wildcard with prefix',
194201
path: '/abc/prefix{$}',
195202
nav: '/abc/prefix/8',
196203
params: { '*': '/8', _splat: '/8' },
204+
matchParams: { '*': '/8', _splat: '/8' },
197205
},
198206
{
199207
name: 'wildcard with suffix',
200208
path: '/abc/{$}suffix',
201209
nav: '/abc/9/suffix',
202210
params: { _splat: '9/', '*': '9/' },
211+
matchParams: { _splat: '9/', '*': '9/' },
203212
},
204213
{
205214
name: 'optional param with no prefix/suffix and value',
206215
path: '/abc/{-$id}/def',
207216
nav: '/abc/10/def',
208217
params: { id: '10' },
218+
matchParams: { id: '10' },
209219
},
210220
{
211221
name: 'optional param with no prefix/suffix and requiredParam and no value',
212222
path: '/abc/{-$id}/$foo/def',
213223
nav: '/abc/bar/def',
214224
params: { foo: 'bar' },
225+
matchParams: { foo: 'bar' },
215226
},
216227
{
217228
name: 'optional param with no prefix/suffix and requiredParam and value',
218229
path: '/abc/{-$id}/$foo/def',
219230
nav: '/abc/10/bar/def',
220231
params: { id: '10', foo: 'bar' },
232+
matchParams: { id: '10', foo: 'bar' },
221233
},
222234
{
223235
name: 'optional param with no prefix/suffix and no value',
224236
path: '/abc/{-$id}/def',
225237
nav: '/abc/def',
226238
params: {},
239+
matchParams: {},
240+
},
241+
{
242+
name: 'multiple optional params with no prefix/suffix and no value',
243+
path: '/{-$a}/{-$b}/{-$c}',
244+
nav: '/',
245+
params: {},
246+
matchParams: {},
247+
},
248+
{
249+
name: 'multiple optional params with no prefix/suffix and values',
250+
path: '/{-$a}/{-$b}/{-$c}',
251+
nav: '/foo/bar/qux',
252+
params: { a: 'foo', b: 'bar', c: 'qux' },
253+
matchParams: { a: 'foo', b: 'bar', c: 'qux' },
254+
},
255+
{
256+
name: 'multiple optional params with no prefix/suffix and mixed values',
257+
path: '/{-$a}/{-$b}/{-$c}',
258+
nav: '/foo/qux',
259+
params: { a: 'foo', b: 'qux' },
260+
matchParams: { a: 'foo', b: 'qux' },
227261
},
228262
{
229263
name: 'optional param with prefix and value',
230264
path: '/optional-{-$id}',
231265
nav: '/optional-12',
232266
params: { id: '12' },
267+
matchParams: { id: '12' },
233268
},
234269
{
235270
name: 'optional param with prefix and no value',
236271
path: '/optional-{-$id}',
237272
nav: '/optional-',
238273
params: {},
274+
matchParams: { id: '' },
239275
},
240276
{
241277
name: 'optional param with suffix and value',
242278
path: '/{-$id}-optional',
243279
nav: '/13-optional',
244280
params: { id: '13' },
281+
matchParams: { id: '13' },
245282
},
246283
{
247284
name: 'optional param with suffix and no value',
248285
path: '/{-$id}-optional',
249286
nav: '/-optional',
250287
params: {},
288+
matchParams: { id: '' },
251289
},
252290
{
253291
name: 'optional param with required param, prefix, suffix, wildcard and no value',
254292
path: `/$foo/a{-$id}-optional/$`,
255293
nav: '/bar/a-optional/qux',
256-
params: { foo: 'bar', _splat: 'qux', '*': 'qux' },
294+
params: { foo: 'bar', id: '', _splat: 'qux', '*': 'qux' },
295+
matchParams: { foo: 'bar', id: '', _splat: 'qux', '*': 'qux' },
257296
},
258297
{
259298
name: 'optional param with required param, prefix, suffix, wildcard and value',
260299
path: `/$foo/a{-$id}-optional/$`,
261300
nav: '/bar/a14-optional/qux',
262301
params: { foo: 'bar', id: '14', _splat: 'qux', '*': 'qux' },
302+
matchParams: { foo: 'bar', id: '14', _splat: 'qux', '*': 'qux' },
263303
},
264304
]
265305

266306
afterEach(() => cleanup())
267-
test.each(testCases)('$name', async ({ name, path, params, nav }) => {
268-
const rootRoute = createRootRoute()
269-
270-
const Route = createRoute({
271-
getParentRoute: () => rootRoute,
272-
path,
273-
component: RouteComponent,
274-
})
275-
276-
function RouteComponent() {
277-
const routeParams = Route.useParams()
278-
const matchRoute = useMatchRoute()
279-
const matchRouteMatch = matchRoute({
280-
to: path,
307+
test.each(testCases)(
308+
'$name',
309+
async ({ name, path, params, matchParams, nav }) => {
310+
const rootRoute = createRootRoute()
311+
312+
const Route = createRoute({
313+
getParentRoute: () => rootRoute,
314+
path,
315+
component: RouteComponent,
281316
})
282317

283-
return (
284-
<div>
285-
<h1 data-testid="heading">{name}</h1>
318+
function RouteComponent() {
319+
const routeParams = Route.useParams()
320+
const matchRoute = useMatchRoute()
321+
const matchRouteMatch = matchRoute({
322+
to: path,
323+
})
324+
325+
return (
286326
<div>
287-
Params{' '}
288-
<span data-testid="params">{JSON.stringify(routeParams)}</span>
289-
Matches{' '}
290-
<span data-testid="matches">{JSON.stringify(matchRouteMatch)}</span>
327+
<h1 data-testid="heading">{name}</h1>
328+
<div>
329+
Params{' '}
330+
<span data-testid="params">{JSON.stringify(routeParams)}</span>
331+
Matches{' '}
332+
<span data-testid="matches">
333+
{JSON.stringify(matchRouteMatch)}
334+
</span>
335+
</div>
291336
</div>
292-
</div>
293-
)
294-
}
337+
)
338+
}
295339

296-
const router = createRouter({
297-
routeTree: rootRoute.addChildren([Route]),
298-
history: createMemoryHistory({ initialEntries: ['/'] }),
299-
})
340+
const router = createRouter({
341+
routeTree: rootRoute.addChildren([Route]),
342+
history: createMemoryHistory({ initialEntries: ['/'] }),
343+
})
300344

301-
await act(() => render(<RouterProvider router={router} />))
345+
await act(() => render(<RouterProvider router={router} />))
302346

303-
act(() => router.history.push(nav))
347+
act(() => router.history.push(nav))
304348

305-
const paramsToCheck = await screen.findByTestId('params')
306-
const matchesToCheck = await screen.findByTestId('matches')
349+
const paramsToCheck = await screen.findByTestId('params')
350+
const matchesToCheck = await screen.findByTestId('matches')
307351

308-
expect(JSON.parse(paramsToCheck.textContent)).toEqual(params)
309-
expect(JSON.parse(matchesToCheck.textContent)).toEqual(params)
310-
})
352+
expect(JSON.parse(paramsToCheck.textContent)).toEqual(params)
353+
expect(JSON.parse(matchesToCheck.textContent)).toEqual(matchParams)
354+
},
355+
)
311356
})

packages/router-core/src/path.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,9 +797,7 @@ function isMatch(
797797
}
798798

799799
if (matched) {
800-
if (_paramValue !== '') {
801-
params[routeSegment.value.substring(1)] = _paramValue
802-
}
800+
params[routeSegment.value.substring(1)] = _paramValue
803801
baseIndex++
804802
}
805803

0 commit comments

Comments
 (0)