|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {CommonModule, HashLocationStrategy, Location, LocationStrategy, PlatformLocation, PopStateEvent} from '@angular/common'; |
10 | | -import {ChangeDetectionStrategy, Component, EnvironmentInjector, inject as coreInject, Inject, Injectable, InjectionToken, NgModule, NgModuleRef, NgZone, OnDestroy, QueryList, Type, ViewChild, ViewChildren, ɵConsole as Console, ɵNoopNgZone as NoopNgZone} from '@angular/core'; |
| 10 | +import {ApplicationRef, ChangeDetectionStrategy, Component, EnvironmentInjector, inject as coreInject, Inject, Injectable, InjectionToken, NgModule, NgModuleRef, NgZone, OnDestroy, QueryList, Type, ViewChild, ViewChildren, ɵConsole as Console, ɵNoopNgZone as NoopNgZone} from '@angular/core'; |
11 | 11 | import {ComponentFixture, fakeAsync, inject, TestBed, tick} from '@angular/core/testing'; |
12 | 12 | import {By} from '@angular/platform-browser/src/dom/debug/by'; |
13 | 13 | import {expect} from '@angular/platform-browser/testing/src/matchers'; |
14 | 14 | import {ActivatedRoute, ActivatedRouteSnapshot, ActivationEnd, ActivationStart, ChildActivationEnd, ChildActivationStart, DefaultUrlSerializer, DetachedRouteHandle, Event, GuardsCheckEnd, GuardsCheckStart, Navigation, NavigationCancel, NavigationCancellationCode, NavigationEnd, NavigationError, NavigationSkipped, NavigationStart, ParamMap, Params, PreloadAllModules, PreloadingStrategy, PRIMARY_OUTLET, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, Router, RouteReuseStrategy, RouterEvent, RouterLink, RouterLinkActive, RouterModule, RouterOutlet, RouterPreloader, RouterStateSnapshot, RoutesRecognized, RunGuardsAndResolvers, UrlHandlingStrategy, UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from '@angular/router'; |
15 | 15 | import {RouterTestingHarness} from '@angular/router/testing'; |
16 | | -import {concat, EMPTY, Observable, Observer, of, Subscription} from 'rxjs'; |
| 16 | +import {concat, EMPTY, firstValueFrom, Observable, Observer, of, Subscription} from 'rxjs'; |
17 | 17 | import {delay, filter, first, last, map, mapTo, takeWhile, tap} from 'rxjs/operators'; |
18 | 18 |
|
19 | 19 | import {CanActivateChildFn, CanActivateFn, CanMatchFn, Data, ResolveFn} from '../src/models'; |
@@ -158,6 +158,37 @@ describe('Integration', () => { |
158 | 158 | expect(observedInfo).toEqual('navigation info'); |
159 | 159 | }); |
160 | 160 |
|
| 161 | + it('should set transient navigation info for routerlink', async () => { |
| 162 | + let observedInfo: unknown; |
| 163 | + const router = TestBed.inject(Router); |
| 164 | + router.resetConfig([ |
| 165 | + { |
| 166 | + path: 'simple', |
| 167 | + component: SimpleCmp, |
| 168 | + canActivate: [() => { |
| 169 | + observedInfo = coreInject(Router).getCurrentNavigation()?.extras?.info; |
| 170 | + return true; |
| 171 | + }] |
| 172 | + }, |
| 173 | + ]); |
| 174 | + @Component({ |
| 175 | + standalone: true, |
| 176 | + imports: [RouterLink], |
| 177 | + template: `<a #simpleLink [routerLink]="'/simple'" [info]="simpleLink"></a>` |
| 178 | + }) |
| 179 | + class App { |
| 180 | + } |
| 181 | + |
| 182 | + const fixture = TestBed.createComponent(App); |
| 183 | + fixture.autoDetectChanges(); |
| 184 | + const anchor = fixture.nativeElement.querySelector('a'); |
| 185 | + anchor.click(); |
| 186 | + await fixture.whenStable(); |
| 187 | + |
| 188 | + // An example use-case might be to pass the clicked link along with the navigation information |
| 189 | + expect(observedInfo).toBeInstanceOf(HTMLAnchorElement); |
| 190 | + }); |
| 191 | + |
161 | 192 | it('should make transient navigation info available in redirect', async () => { |
162 | 193 | let observedInfo: unknown; |
163 | 194 | const router = TestBed.inject(Router); |
|
0 commit comments