angular.js/test/testabilityPatch.js

118 lines
2.9 KiB
JavaScript
Raw Normal View History

2010-01-06 00:36:58 +00:00
jstd = jstestdriver;
dump = bind(jstd.console, jstd.console.log);
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,
'compile': compile,
'scope': createScope,
'copy': copy,
'extend': extend,
'foreach': foreach,
'noop':noop,
'identity':identity,
'isUndefined': isUndefined,
'isDefined': isDefined,
'isString': isString,
'isFunction': isFunction,
'isNumber': isNumber,
'isArray': isArray
});
function trigger(element, type) {
var evnt = document.createEvent('MouseEvent');
evnt.initMouseEvent(type, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
(element[0] || element).dispatchEvent(evnt);
}
function sortedHtml(element) {
2010-01-06 00:36:58 +00:00
var html = "";
(function toString(node) {
2010-01-06 00:36:58 +00:00
if (node.nodeName == "#text") {
2010-01-09 23:02:43 +00:00
html += escapeHtml(node.nodeValue);
2010-01-06 00:36:58 +00:00
} else {
html += '<' + node.nodeName.toLowerCase();
var attributes = node.attributes || [];
var attrs = [];
for(var i=0; i<attributes.length; i++) {
var attr = attributes[i];
if(attr.name.match(/^ng-/) ||
attr.value &&
attr.value !='null' &&
attr.value !='auto' &&
attr.value !='false' &&
attr.value !='inherit' &&
attr.value !='0' &&
attr.name !='loop' &&
attr.name !='maxLength' &&
attr.name !='size' &&
attr.name !='start' &&
attr.name !='tabIndex' &&
attr.name.substr(0, 6) != 'jQuery') {
// in IE we need to check for all of these.
attrs.push(' ' + attr.name + '="' + attr.value + '"');
}
}
attrs.sort();
html += attrs.join('');
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() + '>';
}
})(element[0]);
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;