Skip to content

Commit

Permalink
feat(router): hasRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Apr 8, 2020
1 parent f1c2e01 commit ca02444
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions __tests__/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ describe('Router', () => {
})
})
})

describe('Dynamic Routing', () => {
it('resolves new added routes', async () => {
const { router } = await newRouter()
Expand All @@ -565,6 +566,19 @@ describe('Router', () => {
})
})

it('checks if a route exists', async () => {
const { router } = await newRouter()
router.addRoute({
name: 'new-route',
path: '/new-route',
component: components.Foo,
})
expect(router.hasRoute('new-route')).toBe(true)
expect(router.hasRoute('no')).toBe(false)
router.removeRoute('new-route')
expect(router.hasRoute('new-route')).toBe(false)
})

it('can redirect to children in the middle of navigation', async () => {
const { router } = await newRouter()
expect(router.resolve('/new-route')).toMatchObject({
Expand Down
7 changes: 6 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface Router {
addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void
addRoute(route: RouteRecordRaw): () => void
removeRoute(name: RouteRecordName): void
// TODO: hasRoute()
hasRoute(name: RouteRecordName): boolean
getRoutes(): RouteRecord[]

resolve(to: RouteLocationRaw): RouteLocation
Expand Down Expand Up @@ -163,6 +163,10 @@ export function createRouter({
return matcher.getRoutes().map(routeMatcher => routeMatcher.record)
}

function hasRoute(name: RouteRecordName): boolean {
return !!matcher.getRecordMatcher(name)
}

function resolve(
location: RouteLocationRaw,
currentLocation?: RouteLocationNormalizedLoaded
Expand Down Expand Up @@ -545,6 +549,7 @@ export function createRouter({

addRoute,
removeRoute,
hasRoute,
getRoutes,

push,
Expand Down

0 comments on commit ca02444

Please sign in to comment.