Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
refactor(clientsidescripts): use the testability api if available
Browse files Browse the repository at this point in the history
Getting ready for version of Angular which will include the testability API.
  • Loading branch information
juliemr committed Aug 22, 2014
1 parent d48c3db commit 0e9c2f4
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/clientsidescripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ var functions = {};
functions.waitForAngular = function(selector, callback) {
var el = document.querySelector(selector);
try {
angular.element(el).injector().get('$browser').
notifyWhenNoOutstandingRequests(callback);
if (angular.getTestability) {
angular.getTestability(el).whenStable(callback);
} else {
angular.element(el).injector().get('$browser').
notifyWhenNoOutstandingRequests(callback);
}
} catch (e) {
callback(e);
}
Expand All @@ -46,6 +50,10 @@ functions.waitForAngular = function(selector, callback) {
*/
functions.findBindings = function(binding, exactMatch, using) {
using = using || document;
if (angular.getTestability) {
return angular.getTestability(using).
findBindings(using, binding, exactMatch);
}
var bindings = using.getElementsByClassName('ng-binding');
var matches = [];
for (var i = 0; i < bindings.length; ++i) {
Expand Down Expand Up @@ -338,6 +346,10 @@ functions.findRepeaterColumn = function(repeater, binding, using) {
*/
functions.findByModel = function(model, using) {
using = using || document;
if (angular.getTestability) {
return angular.getTestability(using).
findModels(using, model);
}
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
for (var p = 0; p < prefixes.length; ++p) {
var selector = '[' + prefixes[p] + 'model="' + model + '"]';
Expand Down Expand Up @@ -515,6 +527,10 @@ functions.allowAnimations = function(element, value) {
*/
functions.getLocationAbsUrl = function(selector) {
var el = document.querySelector(selector);
if (angular.getTestability) {
return angular.getTestability(el).
getLocation();
}
return angular.element(el).injector().get('$location').absUrl();
};

Expand All @@ -527,6 +543,10 @@ functions.getLocationAbsUrl = function(selector) {
*/
functions.setLocation = function(selector, url) {
var el = document.querySelector(selector);
if (angular.getTestability) {
return angular.getTestability(el).
setLocation(url);
}
var $injector = angular.element(el).injector();
var $location = $injector.get('$location');
var $rootScope = $injector.get('$rootScope');
Expand Down

0 comments on commit 0e9c2f4

Please sign in to comment.