1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import { Location } from '@angular/common';
|
8 | import { provideLocationMocks } from '@angular/common/testing';
|
9 | import * as i0 from '@angular/core';
|
10 | import { inject, Compiler, Injector, NgModule, Injectable, Component, ViewChild } from '@angular/core';
|
11 | import { UrlSerializer, ChildrenOutletContexts, ROUTES, UrlHandlingStrategy, ROUTER_CONFIGURATION, RouteReuseStrategy, TitleStrategy, Router, RouterModule, ɵROUTER_PROVIDERS, ɵwithPreloading, NoPreloading, RouterOutlet, ɵafterNextNavigation } from '@angular/router';
|
12 | import { __awaiter } from 'tslib';
|
13 | import { TestBed } from '@angular/core/testing';
|
14 |
|
15 | function isUrlHandlingStrategy(opts) {
|
16 |
|
17 |
|
18 | return 'shouldProcessUrl' in opts;
|
19 | }
|
20 | function 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 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function setupTestingRouter(urlSerializer, contexts, location, compiler, injector, routes, opts, urlHandlingStrategy, routeReuseStrategy, titleStrategy) {
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
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 |
|
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 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | class 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 | }
|
114 | RouterTestingModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
115 | RouterTestingModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, exports: [RouterModule] });
|
116 | RouterTestingModule.ɵ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] });
|
122 | i0.ɵɵ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 |
|
135 | class 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 | }
|
152 | RootFixtureService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
153 | RootFixtureService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, providedIn: 'root' });
|
154 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, decorators: [{
|
155 | type: Injectable,
|
156 | args: [{ providedIn: 'root' }]
|
157 | }] });
|
158 | class RootCmp {
|
159 | }
|
160 | RootCmp.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootCmp, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
161 | RootCmp.ɵ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"] }] });
|
162 | i0.ɵɵ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 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | class RouterTestingHarness {
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
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 |
|
201 | constructor(fixture) {
|
202 | this.fixture = fixture;
|
203 | }
|
204 |
|
205 | detectChanges() {
|
206 | this.fixture.detectChanges();
|
207 | }
|
208 |
|
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 |
|
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 |
|
234 |
|
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 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | export { RouterTestingHarness, RouterTestingModule, setupTestingRouter };
|
270 |
|