Skip to content

Commit d523613

Browse files
committed
test(di): added a test verifying hierarchical injection
1 parent cfcae6b commit d523613

File tree

5 files changed

+109
-7
lines changed

5 files changed

+109
-7
lines changed

modules/angular2/src/di/forward_ref.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export function forwardRef(forwardRefFn: ForwardRefFn): Type {
3333
return (<Type><any>forwardRefFn);
3434
}
3535

36+
export var FORWARD_REF = forwardRef;
37+
3638
/**
3739
* Lazily retrieve the reference value.
3840
*

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

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ import {
2828
assertionsEnabled,
2929
isJsObject,
3030
global,
31-
stringify
31+
stringify,
32+
CONST
3233
} from 'angular2/src/facade/lang';
3334
import {PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
3435

35-
import {Injector, bind, Injectable} from 'angular2/di';
36+
import {Injector, bind, Injectable, Binding, FORWARD_REF} from 'angular2/di';
3637
import {
3738
PipeRegistry,
3839
defaultPipeRegistry,
@@ -54,6 +55,7 @@ import {
5455
Query
5556
} from 'angular2/annotations';
5657
import * as viewAnn from 'angular2/src/core/annotations_impl/view';
58+
import * as visAnn from 'angular2/src/core/annotations_impl/visibility';
5759

5860
import {QueryList} from 'angular2/src/core/compiler/query_list';
5961

@@ -985,6 +987,43 @@ export function main() {
985987
var comp = view.rawView.locals.get("dir");
986988
expect(comp.directive.injectable).toBeAnInstanceOf(InjectableService);
987989

990+
async.done();
991+
});
992+
}));
993+
994+
it("should support the event-bus scenario",
995+
inject([TestBed, AsyncTestCompleter], (tb, async) => {
996+
tb.overrideView(MyComp, new viewAnn.View({
997+
template: `
998+
<grand-parent-providing-event-bus>
999+
<parent-providing-event-bus>
1000+
<child-consuming-event-bus>
1001+
</child-consuming-event-bus>
1002+
</parent-providing-event-bus>
1003+
</grand-parent-providing-event-bus>
1004+
`,
1005+
directives: [
1006+
GrandParentProvidingEventBus,
1007+
ParentProvidingEventBus,
1008+
ChildConsumingEventBus
1009+
]
1010+
}));
1011+
tb.createView(MyComp, {context: ctx})
1012+
.then((view) => {
1013+
var eis = view.rawView.elementInjectors;
1014+
var childRawView = view.rawView.componentChildViews[1];
1015+
1016+
var grandParent = eis[0].get(GrandParentProvidingEventBus);
1017+
var parent = eis[1].get(ParentProvidingEventBus);
1018+
var child1 = eis[2].get(ChildConsumingEventBus);
1019+
var child2 = childRawView.elementInjectors[0].get(ChildConsumingEventBus);
1020+
1021+
expect(grandParent.bus.name).toEqual("grandparent");
1022+
expect(parent.bus.name).toEqual("parent");
1023+
expect(parent.grandParentBus).toBe(grandParent.bus);
1024+
expect(child1.bus).toBe(parent.bus);
1025+
expect(child2.bus).toBe(parent.bus);
1026+
9881027
async.done();
9891028
});
9901029
}));
@@ -1540,3 +1579,64 @@ class DirectiveConsumingInjectableUnbounded {
15401579
parent.directive = this;
15411580
}
15421581
}
1582+
1583+
1584+
@CONST()
1585+
class EventBus {
1586+
parentEventBus: EventBus;
1587+
name: string;
1588+
1589+
constructor(parentEventBus: EventBus, name: string) {
1590+
this.parentEventBus = parentEventBus;
1591+
this.name = name;
1592+
}
1593+
}
1594+
1595+
@Directive({
1596+
selector: 'grand-parent-providing-event-bus',
1597+
hostInjector: [new Binding(EventBus, {toValue: new EventBus(null, "grandparent")})]
1598+
})
1599+
class GrandParentProvidingEventBus {
1600+
bus: EventBus;
1601+
1602+
constructor(bus: EventBus) { this.bus = bus; }
1603+
}
1604+
1605+
function createParentBusHost(peb) {
1606+
return new EventBus(peb, "parent");
1607+
}
1608+
1609+
function createParentBusView(p) {
1610+
return p.bus;
1611+
}
1612+
@Component({
1613+
selector: 'parent-providing-event-bus',
1614+
hostInjector: [new Binding(
1615+
EventBus, {toFactory: createParentBusHost, deps: [[EventBus, new visAnn.Unbounded()]]})],
1616+
viewInjector: [new Binding(
1617+
EventBus,
1618+
{toFactory: createParentBusView, deps: [[FORWARD_REF(() => ParentProvidingEventBus)]]})]
1619+
})
1620+
@View({
1621+
directives: [FORWARD_REF(() => ChildConsumingEventBus)],
1622+
template: `
1623+
<child-consuming-event-bus></child-consuming-event-bus>
1624+
`
1625+
})
1626+
class ParentProvidingEventBus {
1627+
bus: EventBus;
1628+
grandParentBus: EventBus;
1629+
1630+
constructor(bus: EventBus, @Unbounded() grandParentBus: EventBus) {
1631+
// constructor(bus: EventBus) {
1632+
this.bus = bus;
1633+
this.grandParentBus = grandParentBus;
1634+
}
1635+
}
1636+
1637+
@Directive({selector: 'child-consuming-event-bus'})
1638+
class ChildConsumingEventBus {
1639+
bus: EventBus;
1640+
1641+
constructor(@Unbounded() bus: EventBus) { this.bus = bus; }
1642+
}

npm-shrinkwrap.clean.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8438,7 +8438,7 @@
84388438
}
84398439
},
84408440
"ts2dart": {
8441-
"version": "0.5.6",
8441+
"version": "0.5.7",
84428442
"dependencies": {
84438443
"source-map": {
84448444
"version": "0.4.2",

npm-shrinkwrap.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
"temp": "^0.8.1",
114114
"ternary-stream": "^1.2.3",
115115
"through2": "^0.6.1",
116-
"ts2dart": "^0.5.6",
116+
"ts2dart": "^0.5.7",
117117
"tsd": "^0.5.7",
118118
"typescript": "alexeagle/TypeScript#error_is_class",
119119
"vinyl": "^0.4.6",

0 commit comments

Comments
 (0)