From 3d85288da569975211a01110a7792ca3bd33c40c Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 9 Oct 2025 11:56:30 +0200 Subject: [PATCH] test: optional params --- .../matchers/matcher-pattern.spec.ts | 3 ++ .../router/src/experimental/router.spec.ts | 46 +++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.spec.ts b/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.spec.ts index 582580c7..326f5735 100644 --- a/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.spec.ts +++ b/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.spec.ts @@ -505,8 +505,11 @@ describe('MatcherPatternPathDynamic', () => { expect(pattern.match('/teams/hello')).toEqual({ teamId: 'processed-hello', }) + expect(pattern.build({ teamId: '' })).toBe('/teams') expect(pattern.build({ teamId: 'was-null' })).toBe('/teams') expect(pattern.build({ teamId: 'processed-world' })).toBe('/teams/world') + // null is intentionally handled differently + expect(pattern.build({ teamId: null })).toBe('/teams/null') }) }) }) diff --git a/packages/router/src/experimental/router.spec.ts b/packages/router/src/experimental/router.spec.ts index ca6a5ec9..96b882a4 100644 --- a/packages/router/src/experimental/router.spec.ts +++ b/packages/router/src/experimental/router.spec.ts @@ -336,9 +336,30 @@ describe('Experimental Router', () => { it.skip('warns on null location during dev', async () => {}) - it.skip('removes null/undefined optional params when current location has it', async () => {}) + it('can pass an optional param', async () => { + const { router } = await newRouter() + expect( + router.resolve({ name: 'optional', params: { p: 'a' } }) + ).toHaveProperty('params', { p: 'a' }) + }) + + it('removes optional params when current location has it', async () => { + const { router } = await newRouter() - it('keeps empty strings in optional params', async () => { + await router.push({ name: 'optional', params: { p: 'a' } }) + await router.push({ name: 'optional', params: { p: null } }) + expect(router.currentRoute.value.params).toEqual({ p: null }) + + await router.push({ name: 'optional', params: { p: 'a' } }) + await router.push({ name: 'optional', params: { p: undefined } }) + expect(router.currentRoute.value.params).toEqual({ p: null }) + + await router.push({ name: 'optional', params: { p: 'a' } }) + await router.push({ name: 'optional', params: {} }) + expect(router.currentRoute.value.params).toEqual({ p: null }) + }) + + it('keeps consistent optional null param value', async () => { const { router } = await newRouter() expect( router.resolve({ name: 'optional', params: { p: '' } }) @@ -350,6 +371,25 @@ describe('Experimental Router', () => { 'params', { p: null } ) + expect(router.resolve({ name: 'optional' })).toHaveProperty('params', { + p: null, + }) + expect(router.resolve('/optional').params).toEqual({ p: null }) + }) + + it('does not fail for missing optional params', async () => { + const { router } = await newRouter() + expect( + router.resolve({ + name: 'optional', + params: {}, + }) + ).toHaveProperty('name', 'optional') + + expect(router.resolve({ name: 'optional' })).toHaveProperty( + 'name', + 'optional' + ) }) it('navigates to same route record but different query', async () => { @@ -374,8 +414,6 @@ describe('Experimental Router', () => { it.skip('fails with arrays for non repeatable params', async () => {}) - it.skip('does not fail for optional params', async () => {}) - it('can redirect to a star route when encoding the param', () => { const testCatchAllMatcher = new MatcherPatternPathDynamic( /^\/(.*)$/, -- 2.47.3