]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test(warn): test warnings
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 May 2020 15:54:43 +0000 (17:54 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 May 2020 15:54:43 +0000 (17:54 +0200)
__tests__/warnings.spec.ts [new file with mode: 0644]
src/router.ts

diff --git a/__tests__/warnings.spec.ts b/__tests__/warnings.spec.ts
new file mode 100644 (file)
index 0000000..6e3c63a
--- /dev/null
@@ -0,0 +1,41 @@
+import { mockWarn } from 'jest-mock-warn'
+import { createMemoryHistory, createRouter } from '../src'
+import { defineComponent } from 'vue'
+
+let component = defineComponent({})
+
+describe('warnings', () => {
+  mockWarn()
+  it('warns on missing name and path for redirect', async () => {
+    const history = createMemoryHistory()
+    const router = createRouter({
+      history,
+      routes: [
+        { path: '/', component },
+        { path: '/redirect', redirect: { params: { foo: 'f' } } },
+      ],
+    })
+    await router.push('/redirect').catch(() => {})
+    expect('Invalid redirect found').toHaveBeenWarned()
+  })
+
+  it('warns when resolving a route with path and params', async () => {
+    const history = createMemoryHistory()
+    const router = createRouter({
+      history,
+      routes: [{ path: '/:p', name: 'p', component }],
+    })
+    router.resolve({ path: '/', params: { p: 'p' } })
+    expect('Path "/" was passed with params').toHaveBeenWarned()
+  })
+
+  it('does not warn when resolving a route with path, params and name', async () => {
+    const history = createMemoryHistory()
+    const router = createRouter({
+      history,
+      routes: [{ path: '/:p', name: 'p', component }],
+    })
+    router.resolve({ path: '/p', name: 'p', params: { p: 'p' } })
+    expect('Path "/" was passed with params').not.toHaveBeenWarned()
+  })
+})
index 76a90ec459f92914e8f241588aedda3565e9f898..acefaa3d1e95260842534144256117ed5dcdd360 100644 (file)
@@ -353,6 +353,7 @@ export function createRouter(options: RouterOptions): Router {
             targetLocation.fullPath
           }". A redirect must contain a name or path.`
         )
+        return Promise.reject(new Error('Invalid redirect'))
       }
       return pushWithRedirect(
         {