testing.js 7.0 KB

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