Skip to content

Commit

Permalink
fix(errors): throws with invalid route objects
Browse files Browse the repository at this point in the history
Closes #1892
Throws for missing component, components or redirect and also when components is empty
  • Loading branch information
posva committed Nov 17, 2017
1 parent 07d0298 commit 9acef52
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/create-route-map.js
Expand Up @@ -52,6 +52,10 @@ function addRouteRecord (
const { path, name } = route
if (process.env.NODE_ENV !== 'production') {
assert(path != null, `"path" is required in a route configuration.`)
assert(route.component != null || route.redirect != null || route.components != null, `One of the following options "component", "components" or "redirect" is required for route "${path}"`)
if (route.components) {
assert(route.components.length, `"components" array cannot be empty for route "${path}"`)
}
assert(
typeof route.component !== 'string',
`route config "component" for path: ${String(path || name)} cannot be a ` +
Expand Down
42 changes: 42 additions & 0 deletions test/unit/specs/create-map.spec.js
Expand Up @@ -71,6 +71,48 @@ describe('Creating Route Map', function () {
expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Named Route \'bar\'')
})

it('in development, throws if path is missing', function () {
process.env.NODE_ENV = 'development'
expect(() => {
maps = createRouteMap([{ component: Bar }])
}).toThrowError(/"path" is required/)
})

it('in development, throws if component, components or redirect is missing', function () {
process.env.NODE_ENV = 'development'
expect(() => {
maps = createRouteMap([{ path: '/' }])
}).toThrowError(/"component", "components" or "redirect" is required/)
})

it('in development, does not throw if component is provided', function () {
process.env.NODE_ENV = 'development'
expect(() => {
maps = createRouteMap([{ path: '/', component: Bar }])
}).not.toThrow()
})

it('in development, does not throw if components is provided', function () {
process.env.NODE_ENV = 'development'
expect(() => {
maps = createRouteMap([{ path: '/', components: [{ component: Bar, path: '' }] }])
}).not.toThrow()
})

it('in development, does not throw if redirect is provided', function () {
process.env.NODE_ENV = 'development'
expect(() => {
maps = createRouteMap([{ path: '/', redirect: '/home' }])
}).not.toThrow()
})

it('in development, throws if components is empty', function () {
process.env.NODE_ENV = 'development'
expect(() => {
maps = createRouteMap([{ path: '/', components: [] }])
}).toThrowError(/"components" array cannot be empty/)
})

it('in production, it has not logged this warning', function () {
maps = createRouteMap(routes)
expect(console.warn).not.toHaveBeenCalled()
Expand Down

0 comments on commit 9acef52

Please sign in to comment.