UNPKG

11.4 kBJavaScriptView Raw
1/**
2 * @license Angular v15.2.3
3 * (c) 2010-2022 Google LLC. https://angular.io/
4 * License: MIT
5 */
6
7import { Location } from '@angular/common';
8import { provideLocationMocks } from '@angular/common/testing';
9import * as i0 from '@angular/core';
10import { inject, Compiler, Injector, NgModule, Injectable, Component, ViewChild } from '@angular/core';
11import { UrlSerializer, ChildrenOutletContexts, ROUTES, UrlHandlingStrategy, ROUTER_CONFIGURATION, RouteReuseStrategy, TitleStrategy, Router, RouterModule, ɵROUTER_PROVIDERS, ɵwithPreloading, NoPreloading, RouterOutlet, ɵafterNextNavigation } from '@angular/router';
12import { __awaiter } from 'tslib';
13import { TestBed } from '@angular/core/testing';
14
15function isUrlHandlingStrategy(opts) {
16 // This property check is needed because UrlHandlingStrategy is an interface and doesn't exist at
17 // runtime.
18 return 'shouldProcessUrl' in opts;
19}
20function throwInvalidConfigError(parameter) {
21 throw new Error(`Parameter ${parameter} does not match the one available in the injector. ` +
22 '`setupTestingRouter` is meant to be used as a factory function with dependencies coming from DI.');
23}
24/**
25 * Router setup factory function used for testing.
26 *
27 * @publicApi
28 * @deprecated Use `provideRouter` or `RouterTestingModule` instead.
29 */
30function setupTestingRouter(urlSerializer, contexts, location, compiler, injector, routes, opts, urlHandlingStrategy, routeReuseStrategy, titleStrategy) {
31 // Note: The checks below are to detect misconfigured providers and invalid uses of
32 // `setupTestingRouter`. This function is not used internally (neither in router code or anywhere
33 // in g3). It appears this function was exposed as publicApi by mistake and should not be used
34 // externally either. However, if it is, the documented intent is to be used as a factory function
35 // and parameter values should always match what's available in DI.
36 if (urlSerializer !== inject(UrlSerializer)) {
37 throwInvalidConfigError('urlSerializer');
38 }
39 if (contexts !== inject(ChildrenOutletContexts)) {
40 throwInvalidConfigError('contexts');
41 }
42 if (location !== inject(Location)) {
43 throwInvalidConfigError('location');
44 }
45 if (compiler !== inject(Compiler)) {
46 throwInvalidConfigError('compiler');
47 }
48 if (injector !== inject(Injector)) {
49 throwInvalidConfigError('injector');
50 }
51 if (routes !== inject(ROUTES)) {
52 throwInvalidConfigError('routes');
53 }
54 if (opts) {
55 // Handle deprecated argument ordering.
56 if (isUrlHandlingStrategy(opts)) {
57 if (opts !== inject(UrlHandlingStrategy)) {
58 throwInvalidConfigError('opts (UrlHandlingStrategy)');
59 }
60 }
61 else {
62 if (opts !== inject(ROUTER_CONFIGURATION)) {
63 throwInvalidConfigError('opts (ROUTER_CONFIGURATION)');
64 }
65 }
66 }
67 if (urlHandlingStrategy !== inject(UrlHandlingStrategy)) {
68 throwInvalidConfigError('urlHandlingStrategy');
69 }
70 if (routeReuseStrategy !== inject(RouteReuseStrategy)) {
71 throwInvalidConfigError('routeReuseStrategy');
72 }
73 if (titleStrategy !== inject(TitleStrategy)) {
74 throwInvalidConfigError('titleStrategy');
75 }
76 return new Router();
77}
78/**
79 * @description
80 *
81 * Sets up the router to be used for testing.
82 *
83 * The modules sets up the router to be used for testing.
84 * It provides spy implementations of `Location` and `LocationStrategy`.
85 *
86 * @usageNotes
87 * ### Example
88 *
89 * ```
90 * beforeEach(() => {
91 * TestBed.configureTestingModule({
92 * imports: [
93 * RouterTestingModule.withRoutes(
94 * [{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]
95 * )
96 * ]
97 * });
98 * });
99 * ```
100 *
101 * @publicApi
102 */
103class RouterTestingModule {
104 static withRoutes(routes, config) {
105 return {
106 ngModule: RouterTestingModule,
107 providers: [
108 { provide: ROUTES, multi: true, useValue: routes },
109 { provide: ROUTER_CONFIGURATION, useValue: config ? config : {} },
110 ]
111 };
112 }
113}
114RouterTestingModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
115RouterTestingModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, exports: [RouterModule] });
116RouterTestingModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, providers: [
117 ɵROUTER_PROVIDERS,
118 provideLocationMocks(),
119 ɵwithPreloading(NoPreloading).ɵproviders,
120 { provide: ROUTES, multi: true, useValue: [] },
121 ], imports: [RouterModule] });
122i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, decorators: [{
123 type: NgModule,
124 args: [{
125 exports: [RouterModule],
126 providers: [
127 ɵROUTER_PROVIDERS,
128 provideLocationMocks(),
129 ɵwithPreloading(NoPreloading).ɵproviders,
130 { provide: ROUTES, multi: true, useValue: [] },
131 ]
132 }]
133 }] });
134
135class RootFixtureService {
136 createHarness() {
137 if (this.harness) {
138 throw new Error('Only one harness should be created per test.');
139 }
140 this.harness = new RouterTestingHarness(this.getRootFixture());
141 return this.harness;
142 }
143 getRootFixture() {
144 if (this.fixture !== undefined) {
145 return this.fixture;
146 }
147 this.fixture = TestBed.createComponent(RootCmp);
148 this.fixture.detectChanges();
149 return this.fixture;
150 }
151}
152RootFixtureService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
153RootFixtureService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, providedIn: 'root' });
154i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, decorators: [{
155 type: Injectable,
156 args: [{ providedIn: 'root' }]
157 }] });
158class RootCmp {
159}
160RootCmp.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootCmp, deps: [], target: i0.ɵɵFactoryTarget.Component });
161RootCmp.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.2.3", type: RootCmp, isStandalone: true, selector: "ng-component", viewQueries: [{ propertyName: "outlet", first: true, predicate: RouterOutlet, descendants: true }], ngImport: i0, template: '<router-outlet></router-outlet>', isInline: true, dependencies: [{ kind: "directive", type: RouterOutlet, selector: "router-outlet", inputs: ["name"], outputs: ["activate", "deactivate", "attach", "detach"], exportAs: ["outlet"] }] });
162i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootCmp, decorators: [{
163 type: Component,
164 args: [{
165 standalone: true,
166 template: '<router-outlet></router-outlet>',
167 imports: [RouterOutlet],
168 }]
169 }], propDecorators: { outlet: [{
170 type: ViewChild,
171 args: [RouterOutlet]
172 }] } });
173/**
174 * A testing harness for the `Router` to reduce the boilerplate needed to test routes and routed
175 * components.
176 *
177 * @publicApi
178 */
179class RouterTestingHarness {
180 /**
181 * Creates a `RouterTestingHarness` instance.
182 *
183 * The `RouterTestingHarness` also creates its own root component with a `RouterOutlet` for the
184 * purposes of rendering route components.
185 *
186 * Throws an error if an instance has already been created.
187 * Use of this harness also requires `destroyAfterEach: true` in the `ModuleTeardownOptions`
188 *
189 * @param initialUrl The target of navigation to trigger before returning the harness.
190 */
191 static create(initialUrl) {
192 return __awaiter(this, void 0, void 0, function* () {
193 const harness = TestBed.inject(RootFixtureService).createHarness();
194 if (initialUrl !== undefined) {
195 yield harness.navigateByUrl(initialUrl);
196 }
197 return harness;
198 });
199 }
200 /** @internal */
201 constructor(fixture) {
202 this.fixture = fixture;
203 }
204 /** Instructs the root fixture to run change detection. */
205 detectChanges() {
206 this.fixture.detectChanges();
207 }
208 /** The `DebugElement` of the `RouterOutlet` component. `null` if the outlet is not activated. */
209 get routeDebugElement() {
210 const outlet = this.fixture.componentInstance.outlet;
211 if (!outlet || !outlet.isActivated) {
212 return null;
213 }
214 return this.fixture.debugElement.query(v => v.componentInstance === outlet.component);
215 }
216 /** The native element of the `RouterOutlet` component. `null` if the outlet is not activated. */
217 get routeNativeElement() {
218 var _a, _b;
219 return (_b = (_a = this.routeDebugElement) === null || _a === void 0 ? void 0 : _a.nativeElement) !== null && _b !== void 0 ? _b : null;
220 }
221 navigateByUrl(url, requiredRoutedComponentType) {
222 return __awaiter(this, void 0, void 0, function* () {
223 const router = TestBed.inject(Router);
224 let resolveFn;
225 const redirectTrackingPromise = new Promise(resolve => {
226 resolveFn = resolve;
227 });
228 ɵafterNextNavigation(TestBed.inject(Router), resolveFn);
229 yield router.navigateByUrl(url);
230 yield redirectTrackingPromise;
231 this.fixture.detectChanges();
232 const outlet = this.fixture.componentInstance.outlet;
233 // The outlet might not be activated if the user is testing a navigation for a guard that
234 // rejects
235 if (outlet && outlet.isActivated && outlet.activatedRoute.component) {
236 const activatedComponent = outlet.component;
237 if (requiredRoutedComponentType !== undefined &&
238 !(activatedComponent instanceof requiredRoutedComponentType)) {
239 throw new Error(`Unexpected routed component type. Expected ${requiredRoutedComponentType.name} but got ${activatedComponent.constructor.name}`);
240 }
241 return activatedComponent;
242 }
243 else {
244 return null;
245 }
246 });
247 }
248}
249
250/**
251 * @module
252 * @description
253 * Entry point for all public APIs of the router/testing package.
254 */
255
256/**
257 * @module
258 * @description
259 * Entry point for all public APIs of this package.
260 */
261// This file only reexports content of the `src` folder. Keep it that way.
262
263// This file is not used to build this module. It is only used during editing
264
265/**
266 * Generated bundle index. Do not edit.
267 */
268
269export { RouterTestingHarness, RouterTestingModule, setupTestingRouter };
270//# sourceMappingURL=testing.mjs.map