From 4d338a4f5c65d8408c25cc8b309f5cf29cb022e6 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 4 Jun 2015 12:15:34 +0200 Subject: [PATCH] test(ElementInjector): test that hostInjector has priority over viewInjector --- .../core/compiler/element_injector_spec.ts | 21 +++++------ .../test/core/compiler/integration_spec.ts | 35 ++++++++++++++++++- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/modules/angular2/test/core/compiler/element_injector_spec.ts b/modules/angular2/test/core/compiler/element_injector_spec.ts index dce49534f4..1e7396c0d8 100644 --- a/modules/angular2/test/core/compiler/element_injector_spec.ts +++ b/modules/angular2/test/core/compiler/element_injector_spec.ts @@ -599,15 +599,6 @@ export function main() { expect(childInj.get('injectable2')).toEqual('injectable1-injectable2'); }); - it("should instantiate components that depends on viewInjector dependencies", function() { - var inj = injector( - [DirectiveBinding.createFromType( - NeedsService, - new dirAnn.Component({viewInjector: [bind('service').toValue('service')]}))], - null, true); - expect(inj.get(NeedsService).service).toEqual('service'); - }); - it("should instantiate hostInjector injectables that have dependencies", () => { var inj = injector(ListWrapper.concat( [DirectiveBinding.createFromType(SimpleDirective, new dirAnn.Directive({ @@ -624,7 +615,7 @@ export function main() { expect(inj.get('injectable2')).toEqual('injectable1-injectable2'); }); - it("should instantiate components that depends on viewInjector dependencies", function() { + it("should instantiate components that depends on viewInjector dependencies", () => { var inj = injector( ListWrapper.concat([DirectiveBinding.createFromType(NeedsService, new dirAnn.Component({ viewInjector: [bind('service').toValue('service')] @@ -634,6 +625,16 @@ export function main() { expect(inj.get(NeedsService).service).toEqual('service'); }); + it("should prioritize hostInjector over viewInjector for the same binding", () => { + var inj = injector( + ListWrapper.concat([DirectiveBinding.createFromType(NeedsService, new dirAnn.Component({ + hostInjector: [bind('service').toValue('hostService')], + viewInjector: [bind('service').toValue('viewService')]}) + )], extraBindings), null, true); + expect(inj.get(NeedsService).service).toEqual('hostService'); + }); + + it("should instantiate directives that depend on app services", () => { var appInjector = Injector.resolveAndCreate( ListWrapper.concat([bind("service").toValue("service")], extraBindings)); diff --git a/modules/angular2/test/core/compiler/integration_spec.ts b/modules/angular2/test/core/compiler/integration_spec.ts index 4e6a78100d..fcc123b61e 100644 --- a/modules/angular2/test/core/compiler/integration_spec.ts +++ b/modules/angular2/test/core/compiler/integration_spec.ts @@ -967,6 +967,28 @@ export function main() { }); })); + + it('should prioritze hostInjector over viewInjector for the same binding', + inject([TestBed, AsyncTestCompleter], (tb, async) => { + tb.overrideView(MyComp, new viewAnn.View({ + template: ` + + + + + `, + directives: + [DirectiveProvidingInjectableInHostAndView, DirectiveConsumingInjectable] + })); + tb.createView(MyComp, {context: ctx}) + .then((view) => { + var comp = view.rawView.locals.get("consuming"); + expect(comp.injectable).toEqual("host"); + + async.done(); + }); + })); + it("should support viewInjector", inject([TestBed, AsyncTestCompleter], (tb, async) => { tb.overrideView(DirectiveProvidingInjectableInView, new viewAnn.View({ template: ` @@ -1592,13 +1614,24 @@ class DirectiveProvidingInjectable { class DirectiveProvidingInjectableInView { } +@Component({ + selector: 'directive-providing-injectable', + hostInjector: [new Binding(InjectableService, {toValue: 'host'})], + viewInjector: [new Binding(InjectableService, {toValue: 'view'})] +}) +@View({template: ''}) +@Injectable() +class DirectiveProvidingInjectableInHostAndView { +} + + @Component({selector: 'directive-consuming-injectable'}) @View({template: ''}) @Injectable() class DirectiveConsumingInjectable { injectable; - constructor(@Ancestor() injectable: InjectableService) { this.injectable = injectable; } + constructor(@Ancestor() @Inject(InjectableService) injectable) { this.injectable = injectable; } }