UNPKG

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