Skip to content

Commit

Permalink
test(warn): test warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed May 1, 2020
1 parent 91f4de9 commit 3fe9812
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions __tests__/warnings.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
1 change: 1 addition & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down

0 comments on commit 3fe9812

Please sign in to comment.