Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add children and parent to route record #1991

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist/*.gz
dist/*.map
explorations
docs/_book
.vscode
2 changes: 2 additions & 0 deletions flow/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@ declare type Route = {
fullPath: string;
matched: Array<RouteRecord>;
redirectedFrom?: string;
parent: ?RouteRecord;
children: Array<RouteConfig>;
meta?: any;
}
1 change: 1 addition & 0 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function addRouteRecord (
path: normalizedPath,
regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
components: route.components || { default: route.component },
children: route.children || [],
instances: {},
name,
parent,
Expand Down
2 changes: 2 additions & 0 deletions src/util/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export function createRoute (
const route: Route = {
name: location.name || (record && record.name),
meta: (record && record.meta) || {},
children: (record && record.children) || [],
parent: (record && record.parent),
path: location.path || '/',
hash: location.hash || '',
query,
Expand Down
12 changes: 12 additions & 0 deletions test/unit/specs/create-map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ describe('Creating Route Map', function () {
expect(maps.nameMap['bar.baz']).not.toBeUndefined()
})

it('has baz route in children on bar route', () => {
expect(maps.nameMap.bar.children[0].name).toEqual('bar.baz')
})

it('has bar route in bar parent', () => {
expect(maps.nameMap['bar.baz'].parent.name).toEqual('bar')
})

it('has no parent on /', () => {
expect(maps.nameMap.home.parent).toEqual(undefined)
})

it('in development, has logged a warning concerning named route of parent and default subroute', function () {
process.env.NODE_ENV = 'development'
maps = createRouteMap(routes)
Expand Down
30 changes: 29 additions & 1 deletion test/unit/specs/create-matcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { createMatcher } from '../../../src/create-matcher'

const routes = [
{ path: '/', name: 'home', component: { name: 'home' }},
{ path: '/foo', name: 'foo', component: { name: 'foo' }},
{
path: '/foo',
name: 'foo',
component: { name: 'foo' },
children: [
{ path: '', name: 'foo.baz', component: { name: 'Baz' } }
]
}
]

describe('Creating Matcher', function () {
Expand Down Expand Up @@ -32,4 +39,25 @@ describe('Creating Matcher', function () {
match({ name: 'foo' }, routes[0])
expect(console.warn).not.toHaveBeenCalled()
})

it('should return the matched route with children populated', () => {
const route = match({ name: 'foo' })
expect(route.children[0].name).toEqual('foo.baz')
})

it('should return the matched route with its parent', () => {
const route = match({ name: 'foo.baz' })
expect(route.parent.name).toEqual('foo')
})

it('should have an empty children array when no children', () => {
const route = match({ name: 'home' })
expect(route.children).toEqual([])
})

it('should return the matched route with its children', () => {
const route = match({ name: 'foo' })
expect(route.children.length).toEqual(1)
expect(route.children[0].name).toEqual('foo.baz')
})
})