mirror of
https://github.com/angular/angular.git
synced 2026-11-25 07:57:49 -05:00
refactor(router): update test based on router quick-cancelling ongoing navigations (#25740)
PR Close #25740
This commit is contained in:
committed by
Alex Rickabaugh
parent
c091d40fb0
commit
9acd04c192
@@ -1,23 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {EMPTY, MonoTypeOperatorFunction, Observable, of } from 'rxjs';
|
||||
import {mergeMap} from 'rxjs/operators';
|
||||
|
||||
export function mergeMapIf<T>(
|
||||
predicate: (value: T) => boolean, tap: (value: T) => any): MonoTypeOperatorFunction<T> {
|
||||
return (source: Observable<T>) => {
|
||||
return source.pipe(mergeMap(s => {
|
||||
if (predicate(s)) {
|
||||
tap(s);
|
||||
return EMPTY;
|
||||
}
|
||||
return of (s);
|
||||
}));
|
||||
};
|
||||
}
|
||||
@@ -11,8 +11,8 @@ import {map, mergeMap} from 'rxjs/operators';
|
||||
|
||||
import {NavigationTransition} from '../router';
|
||||
|
||||
export function resolveData(
|
||||
paramsInheritanceStrategy: 'emptyOnly' | 'always'): MonoTypeOperatorFunction<NavigationTransition> {
|
||||
export function resolveData(paramsInheritanceStrategy: 'emptyOnly' | 'always'):
|
||||
MonoTypeOperatorFunction<NavigationTransition> {
|
||||
return function(source: Observable<NavigationTransition>) {
|
||||
return source.pipe(mergeMap(t => {
|
||||
if (!t.preActivation) {
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {MonoTypeOperatorFunction, Observable} from 'rxjs';
|
||||
import {tap} from 'rxjs/operators';
|
||||
|
||||
export function throwIf<T>(
|
||||
predicate: (value: T) => boolean,
|
||||
errorFactory: (() => any) = defaultErrorFactory): MonoTypeOperatorFunction<T> {
|
||||
return (source: Observable<T>) => {
|
||||
return source.pipe(tap(s => {
|
||||
if (predicate(s)) {
|
||||
throw errorFactory();
|
||||
}
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
function defaultErrorFactory() {
|
||||
return new Error();
|
||||
}
|
||||
+162
-158
File diff suppressed because it is too large
Load Diff
@@ -1,19 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
export function assertDefined<T>(actual: T, msg: string) {
|
||||
if (actual == null) {
|
||||
throwError(msg);
|
||||
}
|
||||
}
|
||||
|
||||
function throwError(msg: string): never {
|
||||
// tslint:disable-next-line
|
||||
debugger; // Left intentionally for better debugger experience.
|
||||
throw new Error(`ASSERTION ERROR: ${msg}`);
|
||||
}
|
||||
@@ -291,49 +291,35 @@ describe('Integration', () => {
|
||||
|
||||
});
|
||||
|
||||
// TODO(jasonaden): This test now fails because it relies on waiting on a guard to finish
|
||||
// executing even after a new navigation has been scheduled. The previous implementation
|
||||
// would do so, but ignore the result of any guards that are executing when a new navigation
|
||||
// is scheduled.
|
||||
it('should not wait for prior navigations to start a new navigation',
|
||||
fakeAsync(inject([Router, Location], (router: Router) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
// With new implementation, the current navigation will be unrolled and cleaned up so the
|
||||
// new navigation can start immediately. This test therefore fails as it relies on that
|
||||
// previous incorrect behavior.
|
||||
xit('should execute navigations serialy',
|
||||
fakeAsync(inject([Router, Location], (router: Router) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
router.resetConfig([
|
||||
{path: 'a', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']},
|
||||
{path: 'b', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']}
|
||||
]);
|
||||
|
||||
router.resetConfig([
|
||||
{path: 'a', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']},
|
||||
{path: 'b', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']}
|
||||
]);
|
||||
router.navigateByUrl('/a');
|
||||
tick(100);
|
||||
fixture.detectChanges();
|
||||
|
||||
router.navigateByUrl('/a');
|
||||
tick(100);
|
||||
fixture.detectChanges();
|
||||
router.navigateByUrl('/b');
|
||||
tick(100); // 200
|
||||
fixture.detectChanges();
|
||||
|
||||
router.navigateByUrl('/b');
|
||||
tick(100); // 200
|
||||
fixture.detectChanges();
|
||||
expect(log).toEqual(
|
||||
['trueRightAway', 'trueIn2Seconds-start', 'trueRightAway', 'trueIn2Seconds-start']);
|
||||
|
||||
expect(log).toEqual(['trueRightAway', 'trueIn2Seconds-start']);
|
||||
tick(2000); // 2200
|
||||
fixture.detectChanges();
|
||||
|
||||
tick(2000); // 2200
|
||||
fixture.detectChanges();
|
||||
expect(log).toEqual([
|
||||
'trueRightAway', 'trueIn2Seconds-start', 'trueRightAway', 'trueIn2Seconds-start',
|
||||
'trueIn2Seconds-end', 'trueIn2Seconds-end'
|
||||
]);
|
||||
|
||||
expect(log).toEqual([
|
||||
'trueRightAway', 'trueIn2Seconds-start', 'trueIn2Seconds-end', 'trueRightAway',
|
||||
'trueIn2Seconds-start'
|
||||
]);
|
||||
|
||||
tick(2000); // 4200
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(log).toEqual([
|
||||
'trueRightAway', 'trueIn2Seconds-start', 'trueIn2Seconds-end', 'trueRightAway',
|
||||
'trueIn2Seconds-start', 'trueIn2Seconds-end'
|
||||
]);
|
||||
})));
|
||||
})));
|
||||
});
|
||||
|
||||
it('Should work inside ChangeDetectionStrategy.OnPush components', fakeAsync(() => {
|
||||
@@ -2971,32 +2957,31 @@ describe('Integration', () => {
|
||||
]);
|
||||
})));
|
||||
|
||||
it('should allow redirection in NavigationStart', fakeAsync(inject([Router], (router: Router) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
it('should allow redirection in NavigationStart',
|
||||
fakeAsync(inject([Router], (router: Router) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
router.resetConfig([
|
||||
{path: 'blank', component: UserCmp},
|
||||
{path: 'user/:name', component: BlankCmp},
|
||||
]);
|
||||
router.resetConfig([
|
||||
{path: 'blank', component: UserCmp},
|
||||
{path: 'user/:name', component: BlankCmp},
|
||||
]);
|
||||
|
||||
const navigateSpy = spyOn(router, 'navigate').and.callThrough();
|
||||
const recordedEvents: any[] = [];
|
||||
const navigateSpy = spyOn(router, 'navigate').and.callThrough();
|
||||
const recordedEvents: any[] = [];
|
||||
|
||||
const navStart$ = router.events.pipe(
|
||||
tap(e => recordedEvents.push(e)),
|
||||
filter(e => e instanceof NavigationStart),
|
||||
first()
|
||||
);
|
||||
const navStart$ = router.events.pipe(
|
||||
tap(e => recordedEvents.push(e)), filter(e => e instanceof NavigationStart), first());
|
||||
|
||||
navStart$.subscribe((e: NavigationStart | NavigationError) => {
|
||||
router.navigate(['/blank'], {queryParams: {state: 'redirected'}, queryParamsHandling: 'merge'});
|
||||
advance(fixture);
|
||||
});
|
||||
navStart$.subscribe((e: NavigationStart | NavigationError) => {
|
||||
router.navigate(
|
||||
['/blank'], {queryParams: {state: 'redirected'}, queryParamsHandling: 'merge'});
|
||||
advance(fixture);
|
||||
});
|
||||
|
||||
router.navigate(['/user/:fedor']);
|
||||
advance(fixture);
|
||||
router.navigate(['/user/:fedor']);
|
||||
advance(fixture);
|
||||
|
||||
expect(navigateSpy.calls.mostRecent().args[1].queryParams);
|
||||
expect(navigateSpy.calls.mostRecent().args[1].queryParams);
|
||||
|
||||
})));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user