UNPKG

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