testing.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @license Angular v7.0.3
  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,extraRequire,uselessCode} 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. * \@publicApi
  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. /** @type {?} */
  61. const res = {};
  62. for (const t of Object.keys(modules)) {
  63. res[t] = this.compiler.compileModuleAsync(modules[t]);
  64. }
  65. this._stubbedModules = res;
  66. }
  67. /**
  68. * \@docsNotRequired
  69. * @return {?}
  70. */
  71. get stubbedModules() { return this._stubbedModules; }
  72. /**
  73. * @param {?} path
  74. * @return {?}
  75. */
  76. load(path) {
  77. if (this._stubbedModules[path]) {
  78. return this._stubbedModules[path];
  79. }
  80. else {
  81. return /** @type {?} */ (Promise.reject(new Error(`Cannot find module ${path}`)));
  82. }
  83. }
  84. }
  85. SpyNgModuleFactoryLoader.decorators = [
  86. { type: Injectable }
  87. ];
  88. /** @nocollapse */
  89. SpyNgModuleFactoryLoader.ctorParameters = () => [
  90. { type: Compiler }
  91. ];
  92. /**
  93. * @param {?} opts
  94. * @return {?}
  95. */
  96. function isUrlHandlingStrategy(opts) {
  97. // This property check is needed because UrlHandlingStrategy is an interface and doesn't exist at
  98. // runtime.
  99. return 'shouldProcessUrl' in opts;
  100. }
  101. /**
  102. * Router setup factory function used for testing.
  103. *
  104. * \@publicApi
  105. * @param {?} urlSerializer
  106. * @param {?} contexts
  107. * @param {?} location
  108. * @param {?} loader
  109. * @param {?} compiler
  110. * @param {?} injector
  111. * @param {?} routes
  112. * @param {?=} opts
  113. * @param {?=} urlHandlingStrategy
  114. * @return {?}
  115. */
  116. function setupTestingRouter(urlSerializer, contexts, location, loader, compiler, injector, routes, opts, urlHandlingStrategy) {
  117. /** @type {?} */
  118. const router = new Router(/** @type {?} */ ((null)), urlSerializer, contexts, location, injector, loader, compiler, ɵflatten(routes));
  119. if (opts) {
  120. // Handle deprecated argument ordering.
  121. if (isUrlHandlingStrategy(opts)) {
  122. router.urlHandlingStrategy = opts;
  123. }
  124. else {
  125. // Handle ExtraOptions
  126. if (opts.malformedUriErrorHandler) {
  127. router.malformedUriErrorHandler = opts.malformedUriErrorHandler;
  128. }
  129. if (opts.paramsInheritanceStrategy) {
  130. router.paramsInheritanceStrategy = opts.paramsInheritanceStrategy;
  131. }
  132. }
  133. }
  134. if (urlHandlingStrategy) {
  135. router.urlHandlingStrategy = urlHandlingStrategy;
  136. }
  137. return router;
  138. }
  139. /**
  140. * \@description
  141. *
  142. * Sets up the router to be used for testing.
  143. *
  144. * The modules sets up the router to be used for testing.
  145. * It provides spy implementations of `Location`, `LocationStrategy`, and {\@link
  146. * NgModuleFactoryLoader}.
  147. *
  148. * \@usageNotes
  149. * ### Example
  150. *
  151. * ```
  152. * beforeEach(() => {
  153. * TestBed.configureTestModule({
  154. * imports: [
  155. * RouterTestingModule.withRoutes(
  156. * [{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]
  157. * )
  158. * ]
  159. * });
  160. * });
  161. * ```
  162. *
  163. * \@publicApi
  164. */
  165. class RouterTestingModule {
  166. /**
  167. * @param {?} routes
  168. * @param {?=} config
  169. * @return {?}
  170. */
  171. static withRoutes(routes, config) {
  172. return {
  173. ngModule: RouterTestingModule,
  174. providers: [
  175. provideRoutes(routes),
  176. { provide: ROUTER_CONFIGURATION, useValue: config ? config : {} },
  177. ]
  178. };
  179. }
  180. }
  181. RouterTestingModule.decorators = [
  182. { type: NgModule, args: [{
  183. exports: [RouterModule],
  184. providers: [
  185. ɵROUTER_PROVIDERS, { provide: Location, useClass: SpyLocation },
  186. { provide: LocationStrategy, useClass: MockLocationStrategy },
  187. { provide: NgModuleFactoryLoader, useClass: SpyNgModuleFactoryLoader }, {
  188. provide: Router,
  189. useFactory: setupTestingRouter,
  190. deps: [
  191. UrlSerializer, ChildrenOutletContexts, Location, NgModuleFactoryLoader, Compiler, Injector,
  192. ROUTES, ROUTER_CONFIGURATION, [UrlHandlingStrategy, new Optional()]
  193. ]
  194. },
  195. { provide: PreloadingStrategy, useExisting: NoPreloading }, provideRoutes([])
  196. ]
  197. },] }
  198. ];
  199. /**
  200. * @fileoverview added by tsickle
  201. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  202. */
  203. /**
  204. * @fileoverview added by tsickle
  205. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  206. */
  207. // This file only reexports content of the `src` folder. Keep it that way.
  208. /**
  209. * @fileoverview added by tsickle
  210. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  211. */
  212. /**
  213. * Generated bundle index. Do not edit.
  214. */
  215. export { setupTestingRouter, SpyNgModuleFactoryLoader, RouterTestingModule };
  216. //# sourceMappingURL=testing.js.map