UNPKG

6.15 kBJavaScriptView Raw
1/**
2 * @license Angular v10.0.5
3 * (c) 2010-2020 Google LLC. https://angular.io/
4 * License: MIT
5 */
6
7import { Location, LocationStrategy } from '@angular/common';
8import { SpyLocation, MockLocationStrategy } from '@angular/common/testing';
9import { Injectable, Compiler, NgModule, NgModuleFactoryLoader, Injector, Optional } from '@angular/core';
10import { Router, ɵflatten, provideRoutes, ROUTER_CONFIGURATION, RouterModule, ɵROUTER_PROVIDERS, UrlSerializer, ChildrenOutletContexts, ROUTES, UrlHandlingStrategy, PreloadingStrategy, NoPreloading } from '@angular/router';
11
12/**
13 * @license
14 * Copyright Google LLC All Rights Reserved.
15 *
16 * Use of this source code is governed by an MIT-style license that can be
17 * found in the LICENSE file at https://angular.io/license
18 */
19/**
20 * @description
21 *
22 * Allows to simulate the loading of ng modules in tests.
23 *
24 * ```
25 * const loader = TestBed.inject(NgModuleFactoryLoader);
26 *
27 * @Component({template: 'lazy-loaded'})
28 * class LazyLoadedComponent {}
29 * @NgModule({
30 * declarations: [LazyLoadedComponent],
31 * imports: [RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])]
32 * })
33 *
34 * class LoadedModule {}
35 *
36 * // sets up stubbedModules
37 * loader.stubbedModules = {lazyModule: LoadedModule};
38 *
39 * router.resetConfig([
40 * {path: 'lazy', loadChildren: 'lazyModule'},
41 * ]);
42 *
43 * router.navigateByUrl('/lazy/loaded');
44 * ```
45 *
46 * @publicApi
47 */
48class SpyNgModuleFactoryLoader {
49 constructor(compiler) {
50 this.compiler = compiler;
51 /**
52 * @docsNotRequired
53 */
54 this._stubbedModules = {};
55 }
56 /**
57 * @docsNotRequired
58 */
59 set stubbedModules(modules) {
60 const res = {};
61 for (const t of Object.keys(modules)) {
62 res[t] = this.compiler.compileModuleAsync(modules[t]);
63 }
64 this._stubbedModules = res;
65 }
66 /**
67 * @docsNotRequired
68 */
69 get stubbedModules() {
70 return this._stubbedModules;
71 }
72 load(path) {
73 if (this._stubbedModules[path]) {
74 return this._stubbedModules[path];
75 }
76 else {
77 return Promise.reject(new Error(`Cannot find module ${path}`));
78 }
79 }
80}
81SpyNgModuleFactoryLoader.decorators = [
82 { type: Injectable }
83];
84SpyNgModuleFactoryLoader.ctorParameters = () => [
85 { type: Compiler }
86];
87function isUrlHandlingStrategy(opts) {
88 // This property check is needed because UrlHandlingStrategy is an interface and doesn't exist at
89 // runtime.
90 return 'shouldProcessUrl' in opts;
91}
92/**
93 * Router setup factory function used for testing.
94 *
95 * @publicApi
96 */
97function setupTestingRouter(urlSerializer, contexts, location, loader, compiler, injector, routes, opts, urlHandlingStrategy) {
98 const router = new Router(null, urlSerializer, contexts, location, injector, loader, compiler, ɵflatten(routes));
99 if (opts) {
100 // Handle deprecated argument ordering.
101 if (isUrlHandlingStrategy(opts)) {
102 router.urlHandlingStrategy = opts;
103 }
104 else {
105 // Handle ExtraOptions
106 if (opts.malformedUriErrorHandler) {
107 router.malformedUriErrorHandler = opts.malformedUriErrorHandler;
108 }
109 if (opts.paramsInheritanceStrategy) {
110 router.paramsInheritanceStrategy = opts.paramsInheritanceStrategy;
111 }
112 }
113 }
114 if (urlHandlingStrategy) {
115 router.urlHandlingStrategy = urlHandlingStrategy;
116 }
117 return router;
118}
119/**
120 * @description
121 *
122 * Sets up the router to be used for testing.
123 *
124 * The modules sets up the router to be used for testing.
125 * It provides spy implementations of `Location`, `LocationStrategy`, and {@link
126 * NgModuleFactoryLoader}.
127 *
128 * @usageNotes
129 * ### Example
130 *
131 * ```
132 * beforeEach(() => {
133 * TestBed.configureTestingModule({
134 * imports: [
135 * RouterTestingModule.withRoutes(
136 * [{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]
137 * )
138 * ]
139 * });
140 * });
141 * ```
142 *
143 * @publicApi
144 */
145class RouterTestingModule {
146 static withRoutes(routes, config) {
147 return {
148 ngModule: RouterTestingModule,
149 providers: [
150 provideRoutes(routes),
151 { provide: ROUTER_CONFIGURATION, useValue: config ? config : {} },
152 ]
153 };
154 }
155}
156RouterTestingModule.decorators = [
157 { type: NgModule, args: [{
158 exports: [RouterModule],
159 providers: [
160 ɵROUTER_PROVIDERS, { provide: Location, useClass: SpyLocation },
161 { provide: LocationStrategy, useClass: MockLocationStrategy },
162 { provide: NgModuleFactoryLoader, useClass: SpyNgModuleFactoryLoader }, {
163 provide: Router,
164 useFactory: setupTestingRouter,
165 deps: [
166 UrlSerializer, ChildrenOutletContexts, Location, NgModuleFactoryLoader, Compiler, Injector,
167 ROUTES, ROUTER_CONFIGURATION, [UrlHandlingStrategy, new Optional()]
168 ]
169 },
170 { provide: PreloadingStrategy, useExisting: NoPreloading }, provideRoutes([])
171 ]
172 },] }
173];
174
175/**
176 * @license
177 * Copyright Google LLC All Rights Reserved.
178 *
179 * Use of this source code is governed by an MIT-style license that can be
180 * found in the LICENSE file at https://angular.io/license
181 */
182
183/**
184 * @license
185 * Copyright Google LLC All Rights Reserved.
186 *
187 * Use of this source code is governed by an MIT-style license that can be
188 * found in the LICENSE file at https://angular.io/license
189 */
190// This file only reexports content of the `src` folder. Keep it that way.
191
192/**
193 * @license
194 * Copyright Google LLC All Rights Reserved.
195 *
196 * Use of this source code is governed by an MIT-style license that can be
197 * found in the LICENSE file at https://angular.io/license
198 */
199
200/**
201 * Generated bundle index. Do not edit.
202 */
203
204export { RouterTestingModule, SpyNgModuleFactoryLoader, setupTestingRouter };
205//# sourceMappingURL=testing.js.map