This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(testability): add $testability service #8767
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
'use strict'; | ||
|
||
|
||
function $$TestabilityProvider() { | ||
this.$get = ['$rootScope', '$browser', '$location', | ||
function($rootScope, $browser, $location) { | ||
|
||
/** | ||
* @name $testability | ||
* | ||
* @description | ||
* The private $$testability service provides a collection of methods for use when debugging | ||
* or by automated test and debugging tools. | ||
*/ | ||
var testability = {}; | ||
|
||
/** | ||
* @name $$testability#findBindings | ||
* | ||
* @description | ||
* Returns an array of elements that are bound (via ng-bind or {{}}) | ||
* to expressions matching the input. | ||
* | ||
* @param {Element} element The element root to search from. | ||
* @param {string} expression The binding expression to match. | ||
* @param {boolean} opt_exactMatch If true, only returns exact matches | ||
* for the expression. Filters and whitespace are ignored. | ||
*/ | ||
testability.findBindings = function(element, expression, opt_exactMatch) { | ||
var bindings = element.getElementsByClassName('ng-binding'); | ||
var matches = []; | ||
forEach(bindings, function(binding) { | ||
var dataBinding = angular.element(binding).data('$binding'); | ||
if (dataBinding) { | ||
forEach(dataBinding, function(bindingName) { | ||
if (opt_exactMatch) { | ||
var matcher = new RegExp('(^|\\s)' + expression + '(\\s|\\||$)'); | ||
if (matcher.test(bindingName)) { | ||
matches.push(binding); | ||
} | ||
} else { | ||
if (bindingName.indexOf(expression) != -1) { | ||
matches.push(binding); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
return matches; | ||
}; | ||
|
||
/** | ||
* @name $$testability#findModels | ||
* | ||
* @description | ||
* Returns an array of elements that are two-way found via ng-model to | ||
* expressions matching the input. | ||
* | ||
* @param {Element} element The element root to search from. | ||
* @param {string} expression The model expression to match. | ||
* @param {boolean} opt_exactMatch If true, only returns exact matches | ||
* for the expression. | ||
*/ | ||
testability.findModels = function(element, expression, opt_exactMatch) { | ||
var prefixes = ['ng-', 'data-ng-', 'ng\\:']; | ||
for (var p = 0; p < prefixes.length; ++p) { | ||
var attributeEquals = opt_exactMatch ? '=' : '*='; | ||
var selector = '[' + prefixes[p] + 'model' + attributeEquals + '"' + expression + '"]'; | ||
var elements = element.querySelectorAll(selector); | ||
if (elements.length) { | ||
return elements; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* @name $$testability#getLocation | ||
* | ||
* @description | ||
* Shortcut for getting the location in a browser agnostic way. Returns | ||
* the path, search, and hash. (e.g. /path?a=b#hash) | ||
*/ | ||
testability.getLocation = function() { | ||
return $location.url(); | ||
}; | ||
|
||
/** | ||
* @name $$testability#setLocation | ||
* | ||
* @description | ||
* Shortcut for navigating to a location without doing a full page reload. | ||
* | ||
* @param {string} url The location url (path, search and hash, | ||
* e.g. /path?a=b#hash) to go to. | ||
*/ | ||
testability.setLocation = function(url) { | ||
if (url !== $location.url()) { | ||
$location.url(url); | ||
$rootScope.$digest(); | ||
} | ||
}; | ||
|
||
/** | ||
* @name $$testability#whenStable | ||
* | ||
* @description | ||
* Calls the callback when $timeout and $http requests are completed. | ||
* | ||
* @param {function} callback | ||
*/ | ||
testability.whenStable = function(callback) { | ||
$browser.notifyWhenNoOutstandingRequests(callback); | ||
}; | ||
|
||
return testability; | ||
}]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setLocation works with path while get location returns absolute url. that is inconsistent and surprising.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch. Both work on url now (everything past the authority, e.g. /path?a=b#hash)