Skip to content

Commit

Permalink
refactor: move 'Workbench.forRoot guard' spec into spec directory and…
Browse files Browse the repository at this point in the history
… simplify spec

Closes #43
  • Loading branch information
danielwiehl committed Dec 11, 2018
1 parent d5ce02e commit 68601bf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 111 deletions.
65 changes: 65 additions & 0 deletions projects/scion/workbench/src/lib/spec/workbench.module.spec.ts
Original file line number Diff line number Diff line change
@@ -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/);
})));
});
2 changes: 1 addition & 1 deletion projects/scion/workbench/src/lib/workbench.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
110 changes: 0 additions & 110 deletions projects/scion/workbench/src/lib/workbench.spec.ts

This file was deleted.

0 comments on commit 68601bf

Please sign in to comment.