@@ -12,6 +12,7 @@ import {
12
12
Directive ,
13
13
Inject ,
14
14
Injector ,
15
+ Optional ,
15
16
TemplateRef ,
16
17
ViewChild ,
17
18
ViewContainerRef ,
@@ -125,6 +126,21 @@ describe('Dialog', () => {
125
126
) ;
126
127
} ) ;
127
128
129
+ it ( 'should use custom test injector' , ( ) => {
130
+ // Create a custom injector for the component.
131
+ let viewContainerFixtureChocolateInjector = TestBed . createComponent ( PizzaSnack ) ;
132
+ viewContainerFixtureChocolateInjector . detectChanges ( ) ;
133
+ let chocolateInjector = new ChocolateInjector (
134
+ viewContainerFixtureChocolateInjector . componentInstance . injector ,
135
+ ) ;
136
+
137
+ let dialogRef = dialog . open ( PizzaSnack , {
138
+ injector : chocolateInjector ,
139
+ } ) ;
140
+
141
+ expect ( dialogRef . componentInstance ! . snack . toString ( ) ) . toBe ( 'Chocolate' ) ;
142
+ } ) ;
143
+
128
144
it ( 'should open a dialog with a component and no ViewContainerRef' , ( ) => {
129
145
let dialogRef = dialog . open ( PizzaMsg ) ;
130
146
@@ -1223,3 +1239,26 @@ class DialogWithoutFocusableElements {}
1223
1239
encapsulation : ViewEncapsulation . ShadowDom ,
1224
1240
} )
1225
1241
class ShadowDomComponent { }
1242
+
1243
+ class Chocolate {
1244
+ toString ( ) {
1245
+ return 'Chocolate' ;
1246
+ }
1247
+ }
1248
+
1249
+ class ChocolateInjector {
1250
+ constructor ( public parentInjector : Injector ) { }
1251
+
1252
+ get ( token : any ) {
1253
+ return token === Chocolate ? new Chocolate ( ) : this . parentInjector . get < any > ( token ) ;
1254
+ }
1255
+ }
1256
+
1257
+ /** Simple component for testing custom injector. */
1258
+ @Component ( {
1259
+ selector : 'pizza-snack' ,
1260
+ template : '<p>Pizza</p><p>{{snack}}</p><ng-content></ng-content>' ,
1261
+ } )
1262
+ class PizzaSnack {
1263
+ constructor ( @Optional ( ) public snack : Chocolate , public injector : Injector ) { }
1264
+ }
0 commit comments