foo: {} as RouteRecordNormalized,
parent: {} as RouteRecordNormalized,
childEmpty: {} as RouteRecordNormalized,
+ childEmptyAlias: {} as RouteRecordNormalized,
child: {} as RouteRecordNormalized,
+ childChild: {} as RouteRecordNormalized,
parentAlias: {} as RouteRecordNormalized,
childAlias: {} as RouteRecordNormalized,
}
aliasOf: records.parent,
} as RouteRecordNormalized
records.childAlias = { aliasOf: records.child } as RouteRecordNormalized
+records.childEmptyAlias.aliasOf = records.childEmpty
type RouteLocationResolved = RouteLocationNormalized & { href: string }
name: undefined,
},
},
+ childEmptyAlias: {
+ string: '/parent/alias',
+ normalized: {
+ fullPath: '/parent/alias',
+ href: '/parent/alias',
+ path: '/parent/alias',
+ params: {},
+ meta: {},
+ query: {},
+ hash: '',
+ matched: [records.parent, records.childEmptyAlias],
+ redirectedFrom: undefined,
+ name: undefined,
+ },
+ },
child: {
string: '/parent/child',
normalized: {
name: undefined,
},
},
+ childChild: {
+ string: '/parent/child/child',
+ normalized: {
+ fullPath: '/parent/child/child',
+ href: '/parent/child/child',
+ path: '/parent/child/child',
+ params: {},
+ meta: {},
+ query: {},
+ hash: '',
+ matched: [records.parent, records.child, records.childChild],
+ redirectedFrom: undefined,
+ name: undefined,
+ },
+ },
childAsAbsolute: {
string: '/absolute-child',
normalized: {
)
})
+ it('alias of empty path child is active as if it was the parent when on adjacent child', async () => {
+ const { wrapper } = await factory(
+ locations.child.normalized,
+ { to: locations.childEmptyAlias.string },
+ locations.childEmptyAlias.normalized
+ )
+ expect(wrapper.find('a')!.className).toContain('router-link-active')
+ expect(wrapper.find('a')!.className).not.toContain(
+ 'router-link-exact-active'
+ )
+ })
+
+ it('empty path child is active as if it was the parent when on adjacent nested child', async () => {
+ const { wrapper } = await factory(
+ locations.childChild.normalized,
+ { to: locations.childEmpty.string },
+ locations.childEmpty.normalized
+ )
+ expect(wrapper.find('a')!.className).toContain('router-link-active')
+ expect(wrapper.find('a')!.className).not.toContain(
+ 'router-link-exact-active'
+ )
+ })
+
+ it('alias of empty path child is active as if it was the parent when on adjacent nested nested child', async () => {
+ const { wrapper } = await factory(
+ locations.childChild.normalized,
+ { to: locations.childEmptyAlias.string },
+ locations.childEmptyAlias.normalized
+ )
+ expect(wrapper.find('a')!.className).toContain('router-link-active')
+ expect(wrapper.find('a')!.className).not.toContain(
+ 'router-link-exact-active'
+ )
+ })
+
it('alias parent is active if the child is an absolute path', async () => {
const { wrapper } = await factory(
locations.childAsAbsolute.normalized,
name: 'WithChildren',
component,
children: [
- { path: '', name: 'default-child', component },
+ { path: '', alias: 'alias', name: 'default-child', component },
{ path: 'a', name: 'a-child', component },
- { path: 'b', name: 'b-child', component },
+ {
+ path: 'b',
+ name: 'b-child',
+ component,
+ children: [
+ { path: '', component },
+ { path: 'a2', component },
+ { path: 'b2', component },
+ ],
+ },
],
},
{ path: '/with-data', component: ComponentWithData, name: 'WithData' },
)
if (index > -1) return index
// possible parent record
- let parentRecord = matched[length - 2]
- if (
+ let parentRecordPath = getOriginalPath(
+ matched[length - 2] as RouteRecord | undefined
+ )
+ return (
+ // we are dealing with nested routes
length > 1 &&
- // if the have the same path, this link is referring to the empty child
- // are we currently are on a different child of the same parent
- routeMatched.path === parentRecord.path &&
- // avoid comparing the child with its parent
- currentMatched[currentMatched.length - 1].path !== parentRecord.path
+ // if the have the same path, this link is referring to the empty child
+ // are we currently are on a different child of the same parent
+ getOriginalPath(routeMatched) === parentRecordPath &&
+ // avoid comparing the child with its parent
+ currentMatched[currentMatched.length - 1].path !== parentRecordPath
+ ? currentMatched.findIndex(
+ isSameRouteRecord.bind(null, matched[length - 2])
+ )
+ : index
)
- return currentMatched.findIndex(
- isSameRouteRecord.bind(null, matched[length - 2])
- )
- return index
})
const isActive = computed<boolean>(
return true
}
+
+/**
+ * Get the original path value of a record by following its aliasOf
+ * @param record
+ */
+function getOriginalPath(record: RouteRecord | undefined): string {
+ return record ? (record.aliasOf ? record.aliasOf.path : record.path) : ''
+}