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