From 68601bff1691534f595ef7cab9b1057a3baa276d Mon Sep 17 00:00:00 2001 From: Daniel Wiehl Date: Fri, 9 Nov 2018 12:04:35 +0100 Subject: [PATCH] refactor: move 'Workbench.forRoot guard' spec into spec directory and simplify spec Closes #43 --- .../src/lib/spec/workbench.module.spec.ts | 65 +++++++++++ .../workbench/src/lib/workbench.module.ts | 2 +- .../scion/workbench/src/lib/workbench.spec.ts | 110 ------------------ 3 files changed, 66 insertions(+), 111 deletions(-) create mode 100644 projects/scion/workbench/src/lib/spec/workbench.module.spec.ts delete mode 100644 projects/scion/workbench/src/lib/workbench.spec.ts diff --git a/projects/scion/workbench/src/lib/spec/workbench.module.spec.ts b/projects/scion/workbench/src/lib/spec/workbench.module.spec.ts new file mode 100644 index 000000000..897bcfdce --- /dev/null +++ b/projects/scion/workbench/src/lib/spec/workbench.module.spec.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018 Swiss Federal Railways + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + */ + +import { fakeAsync, inject, TestBed, tick } from '@angular/core/testing'; +import { NgModule, NgModuleFactoryLoader } from '@angular/core'; +import { RouterTestingModule, SpyNgModuleFactoryLoader } from '@angular/router/testing'; +import { Router } from '@angular/router'; +import { WorkbenchModule } from '../workbench.module'; + +fdescribe('WorkbenchModule', () => { + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + WorkbenchModule.forRoot(), + RouterTestingModule.withRoutes([{path: 'lazy-module-forroot', loadChildren: './lazy-forroot.module#LazyForRootModule'}]), + RouterTestingModule.withRoutes([{path: 'lazy-module-forchild', loadChildren: './lazy-forchild.module#LazyForChildModule'}]), + ] + }); + }); + + it('throws an error when forRoot() is used in a lazy context', fakeAsync(inject([Router, NgModuleFactoryLoader], async (router: Router, loader: SpyNgModuleFactoryLoader) => { + // Use forRoot() in a lazy context + @NgModule({ + imports: [ + WorkbenchModule.forRoot() + ] + }) + class LazyForRootModule { + } + + // Use forChild() in a lazy context + @NgModule({ + imports: [ + WorkbenchModule.forChild() + ] + }) + class LazyForChildModule { + } + + loader.stubbedModules = { + './lazy-forroot.module#LazyForRootModule': LazyForRootModule, + './lazy-forchild.module#LazyForChildModule': LazyForChildModule, + }; + + // Navigate to LazyForRootModule + expect(() => { + router.navigate(['lazy-module-forroot']).then(); + tick(); + }).toThrowError(/ModuleForRootError/); + + // Navigate to LazyForChildModule + expect(() => { + router.navigate(['lazy-module-forchild']).then(); + tick(); + }).not.toThrowError(/ModuleForRootError/); + }))); +}); diff --git a/projects/scion/workbench/src/lib/workbench.module.ts b/projects/scion/workbench/src/lib/workbench.module.ts index 7581fe9fe..0ac19b2c6 100644 --- a/projects/scion/workbench/src/lib/workbench.module.ts +++ b/projects/scion/workbench/src/lib/workbench.module.ts @@ -233,7 +233,7 @@ export class WorkbenchModule { export function provideForRootGuard(workbench: WorkbenchService): any { if (workbench) { - throw new Error('WorkbenchModule.forRoot() called twice. Lazy loaded modules should use WorkbenchModule.forChild() instead.'); + throw new Error('[ModuleForRootError] WorkbenchModule.forRoot() called twice. Lazy loaded modules should use WorkbenchModule.forChild() instead.'); } return 'guarded'; } diff --git a/projects/scion/workbench/src/lib/workbench.spec.ts b/projects/scion/workbench/src/lib/workbench.spec.ts deleted file mode 100644 index 232a74d68..000000000 --- a/projects/scion/workbench/src/lib/workbench.spec.ts +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2018 Swiss Federal Railways - * - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - */ - -import { async, fakeAsync, inject, TestBed } from '@angular/core/testing'; -import { Component, NgModule, NgModuleFactoryLoader } from '@angular/core'; -import { RouterTestingModule, SpyNgModuleFactoryLoader } from '@angular/router/testing'; -import { Router, RouterModule } from '@angular/router'; -import { WorkbenchModule } from './workbench.module'; -import { CommonModule } from '@angular/common'; -import { NoopAnimationsModule } from '@angular/platform-browser/animations'; -import { advance } from './spec/util/util.spec'; -import { WorkbenchRouter } from './routing/workbench-router.service'; - -describe('Workbench', () => { - - beforeEach(async(() => { - TestBed.configureTestingModule({ - imports: [AppTestModule] - }); - - TestBed.get(Router).initialNavigation(); - })); - - it('throws an error when forRoot() is used in a lazy context', fakeAsync(inject([WorkbenchRouter, NgModuleFactoryLoader], (wbRouter: WorkbenchRouter, loader: SpyNgModuleFactoryLoader) => { - loader.stubbedModules = { - './feature-a/feature-a.module#FeatureAModule': FeatureAModule, - './feature-b/feature-b.module#FeatureBModule': FeatureBModule, - }; - - const fixture = TestBed.createComponent(AppComponent); - advance(fixture); - - let recordedError: Error = null; - - // Load lazy module with WorkbenchModule.forRoot() import - wbRouter.navigate(['wrong-import'], {blankViewPartRef: 'viewpart.1'}).catch(err => recordedError = err); - advance(fixture); - expect(recordedError.message).toEqual('WorkbenchModule.forRoot() called twice. Lazy loaded modules should use WorkbenchModule.forChild() instead.'); - - // Load lazy module with WorkbenchModule.forChild() import - recordedError = null; - wbRouter.navigate(['correct-import'], {blankViewPartRef: 'viewpart.1'}).catch(err => recordedError = err); - advance(fixture); - expect(recordedError).toBeNull(); - }))); -}); - -/**************************************************************************************************** - * Definition of App Test Module * - ****************************************************************************************************/ -@Component({template: ''}) -class AppComponent { -} - -@NgModule({ - imports: [ - WorkbenchModule.forRoot(), - NoopAnimationsModule, - RouterTestingModule.withRoutes([ - {path: 'wrong-import', loadChildren: './feature-a/feature-a.module#FeatureAModule'}, - {path: 'correct-import', loadChildren: './feature-b/feature-b.module#FeatureBModule'}, - ]), - ], - declarations: [AppComponent] -}) -class AppTestModule { -} - -/**************************************************************************************************** - * Definition of Feature Module A * - ****************************************************************************************************/ -@Component({template: 'Feature Module A'}) -class ModuleAComponent { -} - -@NgModule({ - imports: [ - CommonModule, - RouterModule.forChild([{path: '', component: ModuleAComponent}]), - WorkbenchModule.forRoot() // simulate wrong import - ], - declarations: [ModuleAComponent] -}) -export class FeatureAModule { -} - -/**************************************************************************************************** - * Definition of Feature Module B * - ****************************************************************************************************/ -@Component({template: 'Feature Module B'}) -class ModuleBComponent { -} - -@NgModule({ - imports: [ - CommonModule, - RouterModule.forChild([{path: '', component: ModuleBComponent}]), - WorkbenchModule.forChild() // simulate correct import - ], - declarations: [ModuleBComponent] -}) -export class FeatureBModule { -}