8
8
9
9
import { Component , ComponentFactoryResolver , createEnvironmentInjector , ENVIRONMENT_INITIALIZER , EnvironmentInjector , InjectionToken , INJECTOR , Injector , NgModuleRef } from '@angular/core' ;
10
10
import { R3Injector } from '@angular/core/src/di/r3_injector' ;
11
+ import { TestBed } from '@angular/core/testing' ;
11
12
12
13
describe ( 'environment injector' , ( ) => {
13
14
it ( 'should create and destroy an environment injector' , ( ) => {
14
15
class Service { }
15
16
16
17
let destroyed = false ;
17
- const envInjector = createEnvironmentInjector ( [ Service ] ) as R3Injector ;
18
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
19
+ const envInjector = createEnvironmentInjector ( [ Service ] , parentEnvInjector ) as R3Injector ;
18
20
envInjector . onDestroy ( ( ) => destroyed = true ) ;
19
21
20
22
const service = envInjector . get ( Service ) ;
@@ -27,38 +29,45 @@ describe('environment injector', () => {
27
29
it ( 'should see providers from a parent EnvInjector' , ( ) => {
28
30
class Service { }
29
31
30
- const envInjector = createEnvironmentInjector ( [ ] , createEnvironmentInjector ( [ Service ] ) ) ;
32
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
33
+ const envInjector =
34
+ createEnvironmentInjector ( [ ] , createEnvironmentInjector ( [ Service ] , parentEnvInjector ) ) ;
31
35
expect ( envInjector . get ( Service ) ) . toBeInstanceOf ( Service ) ;
32
36
} ) ;
33
37
34
38
it ( 'should shadow providers from the parent EnvInjector' , ( ) => {
35
39
const token = new InjectionToken ( 'token' ) ;
36
40
41
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
37
42
const envInjector = createEnvironmentInjector (
38
43
[ { provide : token , useValue : 'child' } ] ,
39
- createEnvironmentInjector ( [ { provide : token , useValue : 'parent' } ] ) ) ;
44
+ createEnvironmentInjector ( [ { provide : token , useValue : 'parent' } ] , parentEnvInjector ) ) ;
40
45
expect ( envInjector . get ( token ) ) . toBe ( 'child' ) ;
41
46
} ) ;
42
47
43
48
it ( 'should expose the Injector token' , ( ) => {
44
- const envInjector = createEnvironmentInjector ( [ ] ) ;
49
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
50
+ const envInjector = createEnvironmentInjector ( [ ] , parentEnvInjector ) ;
45
51
expect ( envInjector . get ( Injector ) ) . toBe ( envInjector ) ;
46
52
expect ( envInjector . get ( INJECTOR ) ) . toBe ( envInjector ) ;
47
53
} ) ;
48
54
49
55
it ( 'should expose the EnvInjector token' , ( ) => {
50
- const envInjector = createEnvironmentInjector ( [ ] ) ;
56
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
57
+ const envInjector = createEnvironmentInjector ( [ ] , parentEnvInjector ) ;
51
58
expect ( envInjector . get ( EnvironmentInjector ) ) . toBe ( envInjector ) ;
52
59
} ) ;
53
60
54
61
it ( 'should expose the same object as both the Injector and EnvInjector token' , ( ) => {
55
- const envInjector = createEnvironmentInjector ( [ ] ) ;
62
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
63
+ const envInjector = createEnvironmentInjector ( [ ] , parentEnvInjector ) ;
56
64
expect ( envInjector . get ( Injector ) ) . toBe ( envInjector . get ( EnvironmentInjector ) ) ;
57
65
} ) ;
58
66
59
67
it ( 'should expose the NgModuleRef token' , ( ) => {
60
68
class Service { }
61
- const envInjector = createEnvironmentInjector ( [ Service ] ) ;
69
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
70
+ const envInjector = createEnvironmentInjector ( [ Service ] , parentEnvInjector ) ;
62
71
63
72
const ngModuleRef = envInjector . get ( NgModuleRef ) ;
64
73
@@ -78,7 +87,8 @@ describe('environment injector', () => {
78
87
constructor ( readonly service : Service ) { }
79
88
}
80
89
81
- const envInjector = createEnvironmentInjector ( [ Service ] ) ;
90
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
91
+ const envInjector = createEnvironmentInjector ( [ Service ] , parentEnvInjector ) ;
82
92
const cfr = envInjector . get ( ComponentFactoryResolver ) ;
83
93
const cf = cfr . resolveComponentFactory ( TestComponent ) ;
84
94
const cRef = cf . create ( Injector . NULL ) ;
@@ -88,17 +98,21 @@ describe('environment injector', () => {
88
98
89
99
it ( 'should support the ENVIRONMENT_INITIALIZER muli-token' , ( ) => {
90
100
let initialized = false ;
91
- createEnvironmentInjector ( [ {
92
- provide : ENVIRONMENT_INITIALIZER ,
93
- useValue : ( ) => initialized = true ,
94
- multi : true ,
95
- } ] ) ;
101
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
102
+ createEnvironmentInjector (
103
+ [ {
104
+ provide : ENVIRONMENT_INITIALIZER ,
105
+ useValue : ( ) => initialized = true ,
106
+ multi : true ,
107
+ } ] ,
108
+ parentEnvInjector ) ;
96
109
97
110
expect ( initialized ) . toBeTrue ( ) ;
98
111
} ) ;
99
112
100
113
it ( 'should adopt environment-scoped providers' , ( ) => {
101
- const injector = createEnvironmentInjector ( [ ] ) ;
114
+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
115
+ const injector = createEnvironmentInjector ( [ ] , parentEnvInjector ) ;
102
116
const EnvScopedToken = new InjectionToken ( 'env-scoped token' , {
103
117
providedIn : 'environment' as any ,
104
118
factory : ( ) => true ,
0 commit comments