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 { TestBed } from '@angular/core/testing';
|
13 |
|
14 | function isUrlHandlingStrategy(opts) {
|
15 |
|
16 |
|
17 | return 'shouldProcessUrl' in opts;
|
18 | }
|
19 | function throwInvalidConfigError(parameter) {
|
20 | throw new Error(`Parameter ${parameter} does not match the one available in the injector. ` +
|
21 | '`setupTestingRouter` is meant to be used as a factory function with dependencies coming from DI.');
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function setupTestingRouter(urlSerializer, contexts, location, compiler, injector, routes, opts, urlHandlingStrategy, routeReuseStrategy, titleStrategy) {
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | if (urlSerializer !== inject(UrlSerializer)) {
|
36 | throwInvalidConfigError('urlSerializer');
|
37 | }
|
38 | if (contexts !== inject(ChildrenOutletContexts)) {
|
39 | throwInvalidConfigError('contexts');
|
40 | }
|
41 | if (location !== inject(Location)) {
|
42 | throwInvalidConfigError('location');
|
43 | }
|
44 | if (compiler !== inject(Compiler)) {
|
45 | throwInvalidConfigError('compiler');
|
46 | }
|
47 | if (injector !== inject(Injector)) {
|
48 | throwInvalidConfigError('injector');
|
49 | }
|
50 | if (routes !== inject(ROUTES)) {
|
51 | throwInvalidConfigError('routes');
|
52 | }
|
53 | if (opts) {
|
54 |
|
55 | if (isUrlHandlingStrategy(opts)) {
|
56 | if (opts !== inject(UrlHandlingStrategy)) {
|
57 | throwInvalidConfigError('opts (UrlHandlingStrategy)');
|
58 | }
|
59 | }
|
60 | else {
|
61 | if (opts !== inject(ROUTER_CONFIGURATION)) {
|
62 | throwInvalidConfigError('opts (ROUTER_CONFIGURATION)');
|
63 | }
|
64 | }
|
65 | }
|
66 | if (urlHandlingStrategy !== inject(UrlHandlingStrategy)) {
|
67 | throwInvalidConfigError('urlHandlingStrategy');
|
68 | }
|
69 | if (routeReuseStrategy !== inject(RouteReuseStrategy)) {
|
70 | throwInvalidConfigError('routeReuseStrategy');
|
71 | }
|
72 | if (titleStrategy !== inject(TitleStrategy)) {
|
73 | throwInvalidConfigError('titleStrategy');
|
74 | }
|
75 | return new Router();
|
76 | }
|
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 | class RouterTestingModule {
|
103 | static withRoutes(routes, config) {
|
104 | return {
|
105 | ngModule: RouterTestingModule,
|
106 | providers: [
|
107 | { provide: ROUTES, multi: true, useValue: routes },
|
108 | { provide: ROUTER_CONFIGURATION, useValue: config ? config : {} },
|
109 | ]
|
110 | };
|
111 | }
|
112 | }
|
113 | RouterTestingModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
114 | RouterTestingModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, exports: [RouterModule] });
|
115 | RouterTestingModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, providers: [
|
116 | ɵROUTER_PROVIDERS,
|
117 | provideLocationMocks(),
|
118 | ɵwithPreloading(NoPreloading).ɵproviders,
|
119 | { provide: ROUTES, multi: true, useValue: [] },
|
120 | ], imports: [RouterModule] });
|
121 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RouterTestingModule, decorators: [{
|
122 | type: NgModule,
|
123 | args: [{
|
124 | exports: [RouterModule],
|
125 | providers: [
|
126 | ɵROUTER_PROVIDERS,
|
127 | provideLocationMocks(),
|
128 | ɵwithPreloading(NoPreloading).ɵproviders,
|
129 | { provide: ROUTES, multi: true, useValue: [] },
|
130 | ]
|
131 | }]
|
132 | }] });
|
133 |
|
134 | class RootFixtureService {
|
135 | createHarness() {
|
136 | if (this.harness) {
|
137 | throw new Error('Only one harness should be created per test.');
|
138 | }
|
139 | this.harness = new RouterTestingHarness(this.getRootFixture());
|
140 | return this.harness;
|
141 | }
|
142 | getRootFixture() {
|
143 | if (this.fixture !== undefined) {
|
144 | return this.fixture;
|
145 | }
|
146 | this.fixture = TestBed.createComponent(RootCmp);
|
147 | this.fixture.detectChanges();
|
148 | return this.fixture;
|
149 | }
|
150 | }
|
151 | RootFixtureService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
152 | RootFixtureService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, providedIn: 'root' });
|
153 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootFixtureService, decorators: [{
|
154 | type: Injectable,
|
155 | args: [{ providedIn: 'root' }]
|
156 | }] });
|
157 | class RootCmp {
|
158 | }
|
159 | RootCmp.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootCmp, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
160 | 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"] }] });
|
161 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.3", ngImport: i0, type: RootCmp, decorators: [{
|
162 | type: Component,
|
163 | args: [{
|
164 | standalone: true,
|
165 | template: '<router-outlet></router-outlet>',
|
166 | imports: [RouterOutlet],
|
167 | }]
|
168 | }], propDecorators: { outlet: [{
|
169 | type: ViewChild,
|
170 | args: [RouterOutlet]
|
171 | }] } });
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | class RouterTestingHarness {
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | static async create(initialUrl) {
|
191 | const harness = TestBed.inject(RootFixtureService).createHarness();
|
192 | if (initialUrl !== undefined) {
|
193 | await harness.navigateByUrl(initialUrl);
|
194 | }
|
195 | return harness;
|
196 | }
|
197 |
|
198 | constructor(fixture) {
|
199 | this.fixture = fixture;
|
200 | }
|
201 |
|
202 | detectChanges() {
|
203 | this.fixture.detectChanges();
|
204 | }
|
205 |
|
206 | get routeDebugElement() {
|
207 | const outlet = this.fixture.componentInstance.outlet;
|
208 | if (!outlet || !outlet.isActivated) {
|
209 | return null;
|
210 | }
|
211 | return this.fixture.debugElement.query(v => v.componentInstance === outlet.component);
|
212 | }
|
213 |
|
214 | get routeNativeElement() {
|
215 | return this.routeDebugElement?.nativeElement ?? null;
|
216 | }
|
217 | async navigateByUrl(url, requiredRoutedComponentType) {
|
218 | const router = TestBed.inject(Router);
|
219 | let resolveFn;
|
220 | const redirectTrackingPromise = new Promise(resolve => {
|
221 | resolveFn = resolve;
|
222 | });
|
223 | ɵafterNextNavigation(TestBed.inject(Router), resolveFn);
|
224 | await router.navigateByUrl(url);
|
225 | await redirectTrackingPromise;
|
226 | this.fixture.detectChanges();
|
227 | const outlet = this.fixture.componentInstance.outlet;
|
228 |
|
229 |
|
230 | if (outlet && outlet.isActivated && outlet.activatedRoute.component) {
|
231 | const activatedComponent = outlet.component;
|
232 | if (requiredRoutedComponentType !== undefined &&
|
233 | !(activatedComponent instanceof requiredRoutedComponentType)) {
|
234 | throw new Error(`Unexpected routed component type. Expected ${requiredRoutedComponentType.name} but got ${activatedComponent.constructor.name}`);
|
235 | }
|
236 | return activatedComponent;
|
237 | }
|
238 | else {
|
239 | return null;
|
240 | }
|
241 | }
|
242 | }
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | export { RouterTestingHarness, RouterTestingModule, setupTestingRouter };
|
264 |
|