Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 158e9aa

Browse files
committed
fix(introspection): getTestability should throw when ElementProbes are unavailable
1 parent 4cbac44 commit 158e9aa

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

lib/introspection.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ _jsify(var obj) {
209209
if (obj == null || obj is js.JsObject) {
210210
return obj;
211211
}
212+
if (obj is _JsObjectProxyable) {
213+
return obj._toJsObject();
214+
}
212215
if (obj is Function) {
213216
return _jsFunction(obj);
214217
}
@@ -269,7 +272,6 @@ class _Testability implements _JsObjectProxyable {
269272
final ElementProbe probe;
270273

271274
_Testability(this.node, this.probe);
272-
_Testability.fromNode(dom.Node node): this(node, _findProbeInTree(node));
273275

274276
notifyWhenNoOutstandingRequests(callback) {
275277
probe.injector.get(VmTurnZone).run(
@@ -336,6 +338,18 @@ class _Testability implements _JsObjectProxyable {
336338
}
337339

338340

341+
_Testability getTestability(dom.Node node) {
342+
ElementProbe probe = _findProbeInTree(node);
343+
if (probe == null) {
344+
throw ("Could not find an ElementProbe for $node.  This might happen "
345+
"either because there is no Angular directive for that node OR "
346+
"because your application is running with ElementProbes disabled "
347+
"(CompilerConfig.elementProbeEnabled = false).");
348+
}
349+
return new _Testability(node, probe);
350+
}
351+
352+
339353
void publishToJavaScript() {
340354
var D = {};
341355
D['ngProbe'] = (nodeOrSelector) => _jsProbe(ngProbe(nodeOrSelector));
@@ -345,7 +359,7 @@ void publishToJavaScript() {
345359
ngQuery(node, selector, containsText);
346360
D['angular'] = {
347361
'resumeBootstrap': ([arg]) {},
348-
'getTestability': (node) => new _Testability.fromNode(node)._toJsObject(),
362+
'getTestability': getTestability,
349363
};
350364
js.JsObject J = _jsify(D);
351365
for (String key in D.keys) {

test/angular_spec.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ main() {
227227
"angular.formatter_internal.OrderBy",
228228
"angular.formatter_internal.Stringify",
229229
"angular.formatter_internal.Uppercase",
230+
"angular.introspection.getTestability",
230231
"angular.introspection.ngDirectives",
231232
"angular.introspection.ngInjector",
232233
"angular.introspection.ngProbe",

test/introspection_spec.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,42 @@ void main() {
5353
expect(toHtml(ngQuery(div, 'li'))).toEqual('<li>stash</li><li>secret</li>');
5454
});
5555

56+
describe('getTestability', () {
57+
for (bool elementProbeEnabled in [false, true]) {
58+
describe('elementProbeEnabled=$elementProbeEnabled', () {
59+
var elt;
60+
61+
beforeEachModule((Module m) {
62+
m.bind(CompilerConfig, toValue:
63+
new CompilerConfig.withOptions(elementProbeEnabled: elementProbeEnabled));
64+
});
65+
66+
beforeEach((TestBed _) {
67+
elt = _.compile('<div ng-bind="0"></div>');
68+
document.body.append(elt);
69+
});
70+
71+
afterEach(() {
72+
elt.remove();
73+
});
74+
75+
if (elementProbeEnabled) {
76+
it('should return a Testability object', () {
77+
expect(getTestability(elt)).toBeDefined();
78+
});
79+
} else {
80+
it('should throw an exception', () {
81+
expect(() => getTestability(elt)).toThrow(
82+
"Could not find an ElementProbe for div.  This might happen "
83+
"either because there is no Angular directive for that node OR "
84+
"because your application is running with ElementProbes "
85+
"disabled (CompilerConfig.elementProbeEnabled = false).");
86+
});
87+
}
88+
});
89+
}
90+
});
91+
5692
describe('JavaScript bindings', () {
5793
var elt, angular, ngtop;
5894

@@ -92,6 +128,7 @@ void main() {
92128
// Issue #1219
93129
if (identical(1, 1.0) || !js.context['DART_VERSION'].toString().contains("version: 1.5.")) {
94130
describe(r'testability', () {
131+
95132
var testability;
96133

97134
beforeEach(() {

0 commit comments

Comments
 (0)