From b44b6ef8f541210f686be112a0e55d58f32d3aba Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Thu, 20 Oct 2016 10:58:53 -0700 Subject: [PATCH] fix(router): module loader should start compiling modules when stubbedModules are set (#11742) --- .../@angular/router/test/integration.spec.ts | 2 +- .../test/spy_ng_module_factory_loader.spec.ts | 45 +++++++++++++++++++ .../router/testing/router_testing_module.ts | 22 +++++++-- 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 modules/@angular/router/test/spy_ng_module_factory_loader.spec.ts diff --git a/modules/@angular/router/test/integration.spec.ts b/modules/@angular/router/test/integration.spec.ts index 7518e61ea4..1bada30713 100644 --- a/modules/@angular/router/test/integration.spec.ts +++ b/modules/@angular/router/test/integration.spec.ts @@ -8,7 +8,7 @@ import {CommonModule, Location} from '@angular/common'; import {Component, NgModule, NgModuleFactoryLoader} from '@angular/core'; -import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing'; +import {ComponentFixture, TestBed, async, fakeAsync, inject, tick} from '@angular/core/testing'; import {expect} from '@angular/platform-browser/testing/matchers'; import {Observable} from 'rxjs/Observable'; import {of } from 'rxjs/observable/of'; diff --git a/modules/@angular/router/test/spy_ng_module_factory_loader.spec.ts b/modules/@angular/router/test/spy_ng_module_factory_loader.spec.ts new file mode 100644 index 0000000000..56e55b64d2 --- /dev/null +++ b/modules/@angular/router/test/spy_ng_module_factory_loader.spec.ts @@ -0,0 +1,45 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {fakeAsync, tick} from '@angular/core/testing'; +import {SpyNgModuleFactoryLoader} from '../testing/router_testing_module'; + +describe('SpyNgModuleFactoryLoader', () => { + it('should invoke the compiler when the setter is called', () => { + const expected = Promise.resolve('returned'); + const compiler: any = {compileModuleAsync: () => {}}; + spyOn(compiler, 'compileModuleAsync').and.returnValue(expected); + + const r = new SpyNgModuleFactoryLoader(compiler); + r.stubbedModules = {'one': 'someModule'}; + + expect(compiler.compileModuleAsync).toHaveBeenCalledWith('someModule'); + expect(r.stubbedModules['one']).toBe(expected); + }); + + it('should return the created promise', () => { + const expected = Promise.resolve('returned'); + const compiler: any = {compileModuleAsync: () => expected}; + + const r = new SpyNgModuleFactoryLoader(compiler); + r.stubbedModules = {'one': 'someModule'}; + + expect(r.load('one')).toBe(expected); + }); + + it('should return a rejected promise when given an invalid path', fakeAsync(() => { + const r = new SpyNgModuleFactoryLoader(null); + + let error: any = null; + r.load('two').catch(e => error = e); + + tick(); + + expect(error).toEqual(new Error('Cannot find module two')); + })); +}); \ No newline at end of file diff --git a/modules/@angular/router/testing/router_testing_module.ts b/modules/@angular/router/testing/router_testing_module.ts index c71de9bffa..fc2ae37527 100644 --- a/modules/@angular/router/testing/router_testing_module.ts +++ b/modules/@angular/router/testing/router_testing_module.ts @@ -49,13 +49,29 @@ export class SpyNgModuleFactoryLoader implements NgModuleFactoryLoader { /** * @docsNotRequired */ - public stubbedModules: {[path: string]: any} = {}; + private _stubbedModules: {[path: string]: Promise>} = {}; + + /** + * @docsNotRequired + */ + set stubbedModules(modules: {[path: string]: any}) { + const res: {[path: string]: any} = {}; + for (let t of Object.keys(modules)) { + res[t] = this.compiler.compileModuleAsync(modules[t]); + } + this._stubbedModules = res; + } + + /** + * @docsNotRequired + */ + get stubbedModules(): {[path: string]: any} { return this._stubbedModules; } constructor(private compiler: Compiler) {} load(path: string): Promise> { - if (this.stubbedModules[path]) { - return this.compiler.compileModuleAsync(this.stubbedModules[path]); + if (this._stubbedModules[path]) { + return this._stubbedModules[path]; } else { return Promise.reject(new Error(`Cannot find module ${path}`)); }