From d5236133297eab13ea70b5577d8aa4f7ae1be62c Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 21 May 2015 16:56:02 -0700 Subject: [PATCH] test(di): added a test verifying hierarchical injection --- modules/angular2/src/di/forward_ref.ts | 2 + .../test/core/compiler/integration_spec.ts | 104 +++++++++++++++++- npm-shrinkwrap.clean.json | 2 +- npm-shrinkwrap.json | 6 +- package.json | 2 +- 5 files changed, 109 insertions(+), 7 deletions(-) diff --git a/modules/angular2/src/di/forward_ref.ts b/modules/angular2/src/di/forward_ref.ts index 2db7d4983e..9e103d4899 100644 --- a/modules/angular2/src/di/forward_ref.ts +++ b/modules/angular2/src/di/forward_ref.ts @@ -33,6 +33,8 @@ export function forwardRef(forwardRefFn: ForwardRefFn): Type { return (forwardRefFn); } +export var FORWARD_REF = forwardRef; + /** * Lazily retrieve the reference value. * diff --git a/modules/angular2/test/core/compiler/integration_spec.ts b/modules/angular2/test/core/compiler/integration_spec.ts index 9033808d0c..ae65374363 100644 --- a/modules/angular2/test/core/compiler/integration_spec.ts +++ b/modules/angular2/test/core/compiler/integration_spec.ts @@ -28,11 +28,12 @@ import { assertionsEnabled, isJsObject, global, - stringify + stringify, + CONST } from 'angular2/src/facade/lang'; import {PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async'; -import {Injector, bind, Injectable} from 'angular2/di'; +import {Injector, bind, Injectable, Binding, FORWARD_REF} from 'angular2/di'; import { PipeRegistry, defaultPipeRegistry, @@ -54,6 +55,7 @@ import { Query } from 'angular2/annotations'; import * as viewAnn from 'angular2/src/core/annotations_impl/view'; +import * as visAnn from 'angular2/src/core/annotations_impl/visibility'; import {QueryList} from 'angular2/src/core/compiler/query_list'; @@ -985,6 +987,43 @@ export function main() { var comp = view.rawView.locals.get("dir"); expect(comp.directive.injectable).toBeAnInstanceOf(InjectableService); + async.done(); + }); + })); + + it("should support the event-bus scenario", + inject([TestBed, AsyncTestCompleter], (tb, async) => { + tb.overrideView(MyComp, new viewAnn.View({ + template: ` + + + + + + + `, + directives: [ + GrandParentProvidingEventBus, + ParentProvidingEventBus, + ChildConsumingEventBus + ] + })); + tb.createView(MyComp, {context: ctx}) + .then((view) => { + var eis = view.rawView.elementInjectors; + var childRawView = view.rawView.componentChildViews[1]; + + var grandParent = eis[0].get(GrandParentProvidingEventBus); + var parent = eis[1].get(ParentProvidingEventBus); + var child1 = eis[2].get(ChildConsumingEventBus); + var child2 = childRawView.elementInjectors[0].get(ChildConsumingEventBus); + + expect(grandParent.bus.name).toEqual("grandparent"); + expect(parent.bus.name).toEqual("parent"); + expect(parent.grandParentBus).toBe(grandParent.bus); + expect(child1.bus).toBe(parent.bus); + expect(child2.bus).toBe(parent.bus); + async.done(); }); })); @@ -1540,3 +1579,64 @@ class DirectiveConsumingInjectableUnbounded { parent.directive = this; } } + + +@CONST() +class EventBus { + parentEventBus: EventBus; + name: string; + + constructor(parentEventBus: EventBus, name: string) { + this.parentEventBus = parentEventBus; + this.name = name; + } +} + +@Directive({ + selector: 'grand-parent-providing-event-bus', + hostInjector: [new Binding(EventBus, {toValue: new EventBus(null, "grandparent")})] +}) +class GrandParentProvidingEventBus { + bus: EventBus; + + constructor(bus: EventBus) { this.bus = bus; } +} + +function createParentBusHost(peb) { + return new EventBus(peb, "parent"); +} + +function createParentBusView(p) { + return p.bus; +} +@Component({ + selector: 'parent-providing-event-bus', + hostInjector: [new Binding( + EventBus, {toFactory: createParentBusHost, deps: [[EventBus, new visAnn.Unbounded()]]})], + viewInjector: [new Binding( + EventBus, + {toFactory: createParentBusView, deps: [[FORWARD_REF(() => ParentProvidingEventBus)]]})] +}) +@View({ + directives: [FORWARD_REF(() => ChildConsumingEventBus)], + template: ` + + ` +}) +class ParentProvidingEventBus { + bus: EventBus; + grandParentBus: EventBus; + + constructor(bus: EventBus, @Unbounded() grandParentBus: EventBus) { + // constructor(bus: EventBus) { + this.bus = bus; + this.grandParentBus = grandParentBus; + } +} + +@Directive({selector: 'child-consuming-event-bus'}) +class ChildConsumingEventBus { + bus: EventBus; + + constructor(@Unbounded() bus: EventBus) { this.bus = bus; } +} diff --git a/npm-shrinkwrap.clean.json b/npm-shrinkwrap.clean.json index 403386b8ab..d2bf3b1522 100644 --- a/npm-shrinkwrap.clean.json +++ b/npm-shrinkwrap.clean.json @@ -8438,7 +8438,7 @@ } }, "ts2dart": { - "version": "0.5.6", + "version": "0.5.7", "dependencies": { "source-map": { "version": "0.4.2", diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 978ae3f8f5..adea5d701a 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -13027,9 +13027,9 @@ } }, "ts2dart": { - "version": "0.5.6", - "from": "https://registry.npmjs.org/ts2dart/-/ts2dart-0.5.6.tgz", - "resolved": "https://registry.npmjs.org/ts2dart/-/ts2dart-0.5.6.tgz", + "version": "0.5.7", + "from": "https://registry.npmjs.org/ts2dart/-/ts2dart-0.5.7.tgz", + "resolved": "https://registry.npmjs.org/ts2dart/-/ts2dart-0.5.7.tgz", "dependencies": { "source-map": { "version": "0.4.2", diff --git a/package.json b/package.json index a2a1bd6467..3ef7f657fb 100644 --- a/package.json +++ b/package.json @@ -113,7 +113,7 @@ "temp": "^0.8.1", "ternary-stream": "^1.2.3", "through2": "^0.6.1", - "ts2dart": "^0.5.6", + "ts2dart": "^0.5.7", "tsd": "^0.5.7", "typescript": "alexeagle/TypeScript#error_is_class", "vinyl": "^0.4.6",