Skip to content

Commit

Permalink
fix: app doesn't break if a component in the config does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal committed Jul 15, 2022
1 parent 7c0374e commit eb419e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/app/core/view/dynamic-routing/router.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ describe("RouterService", () => {
let service: RouterService;

let mockConfigService: jasmine.SpyObj<ConfigService>;
let mockLoggingService: jasmine.SpyObj<LoggingService>;

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 },
],
});
Expand Down Expand Up @@ -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();
});
});
27 changes: 18 additions & 9 deletions src/app/core/view/dynamic-routing/router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ export class RouterService {
* Initialize routes from the config while respecting existing routes.
*/
initRouting() {
const viewConfigs = this.configService.getAllConfigs<ViewConfig>(
PREFIX_VIEW_CONFIG
);
const viewConfigs =
this.configService.getAllConfigs<ViewConfig>(PREFIX_VIEW_CONFIG);
this.reloadRouting(viewConfigs, this.router.config);
}

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

Expand All @@ -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 = {
Expand Down

0 comments on commit eb419e6

Please sign in to comment.