6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { ComponentFactory , ComponentRef , Injector , NgModuleRef , SimpleChange , SimpleChanges , Type } from '@angular/core' ;
9
+ import { ComponentFactory , ComponentRef , Injector , NgModuleRef , NgZone , SimpleChange , SimpleChanges , Type } from '@angular/core' ;
10
10
import { fakeAsync , tick } from '@angular/core/testing' ;
11
11
import { Subject } from 'rxjs' ;
12
12
@@ -20,12 +20,27 @@ describe('ComponentFactoryNgElementStrategy', () => {
20
20
let injector : any ;
21
21
let componentRef : any ;
22
22
let applicationRef : any ;
23
+ let factoryResolver : any ;
24
+ let ngZone : any ;
23
25
24
26
beforeEach ( ( ) => {
25
27
factory = new FakeComponentFactory ( ) ;
26
28
componentRef = factory . componentRef ;
27
29
28
30
applicationRef = jasmine . createSpyObj ( 'applicationRef' , [ 'attachView' ] ) ;
31
+ ngZone = jasmine . createSpyObj ( 'ngZone' , [ 'run' ] ) ;
32
+ ngZone . run . and . callFake ( ( fn : any ) => { return fn ( ) ; } ) ;
33
+ injector = jasmine . createSpyObj ( 'injector' , [ 'get' ] ) ;
34
+ injector . get . and . callFake ( function ( identify : any ) {
35
+ const name = identify && identify . name ;
36
+ if ( name === 'ApplicationRef' ) {
37
+ return applicationRef ;
38
+ } else if ( name === 'NgZone' ) {
39
+ return ngZone ;
40
+ } else if ( name === 'ComponentFactoryResolver' ) {
41
+ return factoryResolver ;
42
+ }
43
+ } ) ;
29
44
30
45
strategy = new ComponentNgElementStrategy ( factory , injector ) ;
31
46
} ) ;
@@ -40,6 +55,34 @@ describe('ComponentFactoryNgElementStrategy', () => {
40
55
expect ( strategyFactory . create ( injector ) ) . toBeTruthy ( ) ;
41
56
} ) ;
42
57
58
+ describe ( 'connected' , ( ) => {
59
+
60
+ it ( 'should not run initializeComponentFn in ngZone if is already inAngularZone' ,
61
+ fakeAsync ( ( ) => {
62
+ const spy = spyOn ( NgZone , 'isInAngularZone' ) ;
63
+ spy . and . callFake ( function ( ) { return true ; } ) ;
64
+ ngZone . run . calls . reset ( ) ;
65
+
66
+ strategy . connect ( document . createElement ( 'div' ) ) ;
67
+
68
+ expect ( ngZone . run ) . not . toHaveBeenCalled ( ) ;
69
+ ngZone . run . and . callFake ( ( ) => { } ) ;
70
+ tick ( 16 ) ; // scheduler waits 16ms if RAF is unavailable
71
+ expect ( ngZone . run ) . not . toHaveBeenCalled ( ) ;
72
+ } ) ) ;
73
+
74
+ it ( 'should run initializeComponentFn in ngZone if is not already inAngularZone' ,
75
+ fakeAsync ( ( ) => {
76
+ const spy = spyOn ( NgZone , 'isInAngularZone' ) ;
77
+ spy . and . callFake ( function ( ) { return false ; } ) ;
78
+ ngZone . run . calls . reset ( ) ;
79
+
80
+ strategy . connect ( document . createElement ( 'div' ) ) ;
81
+
82
+ expect ( ngZone . run ) . toHaveBeenCalled ( ) ;
83
+ } ) ) ;
84
+
85
+ } ) ;
43
86
describe ( 'after connected' , ( ) => {
44
87
beforeEach ( ( ) => {
45
88
// Set up an initial value to make sure it is passed to the component
0 commit comments