testing.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @license Angular v6.0.9
  3. * (c) 2010-2018 Google, Inc. https://angular.io/
  4. * License: MIT
  5. */
  6. import { __values } from 'tslib';
  7. import { Location, LocationStrategy } from '@angular/common';
  8. import { MockLocationStrategy, SpyLocation } from '@angular/common/testing';
  9. import { Compiler, Injectable, Injector, NgModule, NgModuleFactoryLoader, Optional } from '@angular/core';
  10. import { ChildrenOutletContexts, NoPreloading, PreloadingStrategy, ROUTER_CONFIGURATION, ROUTES, Router, RouterModule, UrlHandlingStrategy, UrlSerializer, provideRoutes, ɵROUTER_PROVIDERS, ɵflatten } 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. *
  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 res = {};
  65. try {
  66. for (var _a = __values(Object.keys(modules)), _b = _a.next(); !_b.done; _b = _a.next()) {
  67. var t = _b.value;
  68. res[t] = this.compiler.compileModuleAsync(modules[t]);
  69. }
  70. }
  71. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  72. finally {
  73. try {
  74. if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
  75. }
  76. finally { if (e_1) throw e_1.error; }
  77. }
  78. this._stubbedModules = res;
  79. var e_1, _c;
  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.decorators = [
  93. { type: Injectable }
  94. ];
  95. /** @nocollapse */
  96. SpyNgModuleFactoryLoader.ctorParameters = function () { return [
  97. { type: Compiler }
  98. ]; };
  99. return SpyNgModuleFactoryLoader;
  100. }());
  101. function isUrlHandlingStrategy(opts) {
  102. // This property check is needed because UrlHandlingStrategy is an interface and doesn't exist at
  103. // runtime.
  104. return 'shouldProcessUrl' in opts;
  105. }
  106. /**
  107. * Router setup factory function used for testing.
  108. *
  109. *
  110. */
  111. function setupTestingRouter(urlSerializer, contexts, location, loader, compiler, injector, routes, opts, urlHandlingStrategy) {
  112. var router = new Router(null, urlSerializer, contexts, location, injector, loader, compiler, ɵflatten(routes));
  113. if (opts) {
  114. // Handle deprecated argument ordering.
  115. if (isUrlHandlingStrategy(opts)) {
  116. router.urlHandlingStrategy = opts;
  117. }
  118. else {
  119. // Handle ExtraOptions
  120. if (opts.malformedUriErrorHandler) {
  121. router.malformedUriErrorHandler = opts.malformedUriErrorHandler;
  122. }
  123. if (opts.paramsInheritanceStrategy) {
  124. router.paramsInheritanceStrategy = opts.paramsInheritanceStrategy;
  125. }
  126. }
  127. }
  128. if (urlHandlingStrategy) {
  129. router.urlHandlingStrategy = urlHandlingStrategy;
  130. }
  131. return router;
  132. }
  133. /**
  134. * @description
  135. *
  136. * Sets up the router to be used for testing.
  137. *
  138. * The modules sets up the router to be used for testing.
  139. * It provides spy implementations of `Location`, `LocationStrategy`, and {@link
  140. * NgModuleFactoryLoader}.
  141. *
  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. *
  157. */
  158. var RouterTestingModule = /** @class */ (function () {
  159. function RouterTestingModule() {
  160. }
  161. RouterTestingModule.withRoutes = function (routes, config) {
  162. return {
  163. ngModule: RouterTestingModule,
  164. providers: [
  165. provideRoutes(routes),
  166. { provide: ROUTER_CONFIGURATION, useValue: config ? config : {} },
  167. ]
  168. };
  169. };
  170. RouterTestingModule.decorators = [
  171. { type: NgModule, args: [{
  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. ];
  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