fix(router): module loader should start compiling modules when stubbedModules are set (#11742)

This commit is contained in:
Victor Savkin 2016-10-20 10:58:53 -07:00 committed by Alex Rickabaugh
parent 0f21a5823b
commit b44b6ef8f5
3 changed files with 65 additions and 4 deletions

View File

@ -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';

View File

@ -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(<any>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(<any>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(<any>null);
let error: any = null;
r.load('two').catch(e => error = e);
tick();
expect(error).toEqual(new Error('Cannot find module two'));
}));
});

View File

@ -49,13 +49,29 @@ export class SpyNgModuleFactoryLoader implements NgModuleFactoryLoader {
/**
* @docsNotRequired
*/
public stubbedModules: {[path: string]: any} = {};
private _stubbedModules: {[path: string]: Promise<NgModuleFactory<any>>} = {};
/**
* @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<NgModuleFactory<any>> {
if (this.stubbedModules[path]) {
return this.compiler.compileModuleAsync(this.stubbedModules[path]);
if (this._stubbedModules[path]) {
return this._stubbedModules[path];
} else {
return <any>Promise.reject(new Error(`Cannot find module ${path}`));
}