|
| 1 | +'use strict'; |
| 2 | + |
| 3 | + |
| 4 | +function $$TestabilityProvider() { |
| 5 | + this.$get = ['$rootScope', '$browser', '$location', |
| 6 | + function($rootScope, $browser, $location) { |
| 7 | + |
| 8 | + /** |
| 9 | + * @name $testability |
| 10 | + * |
| 11 | + * @description |
| 12 | + * The private $$testability service provides a collection of methods for use when debugging |
| 13 | + * or by automated test and debugging tools. |
| 14 | + */ |
| 15 | + var testability = {}; |
| 16 | + |
| 17 | + /** |
| 18 | + * @name $$testability#findBindings |
| 19 | + * |
| 20 | + * @description |
| 21 | + * Returns an array of elements that are bound (via ng-bind or {{}}) |
| 22 | + * to expressions matching the input. |
| 23 | + * |
| 24 | + * @param {Element} element The element root to search from. |
| 25 | + * @param {string} expression The binding expression to match. |
| 26 | + * @param {boolean} opt_exactMatch If true, only returns exact matches |
| 27 | + * for the expression. Filters and whitespace are ignored. |
| 28 | + */ |
| 29 | + testability.findBindings = function(element, expression, opt_exactMatch) { |
| 30 | + var bindings = element.getElementsByClassName('ng-binding'); |
| 31 | + var matches = []; |
| 32 | + forEach(bindings, function(binding) { |
| 33 | + var dataBinding = angular.element(binding).data('$binding'); |
| 34 | + if (dataBinding) { |
| 35 | + forEach(dataBinding, function(bindingName) { |
| 36 | + if (opt_exactMatch) { |
| 37 | + var matcher = new RegExp('(^|\\s)' + expression + '(\\s|\\||$)'); |
| 38 | + if (matcher.test(bindingName)) { |
| 39 | + matches.push(binding); |
| 40 | + } |
| 41 | + } else { |
| 42 | + if (bindingName.indexOf(expression) != -1) { |
| 43 | + matches.push(binding); |
| 44 | + } |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + }); |
| 49 | + return matches; |
| 50 | + }; |
| 51 | + |
| 52 | + /** |
| 53 | + * @name $$testability#findModels |
| 54 | + * |
| 55 | + * @description |
| 56 | + * Returns an array of elements that are two-way found via ng-model to |
| 57 | + * expressions matching the input. |
| 58 | + * |
| 59 | + * @param {Element} element The element root to search from. |
| 60 | + * @param {string} expression The model expression to match. |
| 61 | + * @param {boolean} opt_exactMatch If true, only returns exact matches |
| 62 | + * for the expression. |
| 63 | + */ |
| 64 | + testability.findModels = function(element, expression, opt_exactMatch) { |
| 65 | + var prefixes = ['ng-', 'data-ng-', 'ng\\:']; |
| 66 | + for (var p = 0; p < prefixes.length; ++p) { |
| 67 | + var attributeEquals = opt_exactMatch ? '=' : '*='; |
| 68 | + var selector = '[' + prefixes[p] + 'model' + attributeEquals + '"' + expression + '"]'; |
| 69 | + var elements = element.querySelectorAll(selector); |
| 70 | + if (elements.length) { |
| 71 | + return elements; |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + /** |
| 77 | + * @name $$testability#getLocation |
| 78 | + * |
| 79 | + * @description |
| 80 | + * Shortcut for getting the location in a browser agnostic way. Returns |
| 81 | + * the path, search, and hash. (e.g. /path?a=b#hash) |
| 82 | + */ |
| 83 | + testability.getLocation = function() { |
| 84 | + return $location.url(); |
| 85 | + }; |
| 86 | + |
| 87 | + /** |
| 88 | + * @name $$testability#setLocation |
| 89 | + * |
| 90 | + * @description |
| 91 | + * Shortcut for navigating to a location without doing a full page reload. |
| 92 | + * |
| 93 | + * @param {string} url The location url (path, search and hash, |
| 94 | + * e.g. /path?a=b#hash) to go to. |
| 95 | + */ |
| 96 | + testability.setLocation = function(url) { |
| 97 | + if (url !== $location.url()) { |
| 98 | + $location.url(url); |
| 99 | + $rootScope.$digest(); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + /** |
| 104 | + * @name $$testability#whenStable |
| 105 | + * |
| 106 | + * @description |
| 107 | + * Calls the callback when $timeout and $http requests are completed. |
| 108 | + * |
| 109 | + * @param {function} callback |
| 110 | + */ |
| 111 | + testability.whenStable = function(callback) { |
| 112 | + $browser.notifyWhenNoOutstandingRequests(callback); |
| 113 | + }; |
| 114 | + |
| 115 | + return testability; |
| 116 | + }]; |
| 117 | +} |
0 commit comments