Browse Source

feat: add routes handler to catch routing config

pull/4445/head
Arman Ozak 6 years ago
parent
commit
ff3a60e68e
  1. 7
      npm/ng-packs/packages/core/src/lib/core.module.ts
  2. 1
      npm/ng-packs/packages/core/src/lib/handlers/index.ts
  3. 25
      npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts
  4. 63
      npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts

7
npm/ng-packs/packages/core/src/lib/core.module.ts

@ -21,6 +21,7 @@ import { PermissionDirective } from './directives/permission.directive';
import { ReplaceableTemplateDirective } from './directives/replaceable-template.directive';
import { StopPropagationDirective } from './directives/stop-propagation.directive';
import { VisibilityDirective } from './directives/visibility.directive';
import { RoutesHandler } from './handlers/routes.handler';
import { ApiInterceptor } from './interceptors/api.interceptor';
import { LocalizationModule } from './localization.module';
import { ABP } from './models/common';
@ -196,6 +197,12 @@ export class CoreModule {
deps: [LocalizationService],
useFactory: noop,
},
{
provide: APP_INITIALIZER,
multi: true,
deps: [RoutesHandler],
useFactory: noop,
},
{ provide: OAuthStorage, useFactory: storageFactory },
],
};

1
npm/ng-packs/packages/core/src/lib/handlers/index.ts

@ -0,0 +1 @@
export * from './routes.handler';

25
npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts

@ -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);
});
}
}

63
npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts

@ -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…
Cancel
Save