2016-09-16 18:08:15 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2017-04-05 00:43:51 -04:00
|
|
|
import {Compiler, Component, NgModule, NgModuleFactoryLoader, NgModuleRef} from '@angular/core';
|
2016-09-27 20:12:25 -04:00
|
|
|
import {TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
|
2016-09-16 18:08:15 -04:00
|
|
|
|
2017-04-05 00:43:51 -04:00
|
|
|
import {Route, RouteConfigLoadEnd, RouteConfigLoadStart, Router, RouterModule} from '../index';
|
|
|
|
import {LoadedRouterConfig} from '../src/router_config_loader';
|
2016-09-16 18:08:15 -04:00
|
|
|
import {PreloadAllModules, PreloadingStrategy, RouterPreloader} from '../src/router_preloader';
|
|
|
|
import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing';
|
|
|
|
|
|
|
|
describe('RouterPreloader', () => {
|
|
|
|
@Component({template: ''})
|
|
|
|
class LazyLoadedCmp {
|
|
|
|
}
|
|
|
|
|
2017-04-05 00:43:51 -04:00
|
|
|
describe('should not load configurations with canLoad guard', () => {
|
2016-09-16 18:08:15 -04:00
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedCmp],
|
2017-04-05 00:43:51 -04:00
|
|
|
imports: [RouterModule.forChild([{path: 'LoadedModule1', component: LazyLoadedCmp}])]
|
2016-09-16 18:08:15 -04:00
|
|
|
})
|
2017-04-05 00:43:51 -04:00
|
|
|
class LoadedModule {
|
2016-09-16 18:08:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
2017-04-05 00:43:51 -04:00
|
|
|
imports: [RouterTestingModule.withRoutes(
|
|
|
|
[{path: 'lazy', loadChildren: 'expected', canLoad: ['someGuard']}])],
|
2016-09-16 18:08:15 -04:00
|
|
|
providers: [{provide: PreloadingStrategy, useExisting: PreloadAllModules}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[NgModuleFactoryLoader, RouterPreloader, Router],
|
|
|
|
(loader: SpyNgModuleFactoryLoader, preloader: RouterPreloader, router: Router) => {
|
2017-04-05 00:43:51 -04:00
|
|
|
loader.stubbedModules = {expected: LoadedModule};
|
|
|
|
|
|
|
|
preloader.preload().subscribe(() => {});
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
const c = router.config;
|
|
|
|
expect(!!((<any>c[0])._loadedConfig)).toBe(false);
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('should preload configurations', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
imports: [RouterTestingModule.withRoutes([{path: 'lazy', loadChildren: 'expected'}])],
|
|
|
|
providers: [{provide: PreloadingStrategy, useExisting: PreloadAllModules}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[NgModuleFactoryLoader, RouterPreloader, Router, NgModuleRef],
|
|
|
|
(loader: SpyNgModuleFactoryLoader, preloader: RouterPreloader, router: Router,
|
|
|
|
testModule: NgModuleRef<any>) => {
|
2017-02-15 13:57:03 -05:00
|
|
|
const events: Array<RouteConfigLoadStart|RouteConfigLoadEnd> = [];
|
2017-04-05 00:43:51 -04:00
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedCmp],
|
|
|
|
imports:
|
|
|
|
[RouterModule.forChild([{path: 'LoadedModule2', component: LazyLoadedCmp}])]
|
|
|
|
})
|
|
|
|
class LoadedModule2 {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
imports:
|
|
|
|
[RouterModule.forChild([{path: 'LoadedModule1', loadChildren: 'expected2'}])]
|
|
|
|
})
|
|
|
|
class LoadedModule1 {
|
|
|
|
}
|
2017-02-15 13:57:03 -05:00
|
|
|
|
2017-02-01 16:13:57 -05:00
|
|
|
router.events.subscribe(e => {
|
2017-02-15 13:57:03 -05:00
|
|
|
if (e instanceof RouteConfigLoadEnd || e instanceof RouteConfigLoadStart) {
|
2017-02-01 16:13:57 -05:00
|
|
|
events.push(e);
|
|
|
|
}
|
|
|
|
});
|
2017-02-15 13:57:03 -05:00
|
|
|
|
|
|
|
loader.stubbedModules = {
|
|
|
|
expected: LoadedModule1,
|
|
|
|
expected2: LoadedModule2,
|
|
|
|
};
|
2016-09-16 18:08:15 -04:00
|
|
|
|
|
|
|
preloader.preload().subscribe(() => {});
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
const c = router.config;
|
|
|
|
expect(c[0].loadChildren).toEqual('expected');
|
|
|
|
|
2017-04-05 00:43:51 -04:00
|
|
|
const loadedConfig: LoadedRouterConfig = (<any>c[0])._loadedConfig;
|
|
|
|
const module: any = loadedConfig.module;
|
|
|
|
expect(loadedConfig.routes[0].path).toEqual('LoadedModule1');
|
|
|
|
expect(module.parent).toBe(testModule);
|
2016-09-16 18:08:15 -04:00
|
|
|
|
2017-04-05 00:43:51 -04:00
|
|
|
const loadedConfig2: LoadedRouterConfig = (<any>loadedConfig.routes[0])._loadedConfig;
|
|
|
|
const module2: any = loadedConfig2.module;
|
|
|
|
expect(loadedConfig2.routes[0].path).toEqual('LoadedModule2');
|
|
|
|
expect(module2.parent).toBe(module);
|
2017-02-15 13:57:03 -05:00
|
|
|
|
|
|
|
expect(events.map(e => e.toString())).toEqual([
|
|
|
|
'RouteConfigLoadStart(path: lazy)',
|
|
|
|
'RouteConfigLoadEnd(path: lazy)',
|
|
|
|
'RouteConfigLoadStart(path: LoadedModule1)',
|
|
|
|
'RouteConfigLoadEnd(path: LoadedModule1)',
|
|
|
|
]);
|
2016-09-16 18:08:15 -04:00
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
2017-04-05 00:43:51 -04:00
|
|
|
describe('should support modules that have already been loaded', () => {
|
2016-09-16 18:08:15 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
2017-04-05 00:43:51 -04:00
|
|
|
imports: [RouterTestingModule.withRoutes([{path: 'lazy', loadChildren: 'expected'}])],
|
2016-09-16 18:08:15 -04:00
|
|
|
providers: [{provide: PreloadingStrategy, useExisting: PreloadAllModules}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work',
|
|
|
|
fakeAsync(inject(
|
2017-04-05 00:43:51 -04:00
|
|
|
[NgModuleFactoryLoader, RouterPreloader, Router, NgModuleRef, Compiler],
|
|
|
|
(loader: SpyNgModuleFactoryLoader, preloader: RouterPreloader, router: Router,
|
|
|
|
testModule: NgModuleRef<any>, compiler: Compiler) => {
|
|
|
|
@NgModule()
|
|
|
|
class LoadedModule2 {
|
|
|
|
}
|
|
|
|
|
|
|
|
const module2 = compiler.compileModuleSync(LoadedModule2).create(null);
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
imports: [RouterModule.forChild([
|
|
|
|
<Route>{
|
|
|
|
path: 'LoadedModule2',
|
|
|
|
loadChildren: 'no',
|
|
|
|
_loadedConfig: {
|
|
|
|
routes: [{path: 'LoadedModule3', loadChildren: 'expected3'}],
|
|
|
|
module: module2,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
])]
|
|
|
|
})
|
|
|
|
class LoadedModule1 {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({imports: [RouterModule.forChild([])]})
|
|
|
|
class LoadedModule3 {
|
|
|
|
}
|
|
|
|
|
|
|
|
loader.stubbedModules = {
|
|
|
|
expected: LoadedModule1,
|
|
|
|
expected3: LoadedModule3,
|
|
|
|
};
|
2016-09-16 18:08:15 -04:00
|
|
|
|
|
|
|
preloader.preload().subscribe(() => {});
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
const c = router.config;
|
2017-04-05 00:43:51 -04:00
|
|
|
|
|
|
|
const loadedConfig: LoadedRouterConfig = (<any>c[0])._loadedConfig;
|
|
|
|
const module: any = loadedConfig.module;
|
|
|
|
expect(module.parent).toBe(testModule);
|
|
|
|
|
|
|
|
const loadedConfig2: LoadedRouterConfig = (<any>loadedConfig.routes[0])._loadedConfig;
|
|
|
|
const loadedConfig3: LoadedRouterConfig = (<any>loadedConfig2.routes[0])._loadedConfig;
|
|
|
|
const module3: any = loadedConfig3.module;
|
|
|
|
expect(module3.parent).toBe(module2);
|
2016-09-16 18:08:15 -04:00
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('should ignore errors', () => {
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedCmp],
|
|
|
|
imports: [RouterModule.forChild([{path: 'LoadedModule1', component: LazyLoadedCmp}])]
|
|
|
|
})
|
|
|
|
class LoadedModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
imports: [RouterTestingModule.withRoutes([
|
|
|
|
{path: 'lazy1', loadChildren: 'expected1'}, {path: 'lazy2', loadChildren: 'expected2'}
|
|
|
|
])],
|
|
|
|
providers: [{provide: PreloadingStrategy, useExisting: PreloadAllModules}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[NgModuleFactoryLoader, RouterPreloader, Router],
|
|
|
|
(loader: SpyNgModuleFactoryLoader, preloader: RouterPreloader, router: Router) => {
|
|
|
|
loader.stubbedModules = {expected2: LoadedModule};
|
|
|
|
|
|
|
|
preloader.preload().subscribe(() => {});
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
const c = router.config;
|
|
|
|
expect(!!((<any>c[0])._loadedConfig)).toBe(false);
|
|
|
|
expect(!!((<any>c[1])._loadedConfig)).toBe(true);
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
});
|