angular.js/test/testabilityPatch.js

217 lines
5.8 KiB
JavaScript
Raw Normal View History

/**
* Here is the problem: http://bugs.jquery.com/ticket/7292
* basically jQuery treats change event on some browsers (IE) as a
* special event and changes it form 'change' to 'click/keyup' and
* few others. This horrible hack removes the special treatment
*/
_jQuery.event.special.change = undefined;
if (window.jstestdriver) {
jstd = jstestdriver;
dump = bind(jstd.console, jstd.console.log);
}
2010-03-15 21:36:50 +00:00
beforeEach(function(){
this.addMatchers({
toBeInvalid: function(){
var element = jqLite(this.actual);
var hasClass = element.hasClass('ng-validation-error');
var validationError = element.attr('ng-validation-error');
this.message = function(){
if (!hasClass)
return "Expected class 'ng-validation-error' not found.";
return "Expected an error message, but none was found.";
};
return hasClass && validationError;
},
toBeValid: function(){
var element = jqLite(this.actual);
var hasClass = element.hasClass('ng-validation-error');
this.message = function(){
return "Expected to not have class 'ng-validation-error' but found.";
};
return !hasClass;
},
toEqualData: function(expected) {
return equals(this.actual, expected);
},
toHaveClass: function(clazz) {
this.message = function(){
return "Expected '" + sortedHtml(this.actual) + "' to have class '" + clazz + "'.";
};
2010-10-15 20:44:53 +00:00
return this.actual.hasClass ?
this.actual.hasClass(clazz) :
jqLite(this.actual).hasClass(clazz);
}
});
});
2010-03-15 21:36:50 +00:00
function nakedExpect(obj) {
return expect(angular.fromJson(angular.toJson(obj)));
2010-04-04 00:04:36 +00:00
}
2010-01-06 00:36:58 +00:00
2010-03-30 21:55:04 +00:00
function childNode(element, index) {
return jqLite(element[0].childNodes[index]);
}
2010-04-05 21:09:25 +00:00
extend(angular, {
'element': jqLite,
2010-04-05 21:09:25 +00:00
'compile': compile,
'scope': createScope,
2010-04-05 21:09:25 +00:00
'copy': copy,
'extend': extend,
'equals': equals,
2010-04-05 21:09:25 +00:00
'foreach': foreach,
'noop':noop,
'bind':bind,
'toJson': toJson,
'fromJson': fromJson,
2010-04-05 21:09:25 +00:00
'identity':identity,
'isUndefined': isUndefined,
'isDefined': isDefined,
'isString': isString,
'isFunction': isFunction,
'isObject': isObject,
2010-04-05 21:09:25 +00:00
'isNumber': isNumber,
'isArray': isArray
2010-04-05 21:09:25 +00:00
});
function sortedHtml(element, showNgClass) {
2010-01-06 00:36:58 +00:00
var html = "";
foreach(jqLite(element), function toString(node) {
2010-01-06 00:36:58 +00:00
if (node.nodeName == "#text") {
html += node.nodeValue.
replace(/&(\w+[&;\W])?/g, function(match, entity){return entity?match:'&';}).
replace(/</g, '&lt;').
replace(/>/g, '&gt;');
2010-01-06 00:36:58 +00:00
} else {
html += '<' + node.nodeName.toLowerCase();
var attributes = node.attributes || [];
var attrs = [];
var className = node.className || '';
if (!showNgClass) {
className = className.replace(/ng-[\w-]+\s*/g, '');
}
className = trim(className);
if (className) {
attrs.push(' class="' + className + '"');
}
2010-01-06 00:36:58 +00:00
for(var i=0; i<attributes.length; i++) {
var attr = attributes[i];
if(attr.name.match(/^ng:/) ||
2010-01-06 00:36:58 +00:00
attr.value &&
attr.value !='null' &&
attr.value !='auto' &&
attr.value !='false' &&
attr.value !='inherit' &&
attr.value !='0' &&
attr.name !='loop' &&
2010-04-21 19:50:05 +00:00
attr.name !='complete' &&
2010-01-06 00:36:58 +00:00
attr.name !='maxLength' &&
attr.name !='size' &&
2010-07-23 20:54:12 +00:00
attr.name !='class' &&
2010-01-06 00:36:58 +00:00
attr.name !='start' &&
attr.name !='tabIndex' &&
attr.name !='style' &&
2010-01-06 00:36:58 +00:00
attr.name.substr(0, 6) != 'jQuery') {
// in IE we need to check for all of these.
if (!/ng-\d+/.exec(attr.name))
2010-04-21 19:50:05 +00:00
attrs.push(' ' + attr.name + '="' + attr.value + '"');
2010-01-06 00:36:58 +00:00
}
}
attrs.sort();
html += attrs.join('');
2010-04-15 21:17:33 +00:00
if (node.style) {
var style = [];
if (node.style.cssText) {
foreach(node.style.cssText.split(';'), function(value){
value = trim(value);
if (value) {
2010-04-21 19:50:05 +00:00
style.push(lowercase(value));
2010-04-15 21:17:33 +00:00
}
});
}
for(var css in node.style){
var value = node.style[css];
if (isString(value) && isString(css) && css != 'cssText' && value && (1*css != css)) {
2010-04-21 19:50:05 +00:00
var text = lowercase(css + ': ' + value);
2010-04-19 21:36:41 +00:00
if (value != 'false' && indexOf(style, text) == -1) {
2010-04-15 21:17:33 +00:00
style.push(text);
}
}
2010-04-19 21:41:36 +00:00
}
2010-04-13 02:16:30 +00:00
style.sort();
2010-04-22 22:50:20 +00:00
var tmp = style;
style = [];
foreach(tmp, function(value){
if (!value.match(/^max[^\-]/))
style.push(value);
});
2010-04-15 21:17:33 +00:00
if (style.length) {
html += ' style="' + style.join('; ') + ';"';
}
}
2010-01-06 00:36:58 +00:00
html += '>';
var children = node.childNodes;
for(var j=0; j<children.length; j++) {
toString(children[j]);
2010-01-06 00:36:58 +00:00
}
html += '</' + node.nodeName.toLowerCase() + '>';
}
});
2010-01-06 00:36:58 +00:00
return html;
2010-04-04 00:04:36 +00:00
}
2010-01-06 00:36:58 +00:00
2010-04-08 20:43:40 +00:00
function isCssVisible(node) {
2010-03-31 20:57:25 +00:00
var display = node.css('display');
if (display == 'block') display = "";
return display != 'none';
}
2010-01-06 00:36:58 +00:00
function assertHidden(node) {
2010-04-08 20:43:40 +00:00
assertFalse("Node should be hidden but vas visible: " + sortedHtml(node), isCssVisible(node));
2010-01-06 00:36:58 +00:00
}
function assertVisible(node) {
2010-04-08 20:43:40 +00:00
assertTrue("Node should be visible but vas hidden: " + sortedHtml(node), isCssVisible(node));
2010-01-06 00:36:58 +00:00
}
function assertJsonEquals(expected, actual) {
2010-01-09 23:02:43 +00:00
assertEquals(toJson(expected), toJson(actual));
2010-01-06 00:36:58 +00:00
}
function assertUndefined(value) {
assertEquals('undefined', typeof value);
}
function assertDefined(value) {
2010-01-09 23:02:43 +00:00
assertTrue(toJson(value), !!value);
2010-01-06 00:36:58 +00:00
}
function assertThrows(error, fn){
var exception = null;
try {
fn();
} catch(e) {
exception = e;
}
if (!exception) {
fail("Expecting exception, none thrown");
}
assertEquals(error, exception);
}
log = noop;
2010-03-15 21:36:50 +00:00
error = noop;
2010-04-23 00:11:56 +00:00
2010-10-15 20:44:53 +00:00
function rethrow(e) {
if(e) {
2010-10-15 20:44:53 +00:00
throw e;
}
}