Revert "revert: fix(router): ensure URL is updated after second redirect with UrlUpdateStrategy="eager" (#27523)" (#28300)
This reverts commit eea2b0f288
.
PR Close #28300
This commit is contained in:

committed by
Alex Rickabaugh

parent
ea1b5c100f
commit
33e49c2894
@ -12,7 +12,7 @@ import {ChangeDetectionStrategy, Component, Injectable, NgModule, NgModuleFactor
|
||||
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
|
||||
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
||||
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
||||
import {ActivatedRoute, ActivatedRouteSnapshot, ActivationEnd, ActivationStart, CanActivate, CanDeactivate, ChildActivationEnd, ChildActivationStart, DetachedRouteHandle, Event, GuardsCheckEnd, GuardsCheckStart, Navigation, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, PRIMARY_OUTLET, ParamMap, Params, PreloadAllModules, PreloadingStrategy, Resolve, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteReuseStrategy, Router, RouterEvent, RouterModule, RouterPreloader, RouterStateSnapshot, RoutesRecognized, RunGuardsAndResolvers, UrlHandlingStrategy, UrlSegmentGroup, UrlSerializer, UrlTree} from '@angular/router';
|
||||
import {ActivatedRoute, ActivatedRouteSnapshot, ActivationEnd, ActivationStart, CanActivate, CanDeactivate, ChildActivationEnd, ChildActivationStart, DefaultUrlSerializer, DetachedRouteHandle, Event, GuardsCheckEnd, GuardsCheckStart, Navigation, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, PRIMARY_OUTLET, ParamMap, Params, PreloadAllModules, PreloadingStrategy, Resolve, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteReuseStrategy, Router, RouterEvent, RouterModule, RouterPreloader, RouterStateSnapshot, RoutesRecognized, RunGuardsAndResolvers, UrlHandlingStrategy, UrlSegmentGroup, UrlSerializer, UrlTree} from '@angular/router';
|
||||
import {Observable, Observer, Subscription, of } from 'rxjs';
|
||||
import {filter, first, map, tap} from 'rxjs/operators';
|
||||
|
||||
@ -571,64 +571,110 @@ describe('Integration', () => {
|
||||
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
||||
})));
|
||||
|
||||
it('should eagerly update the URL with urlUpdateStrategy="eagar"',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = TestBed.createComponent(RootCmp);
|
||||
advance(fixture);
|
||||
describe('"eager" urlUpdateStrategy', () => {
|
||||
beforeEach(() => {
|
||||
const serializer = new DefaultUrlSerializer();
|
||||
TestBed.configureTestingModule({
|
||||
providers: [{
|
||||
provide: 'authGuardFail',
|
||||
useValue: (a: any, b: any) => {
|
||||
return new Promise(res => { setTimeout(() => res(serializer.parse('/login')), 1); });
|
||||
}
|
||||
}]
|
||||
});
|
||||
|
||||
router.resetConfig([{path: 'team/:id', component: TeamCmp}]);
|
||||
});
|
||||
|
||||
router.navigateByUrl('/team/22');
|
||||
advance(fixture);
|
||||
expect(location.path()).toEqual('/team/22');
|
||||
|
||||
expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]');
|
||||
it('should eagerly update the URL with urlUpdateStrategy="eagar"',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = TestBed.createComponent(RootCmp);
|
||||
advance(fixture);
|
||||
|
||||
router.resetConfig([{path: 'team/:id', component: TeamCmp}]);
|
||||
|
||||
router.navigateByUrl('/team/22');
|
||||
advance(fixture);
|
||||
expect(location.path()).toEqual('/team/22');
|
||||
|
||||
router.urlUpdateStrategy = 'eager';
|
||||
(router as any).hooks.beforePreactivation = () => {
|
||||
expect(location.path()).toEqual('/team/33');
|
||||
expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]');
|
||||
return of (null);
|
||||
};
|
||||
router.navigateByUrl('/team/33');
|
||||
|
||||
advance(fixture);
|
||||
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
||||
})));
|
||||
router.urlUpdateStrategy = 'eager';
|
||||
(router as any).hooks.beforePreactivation = () => {
|
||||
expect(location.path()).toEqual('/team/33');
|
||||
expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]');
|
||||
return of (null);
|
||||
};
|
||||
router.navigateByUrl('/team/33');
|
||||
|
||||
it('should eagerly update URL after redirects are applied with urlUpdateStrategy="eagar"',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = TestBed.createComponent(RootCmp);
|
||||
advance(fixture);
|
||||
advance(fixture);
|
||||
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
||||
})));
|
||||
it('should eagerly update the URL with urlUpdateStrategy="eagar"',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = TestBed.createComponent(RootCmp);
|
||||
advance(fixture);
|
||||
|
||||
router.resetConfig([{path: 'team/:id', component: TeamCmp}]);
|
||||
router.urlUpdateStrategy = 'eager';
|
||||
|
||||
router.navigateByUrl('/team/22');
|
||||
advance(fixture);
|
||||
expect(location.path()).toEqual('/team/22');
|
||||
router.resetConfig([
|
||||
{path: 'team/:id', component: SimpleCmp, canActivate: ['authGuardFail']},
|
||||
{path: 'login', component: AbsoluteSimpleLinkCmp}
|
||||
]);
|
||||
|
||||
expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]');
|
||||
router.navigateByUrl('/team/22');
|
||||
advance(fixture);
|
||||
expect(location.path()).toEqual('/team/22');
|
||||
|
||||
router.urlUpdateStrategy = 'eager';
|
||||
// Redirects to /login
|
||||
advance(fixture, 1);
|
||||
expect(location.path()).toEqual('/login');
|
||||
|
||||
let urlAtNavStart = '';
|
||||
let urlAtRoutesRecognized = '';
|
||||
router.events.subscribe(e => {
|
||||
if (e instanceof NavigationStart) {
|
||||
urlAtNavStart = location.path();
|
||||
}
|
||||
if (e instanceof RoutesRecognized) {
|
||||
urlAtRoutesRecognized = location.path();
|
||||
}
|
||||
});
|
||||
// Perform the same logic again, and it should produce the same result
|
||||
router.navigateByUrl('/team/22');
|
||||
advance(fixture);
|
||||
expect(location.path()).toEqual('/team/22');
|
||||
|
||||
router.navigateByUrl('/team/33');
|
||||
// Redirects to /login
|
||||
advance(fixture, 1);
|
||||
expect(location.path()).toEqual('/login');
|
||||
})));
|
||||
|
||||
advance(fixture);
|
||||
expect(urlAtNavStart).toBe('/team/22');
|
||||
expect(urlAtRoutesRecognized).toBe('/team/33');
|
||||
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
||||
})));
|
||||
it('should eagerly update URL after redirects are applied with urlUpdateStrategy="eagar"',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = TestBed.createComponent(RootCmp);
|
||||
advance(fixture);
|
||||
|
||||
router.resetConfig([{path: 'team/:id', component: TeamCmp}]);
|
||||
|
||||
router.navigateByUrl('/team/22');
|
||||
advance(fixture);
|
||||
expect(location.path()).toEqual('/team/22');
|
||||
|
||||
expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]');
|
||||
|
||||
router.urlUpdateStrategy = 'eager';
|
||||
|
||||
let urlAtNavStart = '';
|
||||
let urlAtRoutesRecognized = '';
|
||||
router.events.subscribe(e => {
|
||||
if (e instanceof NavigationStart) {
|
||||
urlAtNavStart = location.path();
|
||||
}
|
||||
if (e instanceof RoutesRecognized) {
|
||||
urlAtRoutesRecognized = location.path();
|
||||
}
|
||||
});
|
||||
|
||||
router.navigateByUrl('/team/33');
|
||||
|
||||
advance(fixture);
|
||||
expect(urlAtNavStart).toBe('/team/22');
|
||||
expect(urlAtRoutesRecognized).toBe('/team/33');
|
||||
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
||||
})));
|
||||
|
||||
});
|
||||
|
||||
it('should navigate back and forward',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
@ -4656,6 +4702,10 @@ class DummyLinkCmp {
|
||||
}
|
||||
}
|
||||
|
||||
@Component({selector: 'link-cmp', template: `<a [routerLink]="['/simple']">link</a>`})
|
||||
class AbsoluteSimpleLinkCmp {
|
||||
}
|
||||
|
||||
@Component({selector: 'link-cmp', template: `<a [routerLink]="['../simple']">link</a>`})
|
||||
class RelativeLinkCmp {
|
||||
}
|
||||
@ -4836,8 +4886,8 @@ class ThrowingCmp {
|
||||
|
||||
|
||||
|
||||
function advance(fixture: ComponentFixture<any>): void {
|
||||
tick();
|
||||
function advance(fixture: ComponentFixture<any>, millis?: number): void {
|
||||
tick(millis);
|
||||
fixture.detectChanges();
|
||||
}
|
||||
|
||||
@ -4865,6 +4915,7 @@ class LazyComponent {
|
||||
StringLinkCmp,
|
||||
DummyLinkCmp,
|
||||
AbsoluteLinkCmp,
|
||||
AbsoluteSimpleLinkCmp,
|
||||
RelativeLinkCmp,
|
||||
DummyLinkWithParentCmp,
|
||||
LinkWithQueryParamsAndFragment,
|
||||
@ -4893,6 +4944,7 @@ class LazyComponent {
|
||||
StringLinkCmp,
|
||||
DummyLinkCmp,
|
||||
AbsoluteLinkCmp,
|
||||
AbsoluteSimpleLinkCmp,
|
||||
RelativeLinkCmp,
|
||||
DummyLinkWithParentCmp,
|
||||
LinkWithQueryParamsAndFragment,
|
||||
@ -4923,6 +4975,7 @@ class LazyComponent {
|
||||
StringLinkCmp,
|
||||
DummyLinkCmp,
|
||||
AbsoluteLinkCmp,
|
||||
AbsoluteSimpleLinkCmp,
|
||||
RelativeLinkCmp,
|
||||
DummyLinkWithParentCmp,
|
||||
LinkWithQueryParamsAndFragment,
|
||||
|
Reference in New Issue
Block a user