blob: 4e0cf15f3aa58872f31cc23f02cc4676a4da7ae7 [file] [log] [blame]
{"version":3,"file":"testing.js","sources":["../testing/src/router_testing_module.ts","../testing/testing.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {Location, LocationStrategy} from '@angular/common';\nimport {MockLocationStrategy, SpyLocation} from '@angular/common/testing';\nimport {Compiler, Injectable, Injector, ModuleWithProviders, NgModule, NgModuleFactory, NgModuleFactoryLoader, Optional} from '@angular/core';\nimport {ChildrenOutletContexts, ExtraOptions, NoPreloading, PreloadingStrategy, ROUTER_CONFIGURATION, ROUTES, Route, Router, RouterModule, Routes, UrlHandlingStrategy, UrlSerializer, provideRoutes, ɵROUTER_PROVIDERS as ROUTER_PROVIDERS, ɵflatten as flatten} from '@angular/router';\n\n\n\n/**\n * @description\n *\n * Allows to simulate the loading of ng modules in tests.\n *\n * ```\n * const loader = TestBed.get(NgModuleFactoryLoader);\n *\n * @Component({template: 'lazy-loaded'})\n * class LazyLoadedComponent {}\n * @NgModule({\n * declarations: [LazyLoadedComponent],\n * imports: [RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])]\n * })\n *\n * class LoadedModule {}\n *\n * // sets up stubbedModules\n * loader.stubbedModules = {lazyModule: LoadedModule};\n *\n * router.resetConfig([\n * {path: 'lazy', loadChildren: 'lazyModule'},\n * ]);\n *\n * router.navigateByUrl('/lazy/loaded');\n * ```\n *\n * @publicApi\n */\n@Injectable()\nexport class SpyNgModuleFactoryLoader implements NgModuleFactoryLoader {\n /**\n * @docsNotRequired\n */\n private _stubbedModules: {[path: string]: Promise<NgModuleFactory<any>>} = {};\n\n /**\n * @docsNotRequired\n */\n set stubbedModules(modules: {[path: string]: any}) {\n const res: {[path: string]: any} = {};\n for (const t of Object.keys(modules)) {\n res[t] = this.compiler.compileModuleAsync(modules[t]);\n }\n this._stubbedModules = res;\n }\n\n /**\n * @docsNotRequired\n */\n get stubbedModules(): {[path: string]: any} { return this._stubbedModules; }\n\n constructor(private compiler: Compiler) {}\n\n load(path: string): Promise<NgModuleFactory<any>> {\n if (this._stubbedModules[path]) {\n return this._stubbedModules[path];\n } else {\n return <any>Promise.reject(new Error(`Cannot find module ${path}`));\n }\n }\n}\n\nfunction isUrlHandlingStrategy(opts: ExtraOptions | UrlHandlingStrategy):\n opts is UrlHandlingStrategy {\n // This property check is needed because UrlHandlingStrategy is an interface and doesn't exist at\n // runtime.\n return 'shouldProcessUrl' in opts;\n}\n\n/**\n * Router setup factory function used for testing.\n *\n * @publicApi\n */\nexport function setupTestingRouter(\n urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location,\n loader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, routes: Route[][],\n opts?: ExtraOptions, urlHandlingStrategy?: UrlHandlingStrategy): Router;\n\n/**\n * Router setup factory function used for testing.\n *\n * @deprecated As of v5.2. The 2nd-to-last argument should be `ExtraOptions`, not\n * `UrlHandlingStrategy`\n * @publicApi\n */\nexport function setupTestingRouter(\n urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location,\n loader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, routes: Route[][],\n urlHandlingStrategy?: UrlHandlingStrategy): Router;\n\n/**\n * Router setup factory function used for testing.\n *\n * @publicApi\n */\nexport function setupTestingRouter(\n urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location,\n loader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, routes: Route[][],\n opts?: ExtraOptions | UrlHandlingStrategy, urlHandlingStrategy?: UrlHandlingStrategy) {\n const router = new Router(\n null !, urlSerializer, contexts, location, injector, loader, compiler, flatten(routes));\n if (opts) {\n // Handle deprecated argument ordering.\n if (isUrlHandlingStrategy(opts)) {\n router.urlHandlingStrategy = opts;\n } else {\n // Handle ExtraOptions\n\n if (opts.malformedUriErrorHandler) {\n router.malformedUriErrorHandler = opts.malformedUriErrorHandler;\n }\n\n if (opts.paramsInheritanceStrategy) {\n router.paramsInheritanceStrategy = opts.paramsInheritanceStrategy;\n }\n }\n }\n\n if (urlHandlingStrategy) {\n router.urlHandlingStrategy = urlHandlingStrategy;\n }\n return router;\n}\n\n/**\n * @description\n *\n * Sets up the router to be used for testing.\n *\n * The modules sets up the router to be used for testing.\n * It provides spy implementations of `Location`, `LocationStrategy`, and {@link\n * NgModuleFactoryLoader}.\n *\n * @usageNotes\n * ### Example\n *\n * ```\n * beforeEach(() => {\n * TestBed.configureTestModule({\n * imports: [\n * RouterTestingModule.withRoutes(\n * [{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]\n * )\n * ]\n * });\n * });\n * ```\n *\n * @publicApi\n */\n@NgModule({\n exports: [RouterModule],\n providers: [\n ROUTER_PROVIDERS, {provide: Location, useClass: SpyLocation},\n {provide: LocationStrategy, useClass: MockLocationStrategy},\n {provide: NgModuleFactoryLoader, useClass: SpyNgModuleFactoryLoader}, {\n provide: Router,\n useFactory: setupTestingRouter,\n deps: [\n UrlSerializer, ChildrenOutletContexts, Location, NgModuleFactoryLoader, Compiler, Injector,\n ROUTES, ROUTER_CONFIGURATION, [UrlHandlingStrategy, new Optional()]\n ]\n },\n {provide: PreloadingStrategy, useExisting: NoPreloading}, provideRoutes([])\n ]\n})\nexport class RouterTestingModule {\n static withRoutes(routes: Routes, config?: ExtraOptions):\n ModuleWithProviders<RouterTestingModule> {\n return {\n ngModule: RouterTestingModule,\n providers: [\n provideRoutes(routes),\n {provide: ROUTER_CONFIGURATION, useValue: config ? config : {}},\n ]\n };\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":["flatten","ROUTER_PROVIDERS"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,MAAa,wBAAwB;;;;IAsBnC,YAAoB,QAAkB;QAAlB,aAAQ,GAAR,QAAQ,CAAU;;;;QAlB9B,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