mirror of https://github.com/abpframework/abp.git
2 changed files with 65 additions and 82 deletions
@ -0,0 +1,60 @@ |
|||||
|
import { Component } from '@angular/core'; |
||||
|
import { RouterModule } from '@angular/router'; |
||||
|
import { createRoutingFactory, SpectatorRouting } from '@ngneat/spectator/jest'; |
||||
|
import { RouterOutletComponent } from '../components'; |
||||
|
import { getRoutePath } from '../utils/route-utils'; |
||||
|
|
||||
|
// tslint:disable-next-line
|
||||
|
@Component({ template: '' }) |
||||
|
class DummyComponent {} |
||||
|
|
||||
|
describe('Route Utils', () => { |
||||
|
describe('#getRoutePath', () => { |
||||
|
let spectator: SpectatorRouting<DummyComponent>; |
||||
|
const createRouting = createRoutingFactory({ |
||||
|
component: RouterOutletComponent, |
||||
|
stubsEnabled: false, |
||||
|
declarations: [DummyComponent], |
||||
|
imports: [RouterModule], |
||||
|
routes: [ |
||||
|
{ |
||||
|
path: '', |
||||
|
children: [ |
||||
|
{ |
||||
|
path: 'foo', |
||||
|
children: [ |
||||
|
{ |
||||
|
path: 'bar', |
||||
|
children: [ |
||||
|
{ |
||||
|
path: 'baz', |
||||
|
component: DummyComponent, |
||||
|
}, |
||||
|
], |
||||
|
}, |
||||
|
], |
||||
|
}, |
||||
|
], |
||||
|
}, |
||||
|
], |
||||
|
}); |
||||
|
|
||||
|
beforeEach(async () => { |
||||
|
spectator = createRouting(); |
||||
|
}); |
||||
|
|
||||
|
test.each` |
||||
|
url | expected |
||||
|
${''} | ${'/'} |
||||
|
${'/'} | ${'/'} |
||||
|
${'/foo'} | ${'/foo'} |
||||
|
${'/foo/bar'} | ${'/foo/bar'} |
||||
|
${'/foo/bar/baz'} | ${'/foo/bar/baz'} |
||||
|
${'/foo?bar=baz'} | ${'/foo'} |
||||
|
${'/foo#bar'} | ${'/foo'} |
||||
|
`('should return $expected when url is $url', async ({ url, expected }) => {
|
||||
|
await spectator.router.navigateByUrl(url); |
||||
|
expect(getRoutePath(spectator.router)).toBe(expected); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -1,85 +1,8 @@ |
|||||
import { ABP } from '../models/common'; |
import { PRIMARY_OUTLET, Router, UrlSegmentGroup } from '@angular/router'; |
||||
|
|
||||
export function organizeRoutes( |
export function getRoutePath(router: Router) { |
||||
routes: ABP.FullRoute[], |
const emptyGroup = { segments: [] } as UrlSegmentGroup; |
||||
wrappers: ABP.FullRoute[] = [], |
const primaryGroup = router.parseUrl(router.url).root.children[PRIMARY_OUTLET]; |
||||
parentNameArr = [] as ABP.FullRoute[], |
|
||||
parentName: string = null, |
|
||||
): ABP.FullRoute[] { |
|
||||
const filter = route => { |
|
||||
if (route.children && route.children.length) { |
|
||||
route.children = organizeRoutes(route.children, wrappers, parentNameArr, route.name); |
|
||||
} |
|
||||
|
|
||||
if (route.parentName && route.parentName !== parentName) { |
return '/' + (primaryGroup || emptyGroup).segments.map(({ path }) => path).join('/'); |
||||
parentNameArr.push(route); |
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
return true; |
|
||||
}; |
|
||||
|
|
||||
if (parentName) { |
|
||||
// recursive block
|
|
||||
return routes.filter(filter); |
|
||||
} |
|
||||
|
|
||||
const filteredRoutes = routes.filter(filter); |
|
||||
|
|
||||
if (parentNameArr.length) { |
|
||||
return sortRoutes(setChildRoute([...filteredRoutes, ...wrappers], parentNameArr)); |
|
||||
} |
|
||||
|
|
||||
return filteredRoutes; |
|
||||
} |
|
||||
|
|
||||
export function setChildRoute( |
|
||||
routes: ABP.FullRoute[], |
|
||||
parentNameArr: ABP.FullRoute[], |
|
||||
): ABP.FullRoute[] { |
|
||||
return routes.map(route => { |
|
||||
if (route.children && route.children.length) { |
|
||||
route.children = setChildRoute(route.children, parentNameArr); |
|
||||
} |
|
||||
|
|
||||
const foundedChildren = parentNameArr.filter(parent => parent.parentName === route.name); |
|
||||
if (foundedChildren && foundedChildren.length) { |
|
||||
route.children = [...(route.children || []), ...foundedChildren]; |
|
||||
} |
|
||||
|
|
||||
return route; |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
export function sortRoutes(routes: ABP.FullRoute[] = []): ABP.FullRoute[] { |
|
||||
if (!routes.length) return []; |
|
||||
return routes |
|
||||
.map((route, index) => { |
|
||||
return { |
|
||||
...route, |
|
||||
order: typeof route.order === 'undefined' ? index + 1 : route.order, |
|
||||
}; |
|
||||
}) |
|
||||
.sort((a, b) => a.order - b.order) |
|
||||
.map(route => { |
|
||||
if (route.children && route.children.length) { |
|
||||
route.children = sortRoutes(route.children); |
|
||||
} |
|
||||
|
|
||||
return route; |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
const ABP_ROUTES = [] as ABP.FullRoute[]; |
|
||||
|
|
||||
export function addAbpRoutes(routes: ABP.FullRoute | ABP.FullRoute[]): void { |
|
||||
if (!Array.isArray(routes)) { |
|
||||
routes = [routes]; |
|
||||
} |
|
||||
|
|
||||
ABP_ROUTES.push(...routes); |
|
||||
} |
|
||||
|
|
||||
export function getAbpRoutes(): ABP.FullRoute[] { |
|
||||
return ABP_ROUTES; |
|
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue