Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): module loader should start compiling modules when stubbe… #11742

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/@angular/router/test/integration.spec.ts
Expand Up @@ -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';
Expand Down
45 changes: 45 additions & 0 deletions 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(<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'));
}));
});
22 changes: 19 additions & 3 deletions modules/@angular/router/testing/router_testing_module.ts
Expand Up @@ -47,13 +47,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}`));
}
Expand Down