mirror of https://github.com/abpframework/abp.git
4 changed files with 96 additions and 0 deletions
@ -0,0 +1 @@ |
|||||
|
export * from './routes.handler'; |
||||
@ -0,0 +1,25 @@ |
|||||
|
import { Injectable, Optional } from '@angular/core'; |
||||
|
import { Router } from '@angular/router'; |
||||
|
import { RoutesService } from '../services/routes.service'; |
||||
|
|
||||
|
@Injectable({ |
||||
|
providedIn: 'root', |
||||
|
}) |
||||
|
export class RoutesHandler { |
||||
|
constructor(private routes: RoutesService, @Optional() private router: Router) { |
||||
|
this.addRoutes(); |
||||
|
} |
||||
|
|
||||
|
addRoutes() { |
||||
|
this.router?.config.forEach(({ path, data }) => { |
||||
|
if (!data) return; |
||||
|
|
||||
|
if (data.route) { |
||||
|
this.routes.add([{ path: '/' + path, ...data.route }]); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (data.routes) this.routes.add(data.routes); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
import { Router } from '@angular/router'; |
||||
|
import { RoutesHandler } from '../handlers'; |
||||
|
import { RoutesService } from '../services'; |
||||
|
|
||||
|
describe('Routes Handler', () => { |
||||
|
describe('#add', () => { |
||||
|
it('should add routes from router config', () => { |
||||
|
const config = [ |
||||
|
{ path: 'x' }, |
||||
|
{ path: '', data: { route: { name: 'Foo' } } }, |
||||
|
{ path: 'bar', data: { route: { name: 'Bar' } } }, |
||||
|
{ data: { routes: [{ path: '/baz', name: 'Baz' }] } }, |
||||
|
]; |
||||
|
const foo = [{ path: '/', name: 'Foo' }]; |
||||
|
const bar = [{ path: '/bar', name: 'Bar' }]; |
||||
|
const baz = [{ path: '/baz', name: 'Baz' }]; |
||||
|
|
||||
|
const routes = []; |
||||
|
const add = jest.fn(routes.push.bind(routes)); |
||||
|
const mockRoutesService = ({ add } as unknown) as RoutesService; |
||||
|
const mockRouter = ({ config } as unknown) as Router; |
||||
|
|
||||
|
const handler = new RoutesHandler(mockRoutesService, mockRouter); |
||||
|
|
||||
|
expect(add).toHaveBeenCalledTimes(3); |
||||
|
expect(routes).toEqual([foo, bar, baz]); |
||||
|
}); |
||||
|
}); |
||||
|
describe('#add', () => { |
||||
|
it('should add routes from router config', () => { |
||||
|
const config = [ |
||||
|
{ path: 'x' }, |
||||
|
{ path: 'y', data: {} }, |
||||
|
{ path: '', data: { route: { name: 'Foo' } } }, |
||||
|
{ path: 'bar', data: { route: { name: 'Bar' } } }, |
||||
|
{ data: { routes: [{ path: '/baz', name: 'Baz' }] } }, |
||||
|
]; |
||||
|
const foo = [{ path: '/', name: 'Foo' }]; |
||||
|
const bar = [{ path: '/bar', name: 'Bar' }]; |
||||
|
const baz = [{ path: '/baz', name: 'Baz' }]; |
||||
|
|
||||
|
const routes = []; |
||||
|
const add = jest.fn(routes.push.bind(routes)); |
||||
|
const mockRoutesService = ({ add } as unknown) as RoutesService; |
||||
|
const mockRouter = ({ config } as unknown) as Router; |
||||
|
|
||||
|
const handler = new RoutesHandler(mockRoutesService, mockRouter); |
||||
|
|
||||
|
expect(add).toHaveBeenCalledTimes(3); |
||||
|
expect(routes).toEqual([foo, bar, baz]); |
||||
|
}); |
||||
|
|
||||
|
it('should not add routes when there is no router', () => { |
||||
|
const routes = []; |
||||
|
const add = jest.fn(routes.push.bind(routes)); |
||||
|
const mockRoutesService = ({ add } as unknown) as RoutesService; |
||||
|
|
||||
|
const handler = new RoutesHandler(mockRoutesService, null); |
||||
|
|
||||
|
expect(add).not.toHaveBeenCalled(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue