fix(router): better ngZone checking for warning (#25839)

Do not warn that navigation was triggered outside Angular zone if the
Router was created outside Angular zone in the first place.

Closes #25837

PR Close #25839
This commit is contained in:
Trotyl 2018-09-06 16:48:26 +08:00 committed by atscott
parent c9de7daae4
commit 76954688b1
2 changed files with 17 additions and 13 deletions

View File

@ -491,7 +491,7 @@ export class Router {
this.ngModule = injector.get(NgModuleRef);
this.console = injector.get(Console);
const ngZone = injector.get(NgZone);
this.isNgZoneEnabled = ngZone instanceof NgZone;
this.isNgZoneEnabled = ngZone instanceof NgZone && NgZone.isInAngularZone();
this.resetConfig(config);
this.currentUrlTree = createEmptyUrlTree();

View File

@ -306,7 +306,9 @@ describe('Integration', () => {
});
describe('navigation warning', () => {
const isInAngularZoneFn = NgZone.isInAngularZone;
let warnings: string[] = [];
let isInAngularZone = true;
class MockConsole {
warn(message: string) {
@ -316,15 +318,20 @@ describe('Integration', () => {
beforeEach(() => {
warnings = [];
isInAngularZone = true;
NgZone.isInAngularZone = () => isInAngularZone;
TestBed.overrideProvider(Console, {useValue: new MockConsole()});
});
afterEach(() => {
NgZone.isInAngularZone = isInAngularZoneFn;
});
describe('with NgZone enabled', () => {
it('should warn when triggered outside Angular zone',
fakeAsync(inject([Router, NgZone], (router: Router, ngZone: NgZone) => {
ngZone.runOutsideAngular(() => {
router.navigateByUrl('/simple');
});
fakeAsync(inject([Router], (router: Router) => {
isInAngularZone = false;
router.navigateByUrl('/simple');
expect(warnings.length).toBe(1);
expect(warnings[0])
@ -333,10 +340,8 @@ describe('Integration', () => {
})));
it('should not warn when triggered inside Angular zone',
fakeAsync(inject([Router, NgZone], (router: Router, ngZone: NgZone) => {
ngZone.run(() => {
router.navigateByUrl('/simple');
});
fakeAsync(inject([Router], (router: Router) => {
router.navigateByUrl('/simple');
expect(warnings.length).toBe(0);
})));
@ -348,10 +353,9 @@ describe('Integration', () => {
});
it('should not warn when triggered outside Angular zone',
fakeAsync(inject([Router, NgZone], (router: Router, ngZone: NgZone) => {
ngZone.runOutsideAngular(() => {
router.navigateByUrl('/simple');
});
fakeAsync(inject([Router], (router: Router) => {
isInAngularZone = false;
router.navigateByUrl('/simple');
expect(warnings.length).toBe(0);
})));