Skip to content

Commit 3d85288

Browse files
committed
test: optional params
1 parent a69e686 commit 3d85288

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

packages/router/src/experimental/route-resolver/matchers/matcher-pattern.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,11 @@ describe('MatcherPatternPathDynamic', () => {
505505
expect(pattern.match('/teams/hello')).toEqual({
506506
teamId: 'processed-hello',
507507
})
508+
expect(pattern.build({ teamId: '' })).toBe('/teams')
508509
expect(pattern.build({ teamId: 'was-null' })).toBe('/teams')
509510
expect(pattern.build({ teamId: 'processed-world' })).toBe('/teams/world')
511+
// null is intentionally handled differently
512+
expect(pattern.build({ teamId: null })).toBe('/teams/null')
510513
})
511514
})
512515
})

packages/router/src/experimental/router.spec.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,30 @@ describe('Experimental Router', () => {
336336

337337
it.skip('warns on null location during dev', async () => {})
338338

339-
it.skip('removes null/undefined optional params when current location has it', async () => {})
339+
it('can pass an optional param', async () => {
340+
const { router } = await newRouter()
341+
expect(
342+
router.resolve({ name: 'optional', params: { p: 'a' } })
343+
).toHaveProperty('params', { p: 'a' })
344+
})
345+
346+
it('removes optional params when current location has it', async () => {
347+
const { router } = await newRouter()
340348

341-
it('keeps empty strings in optional params', async () => {
349+
await router.push({ name: 'optional', params: { p: 'a' } })
350+
await router.push({ name: 'optional', params: { p: null } })
351+
expect(router.currentRoute.value.params).toEqual({ p: null })
352+
353+
await router.push({ name: 'optional', params: { p: 'a' } })
354+
await router.push({ name: 'optional', params: { p: undefined } })
355+
expect(router.currentRoute.value.params).toEqual({ p: null })
356+
357+
await router.push({ name: 'optional', params: { p: 'a' } })
358+
await router.push({ name: 'optional', params: {} })
359+
expect(router.currentRoute.value.params).toEqual({ p: null })
360+
})
361+
362+
it('keeps consistent optional null param value', async () => {
342363
const { router } = await newRouter()
343364
expect(
344365
router.resolve({ name: 'optional', params: { p: '' } })
@@ -350,6 +371,25 @@ describe('Experimental Router', () => {
350371
'params',
351372
{ p: null }
352373
)
374+
expect(router.resolve({ name: 'optional' })).toHaveProperty('params', {
375+
p: null,
376+
})
377+
expect(router.resolve('/optional').params).toEqual({ p: null })
378+
})
379+
380+
it('does not fail for missing optional params', async () => {
381+
const { router } = await newRouter()
382+
expect(
383+
router.resolve({
384+
name: 'optional',
385+
params: {},
386+
})
387+
).toHaveProperty('name', 'optional')
388+
389+
expect(router.resolve({ name: 'optional' })).toHaveProperty(
390+
'name',
391+
'optional'
392+
)
353393
})
354394

355395
it('navigates to same route record but different query', async () => {
@@ -374,8 +414,6 @@ describe('Experimental Router', () => {
374414

375415
it.skip('fails with arrays for non repeatable params', async () => {})
376416

377-
it.skip('does not fail for optional params', async () => {})
378-
379417
it('can redirect to a star route when encoding the param', () => {
380418
const testCatchAllMatcher = new MatcherPatternPathDynamic(
381419
/^\/(.*)$/,

0 commit comments

Comments
 (0)