Skip to content

Commit

Permalink
fix: avoid warning when there is default child route (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn authored Nov 13, 2018
1 parent 80b29e8 commit a279d04
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/template/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ import * as prettier from 'prettier'
import { PageMeta } from '../resolve'

function createChildrenRoute(children: PageMeta[]): string {
return `,
children: [${children.map(createRoute).join(',')}]`
return `,children: [${children.map(createRoute).join(',')}]`
}

function createRoute(meta: PageMeta): string {
const children = !meta.children ? '' : createChildrenRoute(meta.children)

// If default child is exists, the route should not have a name.
const routeName =
meta.children && meta.children.some(m => m.path === '')
? ''
: `name: '${meta.name}',`

const routeMeta = !meta.routeMeta
? ''
: ',\nmeta: ' + JSON.stringify(meta.routeMeta, null, 2)
: ',meta: ' + JSON.stringify(meta.routeMeta, null, 2)

return `
{
name: '${meta.name}',
${routeName}
path: '${meta.path}',
component: ${meta.specifier}${routeMeta}${children}
}`
Expand Down
1 change: 0 additions & 1 deletion test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default [
component: Index
},
{
name: 'foo',
path: '/foo',
component: Foo,
children: [
Expand Down
21 changes: 21 additions & 0 deletions test/__snapshots__/route-template.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,24 @@ export default [
]
"
`;

exports[`Route template should not include name if the route has a default child 1`] = `
"const Foo = () => import(/* webpackChunkName: \\"foo\\" */ '@/pages/foo.vue')
const FooIndex = () =>
import(/* webpackChunkName: \\"foo-index\\" */ '@/pages/foo/index.vue')
export default [
{
path: '/foo',
component: Foo,
children: [
{
name: 'foo-index',
path: '',
component: FooIndex
}
]
}
]
"
`;
23 changes: 23 additions & 0 deletions test/route-template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,27 @@ describe('Route template', () => {

expect(createRoutes(meta, false)).toMatchSnapshot()
})

it('should not include name if the route has a default child', () => {
const meta: PageMeta[] = [
{
name: 'foo',
specifier: 'Foo',
path: '/foo',
pathSegments: ['foo'],
component: '@/pages/foo.vue',
children: [
{
name: 'foo-index',
specifier: 'FooIndex',
path: '',
pathSegments: ['foo'],
component: '@/pages/foo/index.vue'
}
]
}
]

expect(createRoutes(meta, true)).toMatchSnapshot()
})
})

0 comments on commit a279d04

Please sign in to comment.