Skip to content

Commit 4d338a4

Browse files
committed
test(ElementInjector): test that hostInjector has priority over viewInjector
1 parent 6a6b43d commit 4d338a4

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

modules/angular2/test/core/compiler/element_injector_spec.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -599,15 +599,6 @@ export function main() {
599599
expect(childInj.get('injectable2')).toEqual('injectable1-injectable2');
600600
});
601601

602-
it("should instantiate components that depends on viewInjector dependencies", function() {
603-
var inj = injector(
604-
[DirectiveBinding.createFromType(
605-
NeedsService,
606-
new dirAnn.Component({viewInjector: [bind('service').toValue('service')]}))],
607-
null, true);
608-
expect(inj.get(NeedsService).service).toEqual('service');
609-
});
610-
611602
it("should instantiate hostInjector injectables that have dependencies", () => {
612603
var inj = injector(ListWrapper.concat(
613604
[DirectiveBinding.createFromType(SimpleDirective, new dirAnn.Directive({
@@ -624,7 +615,7 @@ export function main() {
624615
expect(inj.get('injectable2')).toEqual('injectable1-injectable2');
625616
});
626617

627-
it("should instantiate components that depends on viewInjector dependencies", function() {
618+
it("should instantiate components that depends on viewInjector dependencies", () => {
628619
var inj = injector(
629620
ListWrapper.concat([DirectiveBinding.createFromType(NeedsService, new dirAnn.Component({
630621
viewInjector: [bind('service').toValue('service')]
@@ -634,6 +625,16 @@ export function main() {
634625
expect(inj.get(NeedsService).service).toEqual('service');
635626
});
636627

628+
it("should prioritize hostInjector over viewInjector for the same binding", () => {
629+
var inj = injector(
630+
ListWrapper.concat([DirectiveBinding.createFromType(NeedsService, new dirAnn.Component({
631+
hostInjector: [bind('service').toValue('hostService')],
632+
viewInjector: [bind('service').toValue('viewService')]})
633+
)], extraBindings), null, true);
634+
expect(inj.get(NeedsService).service).toEqual('hostService');
635+
});
636+
637+
637638
it("should instantiate directives that depend on app services", () => {
638639
var appInjector = Injector.resolveAndCreate(
639640
ListWrapper.concat([bind("service").toValue("service")], extraBindings));

modules/angular2/test/core/compiler/integration_spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,28 @@ export function main() {
967967
});
968968
}));
969969

970+
971+
it('should prioritze hostInjector over viewInjector for the same binding',
972+
inject([TestBed, AsyncTestCompleter], (tb, async) => {
973+
tb.overrideView(MyComp, new viewAnn.View({
974+
template: `
975+
<directive-providing-injectable>
976+
<directive-consuming-injectable #consuming>
977+
</directive-consuming-injectable>
978+
</directive-providing-injectable>
979+
`,
980+
directives:
981+
[DirectiveProvidingInjectableInHostAndView, DirectiveConsumingInjectable]
982+
}));
983+
tb.createView(MyComp, {context: ctx})
984+
.then((view) => {
985+
var comp = view.rawView.locals.get("consuming");
986+
expect(comp.injectable).toEqual("host");
987+
988+
async.done();
989+
});
990+
}));
991+
970992
it("should support viewInjector", inject([TestBed, AsyncTestCompleter], (tb, async) => {
971993
tb.overrideView(DirectiveProvidingInjectableInView, new viewAnn.View({
972994
template: `
@@ -1592,13 +1614,24 @@ class DirectiveProvidingInjectable {
15921614
class DirectiveProvidingInjectableInView {
15931615
}
15941616

1617+
@Component({
1618+
selector: 'directive-providing-injectable',
1619+
hostInjector: [new Binding(InjectableService, {toValue: 'host'})],
1620+
viewInjector: [new Binding(InjectableService, {toValue: 'view'})]
1621+
})
1622+
@View({template: ''})
1623+
@Injectable()
1624+
class DirectiveProvidingInjectableInHostAndView {
1625+
}
1626+
1627+
15951628
@Component({selector: 'directive-consuming-injectable'})
15961629
@View({template: ''})
15971630
@Injectable()
15981631
class DirectiveConsumingInjectable {
15991632
injectable;
16001633

1601-
constructor(@Ancestor() injectable: InjectableService) { this.injectable = injectable; }
1634+
constructor(@Ancestor() @Inject(InjectableService) injectable) { this.injectable = injectable; }
16021635
}
16031636

16041637

0 commit comments

Comments
 (0)