]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(router): hasRoute
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 8 Apr 2020 12:14:38 +0000 (14:14 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 8 Apr 2020 12:14:38 +0000 (14:14 +0200)
__tests__/router.spec.ts
src/router.ts

index ccfaf41e05d6b1b22e6878cb0ecf4780005fe145..51057338c58b03209eadafe3c889986079c34c57 100644 (file)
@@ -548,6 +548,7 @@ describe('Router', () => {
       })
     })
   })
+
   describe('Dynamic Routing', () => {
     it('resolves new added routes', async () => {
       const { router } = await newRouter()
@@ -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({
index e1047fa36f673d26563443bade41ac2da8af191c..bd4a8202d90a1dd67fdcf86f020ea61d16fb1c47 100644 (file)
@@ -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
@@ -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
@@ -545,6 +549,7 @@ export function createRouter({
 
     addRoute,
     removeRoute,
+    hasRoute,
     getRoutes,
 
     push,