From 1d65b38b28887656c86dfff6a89bf90ba152d8d2 Mon Sep 17 00:00:00 2001 From: Ted Sander Date: Tue, 11 Aug 2015 16:09:16 -0700 Subject: [PATCH] feat(test): find testabilities across dart js applications Find angular testability objects across different dart js contexts by registering each application in a global array and interrogating each for testabilities. Closes #3611 --- .../src/core/testability/get_testability.dart | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/modules/angular2/src/core/testability/get_testability.dart b/modules/angular2/src/core/testability/get_testability.dart index d8fbc4757f..923f7a7c6e 100644 --- a/modules/angular2/src/core/testability/get_testability.dart +++ b/modules/angular2/src/core/testability/get_testability.dart @@ -10,17 +10,8 @@ import 'dart:js' as js; // Proxies a Dart function that accepts up to 10 parameters. js.JsFunction _jsFunction(Function fn) { const Object X = __varargSentinel; - return new js.JsFunction.withThis((thisArg, - [o1 = X, - o2 = X, - o3 = X, - o4 = X, - o5 = X, - o6 = X, - o7 = X, - o8 = X, - o9 = X, - o10 = X]) { + return new js.JsFunction.withThis((thisArg, [o1 = X, o2 = X, o3 = X, o4 = X, + o5 = X, o6 = X, o7 = X, o8 = X, o9 = X, o10 = X]) { return __invokeFn(fn, o1, o2, o3, o4, o5, o6, o7, o8, o9, o10); }); } @@ -91,19 +82,46 @@ class PublicTestability implements _JsObjectProxyable { class GetTestability { static addToWindow(TestabilityRegistry registry) { - js.context['getAngularTestability'] = _jsify((Element elem, - [bool findInAncestors = true]) { - Testability testability = registry.findTestabilityInTree(elem, findInAncestors); - if (testability == null) { + var jsRegistry = js.context['ngTestabilityRegistries']; + if (jsRegistry == null) { + js.context['ngTestabilityRegistries'] = jsRegistry = new js.JsArray(); + js.context['getAngularTestability'] = _jsify((Element elem, + [bool findInAncestors = true]) { + var registry = js.context['ngTestabilityRegistries']; + for (int i = 0; i < registry.length; i++) { + var result = registry[i].callMethod( + 'getAngularTestability', [elem, findInAncestors]); + if (result != null) return result; + } throw 'Could not find testability for element.'; - } + }); + js.context['getAllAngularTestabilities'] = _jsify(() { + var registry = js.context['ngTestabilityRegistries']; + var result = []; + for (int i = 0; i < registry.length; i++) { + var testabilities = + registry[i].callMethod('getAllAngularTestabilities'); + if (testabilities != null) result.addAll(testabilities); + } + return _jsify(result); + }); + } + jsRegistry.add(_createRegistry(registry)); + } + + static js.JsObject _createRegistry(TestabilityRegistry registry) { + var object = new js.JsObject(js.context['Object']); + object['getAngularTestability'] = _jsify((Element elem, + bool findInAncestors) { + var testability = registry.findTestabilityInTree(elem, findInAncestors); return _jsify(new PublicTestability(testability)); }); - js.context['getAllAngularTestabilities'] = _jsify(() { - List testabilities = registry.getAllTestabilities(); - var publicTestabilities = testabilities + object['getAllAngularTestabilities'] = _jsify(() { + var publicTestabilities = registry + .getAllTestabilities() .map((testability) => new PublicTestability(testability)); return _jsify(publicTestabilities); }); + return object; } }