diff --git a/src/app/core/view/dynamic-routing/router.service.spec.ts b/src/app/core/view/dynamic-routing/router.service.spec.ts index b5efbed1ce..e3a84c017c 100644 --- a/src/app/core/view/dynamic-routing/router.service.spec.ts +++ b/src/app/core/view/dynamic-routing/router.service.spec.ts @@ -21,16 +21,18 @@ describe("RouterService", () => { let service: RouterService; let mockConfigService: jasmine.SpyObj; + let mockLoggingService: jasmine.SpyObj; beforeEach(() => { mockConfigService = jasmine.createSpyObj(["getAllConfigs"]); mockConfigService.getAllConfigs.and.returnValue([]); + mockLoggingService = jasmine.createSpyObj(["warn"]); TestBed.configureTestingModule({ imports: [RouterTestingModule], providers: [ { provide: ConfigService, useValue: mockConfigService }, - { provide: LoggingService, useValue: jasmine.createSpyObj(["warn"]) }, + { provide: LoggingService, useValue: mockLoggingService }, { provide: RouteRegistry, useValue: routesRegistry }, ], }); @@ -174,4 +176,14 @@ describe("RouterService", () => { expect(wildcardRoute).toEqual({ path: "**", component: NotFoundComponent }); }); + + it("should log a warning if a view config has a component which is not registered", () => { + const testViewConfigs: ViewConfig[] = [ + { _id: "view:child", component: "Support" }, + ]; + + service.reloadRouting(testViewConfigs); + + expect(mockLoggingService.warn).toHaveBeenCalled(); + }); }); diff --git a/src/app/core/view/dynamic-routing/router.service.ts b/src/app/core/view/dynamic-routing/router.service.ts index 3961c19b24..27ee6406b7 100644 --- a/src/app/core/view/dynamic-routing/router.service.ts +++ b/src/app/core/view/dynamic-routing/router.service.ts @@ -32,9 +32,8 @@ export class RouterService { * Initialize routes from the config while respecting existing routes. */ initRouting() { - const viewConfigs = this.configService.getAllConfigs( - PREFIX_VIEW_CONFIG - ); + const viewConfigs = + this.configService.getAllConfigs(PREFIX_VIEW_CONFIG); this.reloadRouting(viewConfigs, this.router.config); } @@ -48,12 +47,12 @@ export class RouterService { const routes: Route[] = []; for (const view of viewConfigs) { - if (view.lazyLoaded) { - const path = view._id.substring(PREFIX_VIEW_CONFIG.length); - const route = additionalRoutes.find((r) => r.path === path); - routes.push(this.generateRouteFromConfig(view, route)); - } else { - routes.push(this.generateRouteFromConfig(view)); + try { + routes.push(this.createRoute(view, additionalRoutes)); + } catch (e) { + this.loggingService.warn( + `Failed to create route for view ${view._id}: ${e.message}` + ); } } @@ -73,6 +72,16 @@ export class RouterService { this.router.resetConfig(routes); } + private createRoute(view: ViewConfig, additionalRoutes: Route[]) { + if (view.lazyLoaded) { + const path = view._id.substring(PREFIX_VIEW_CONFIG.length); + const route = additionalRoutes.find((r) => r.path === path); + return this.generateRouteFromConfig(view, route); + } else { + return this.generateRouteFromConfig(view); + } + } + private generateRouteFromConfig( view: ViewConfig, route: Route = {