]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(link): not active when matched is empty
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 8 Apr 2020 10:03:08 +0000 (12:03 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 8 Apr 2020 10:03:08 +0000 (12:03 +0200)
__tests__/RouterLink.spec.ts
src/components/Link.ts

index 0ab1087e429bae2f4a09b0a940feed28857741ee..e92b78b265c218fdea6a990b36dd58d33017a421 100644 (file)
@@ -227,6 +227,20 @@ const locations: Record<
       name: undefined,
     },
   },
+  notFound: {
+    string: '/not-found',
+    normalized: {
+      fullPath: '/not-found',
+      path: '/not-found',
+      params: {},
+      meta: {},
+      query: {},
+      hash: '',
+      matched: [],
+      redirectedFrom: undefined,
+      name: undefined,
+    },
+  },
 }
 
 describe('RouterLink', () => {
@@ -333,6 +347,15 @@ describe('RouterLink', () => {
     expect(el.querySelector('a')!.className).toContain('nav-item')
   })
 
+  it('is not active on a non matched location', () => {
+    const { el } = factory(
+      locations.notFound.normalized,
+      { to: locations.basic.string },
+      locations.basic.normalized
+    )
+    expect(el.querySelector('a')!.className).toBe('')
+  })
+
   it('is not active with more repeated params', () => {
     const { el } = factory(
       locations.repeatedParams2.normalized,
index 495f3e5a31fe7e472cacf837e610648c9cf55cd8..efe531212daf9d391827b6c23ffb9f7be16ae8f1 100644 (file)
@@ -46,6 +46,7 @@ export function useLink(props: UseLinkOptions) {
   )
   const isExactActive = computed<boolean>(
     () =>
+      activeRecordIndex.value > -1 &&
       activeRecordIndex.value === currentRoute.value.matched.length - 1 &&
       isSameLocationObject(currentRoute.value.params, route.value.params)
   )