blob: 73d9ba312139621902c443ae2eaa0d118b728f1d [file] [log] [blame]
/**
* @license Angular v11.2.14
* (c) 2010-2021 Google LLC. https://angular.io/
* License: MIT
*/
import { Location, LocationStrategy } from '@angular/common';
import { SpyLocation, MockLocationStrategy } from '@angular/common/testing';
import { Injectable, Compiler, NgModule, NgModuleFactoryLoader, Injector, Optional } from '@angular/core';
import { Router, ɵflatten, ɵassignExtraOptionsToRouter, provideRoutes, ROUTER_CONFIGURATION, RouterModule, ɵROUTER_PROVIDERS, UrlSerializer, ChildrenOutletContexts, ROUTES, UrlHandlingStrategy, PreloadingStrategy, NoPreloading } from '@angular/router';
/**
* @license
* Copyright Google LLC 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
*/
/**
* @description
*
* Allows to simulate the loading of ng modules in tests.
*
* ```
* const loader = TestBed.inject(NgModuleFactoryLoader);
*
* @Component({template: 'lazy-loaded'})
* class LazyLoadedComponent {}
* @NgModule({
* declarations: [LazyLoadedComponent],
* imports: [RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])]
* })
*
* class LoadedModule {}
*
* // sets up stubbedModules
* loader.stubbedModules = {lazyModule: LoadedModule};
*
* router.resetConfig([
* {path: 'lazy', loadChildren: 'lazyModule'},
* ]);
*
* router.navigateByUrl('/lazy/loaded');
* ```
*
* @publicApi
*/
class SpyNgModuleFactoryLoader {
constructor(compiler) {
this.compiler = compiler;
/**
* @docsNotRequired
*/
this._stubbedModules = {};
}
/**
* @docsNotRequired
*/
set stubbedModules(modules) {
const res = {};
for (const t of Object.keys(modules)) {
res[t] = this.compiler.compileModuleAsync(modules[t]);
}
this._stubbedModules = res;
}
/**
* @docsNotRequired
*/
get stubbedModules() {
return this._stubbedModules;
}
load(path) {
if (this._stubbedModules[path]) {
return this._stubbedModules[path];
}
else {
return Promise.reject(new Error(`Cannot find module ${path}`));
}
}
}
SpyNgModuleFactoryLoader.decorators = [
{ type: Injectable }
];
SpyNgModuleFactoryLoader.ctorParameters = () => [
{ type: Compiler }
];
function isUrlHandlingStrategy(opts) {
// This property check is needed because UrlHandlingStrategy is an interface and doesn't exist at
// runtime.
return 'shouldProcessUrl' in opts;
}
/**
* Router setup factory function used for testing.
*
* @publicApi
*/
function setupTestingRouter(urlSerializer, contexts, location, loader, compiler, injector, routes, opts, urlHandlingStrategy) {
const router = new Router(null, urlSerializer, contexts, location, injector, loader, compiler, ɵflatten(routes));
if (opts) {
// Handle deprecated argument ordering.
if (isUrlHandlingStrategy(opts)) {
router.urlHandlingStrategy = opts;
}
else {
// Handle ExtraOptions
ɵassignExtraOptionsToRouter(opts, router);
}
}
if (urlHandlingStrategy) {
router.urlHandlingStrategy = urlHandlingStrategy;
}
return router;
}
/**
* @description
*
* Sets up the router to be used for testing.
*
* The modules sets up the router to be used for testing.
* It provides spy implementations of `Location`, `LocationStrategy`, and {@link
* NgModuleFactoryLoader}.
*
* @usageNotes
* ### Example
*
* ```
* beforeEach(() => {
* TestBed.configureTestingModule({
* imports: [
* RouterTestingModule.withRoutes(
* [{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]
* )
* ]
* });
* });
* ```
*
* @publicApi
*/
class RouterTestingModule {
static withRoutes(routes, config) {
return {
ngModule: RouterTestingModule,
providers: [
provideRoutes(routes),
{ provide: ROUTER_CONFIGURATION, useValue: config ? config : {} },
]
};
}
}
RouterTestingModule.decorators = [
{ type: NgModule, args: [{
exports: [RouterModule],
providers: [
ɵROUTER_PROVIDERS, { provide: Location, useClass: SpyLocation },
{ provide: LocationStrategy, useClass: MockLocationStrategy },
{ provide: NgModuleFactoryLoader, useClass: SpyNgModuleFactoryLoader }, {
provide: Router,
useFactory: setupTestingRouter,
deps: [
UrlSerializer, ChildrenOutletContexts, Location, NgModuleFactoryLoader, Compiler, Injector,
ROUTES, ROUTER_CONFIGURATION, [UrlHandlingStrategy, new Optional()]
]
},
{ provide: PreloadingStrategy, useExisting: NoPreloading }, provideRoutes([])
]
},] }
];
/**
* @license
* Copyright Google LLC 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
*/
/**
* @license
* Copyright Google LLC 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
*/
// This file only reexports content of the `src` folder. Keep it that way.
/**
* @license
* Copyright Google LLC 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
*/
/**
* Generated bundle index. Do not edit.
*/
export { RouterTestingModule, SpyNgModuleFactoryLoader, setupTestingRouter };
//# sourceMappingURL=testing.js.map