]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: optional params
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 9 Oct 2025 09:56:30 +0000 (11:56 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 10 Oct 2025 09:12:12 +0000 (11:12 +0200)
packages/router/src/experimental/route-resolver/matchers/matcher-pattern.spec.ts
packages/router/src/experimental/router.spec.ts

index 582580c7cda9e9f1dbc49da5c946f55f9a28f599..326f573577940c9ca7cabf72ab9ba2f4664a2b8f 100644 (file)
@@ -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')
     })
   })
 })
index ca6a5ec9e63e37a641221e7057f04e38310b0aef..96b882a4a6fadc252f651c55c41b2460379e6317 100644 (file)
@@ -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(
       /^\/(.*)$/,