8
8
9
9
import { CommonModule , Location } from '@angular/common' ;
10
10
import { SpyLocation } from '@angular/common/testing' ;
11
- import { ChangeDetectionStrategy , Component , Injectable , NgModule , NgModuleFactoryLoader , NgModuleRef } from '@angular/core' ;
11
+ import { ChangeDetectionStrategy , Component , Injectable , NgModule , NgModuleFactoryLoader , NgModuleRef , NgZone , ɵConsole as Console , ɵNoopNgZone as NoopNgZone } from '@angular/core' ;
12
12
import { ComponentFixture , TestBed , fakeAsync , inject , tick } from '@angular/core/testing' ;
13
13
import { By } from '@angular/platform-browser/src/dom/debug/by' ;
14
14
import { expect } from '@angular/platform-browser/testing/src/matchers' ;
@@ -20,10 +20,13 @@ import {forEach} from '../src/utils/collection';
20
20
import { RouterTestingModule , SpyNgModuleFactoryLoader } from '../testing' ;
21
21
22
22
describe ( 'Integration' , ( ) => {
23
+ const noopConsole : Console = { log ( ) { } , warn ( ) { } } ;
24
+
23
25
beforeEach ( ( ) => {
24
26
TestBed . configureTestingModule ( {
25
27
imports :
26
- [ RouterTestingModule . withRoutes ( [ { path : 'simple' , component : SimpleCmp } ] ) , TestModule ]
28
+ [ RouterTestingModule . withRoutes ( [ { path : 'simple' , component : SimpleCmp } ] ) , TestModule ] ,
29
+ providers : [ { provide : Console , useValue : noopConsole } ]
27
30
} ) ;
28
31
} ) ;
29
32
@@ -154,6 +157,50 @@ describe('Integration', () => {
154
157
} ) ) ) ;
155
158
} ) ;
156
159
160
+ describe ( 'navigation warning' , ( ) => {
161
+ let warnings : string [ ] = [ ] ;
162
+
163
+ class MockConsole {
164
+ warn ( message : string ) { warnings . push ( message ) ; }
165
+ }
166
+
167
+ beforeEach ( ( ) => {
168
+ warnings = [ ] ;
169
+ TestBed . overrideProvider ( Console , { useValue : new MockConsole ( ) } ) ;
170
+ } ) ;
171
+
172
+ describe ( 'with NgZone enabled' , ( ) => {
173
+ it ( 'should warn when triggered outside Angular zone' ,
174
+ fakeAsync ( inject ( [ Router , NgZone ] , ( router : Router , ngZone : NgZone ) => {
175
+ ngZone . runOutsideAngular ( ( ) => { router . navigateByUrl ( '/simple' ) ; } ) ;
176
+
177
+ expect ( warnings . length ) . toBe ( 1 ) ;
178
+ expect ( warnings [ 0 ] )
179
+ . toBe (
180
+ `Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?` ) ;
181
+ } ) ) ) ;
182
+
183
+ it ( 'should not warn when triggered inside Angular zone' ,
184
+ fakeAsync ( inject ( [ Router , NgZone ] , ( router : Router , ngZone : NgZone ) => {
185
+ ngZone . run ( ( ) => { router . navigateByUrl ( '/simple' ) ; } ) ;
186
+
187
+ expect ( warnings . length ) . toBe ( 0 ) ;
188
+ } ) ) ) ;
189
+ } ) ;
190
+
191
+ describe ( 'with NgZone disabled' , ( ) => {
192
+ beforeEach ( ( ) => { TestBed . overrideProvider ( NgZone , { useValue : new NoopNgZone ( ) } ) ; } ) ;
193
+
194
+ it ( 'should not warn when triggered outside Angular zone' ,
195
+ fakeAsync ( inject ( [ Router , NgZone ] , ( router : Router , ngZone : NgZone ) => {
196
+
197
+ ngZone . runOutsideAngular ( ( ) => { router . navigateByUrl ( '/simple' ) ; } ) ;
198
+
199
+ expect ( warnings . length ) . toBe ( 0 ) ;
200
+ } ) ) ) ;
201
+ } ) ;
202
+ } ) ;
203
+
157
204
describe ( 'should execute navigations serially' , ( ) => {
158
205
let log : any [ ] = [ ] ;
159
206
0 commit comments