test(ElementInjector): test that hostInjector has priority over viewInjector

This commit is contained in:
Victor Berchet 2015-06-04 12:15:34 +02:00
parent 6a6b43de07
commit 4d338a4f5c
2 changed files with 45 additions and 11 deletions

View File

@ -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));

View File

@ -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: `
<directive-providing-injectable>
<directive-consuming-injectable #consuming>
</directive-consuming-injectable>
</directive-providing-injectable>
`,
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; }
}