add better support of DOM nodes into equality matcher
This commit is contained in:
@@ -122,6 +122,45 @@ describe("matchersUtil", function() {
|
||||
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
||||
});
|
||||
|
||||
it("passes for equivalent DOM nodes", function() {
|
||||
if (typeof document === 'undefined') {
|
||||
return;
|
||||
}
|
||||
var a = document.createElement("div");
|
||||
a.setAttribute("test-attr", "attr-value")
|
||||
a.appendChild(document.createTextNode('test'));
|
||||
|
||||
var b = document.createElement("div");
|
||||
b.setAttribute("test-attr", "attr-value")
|
||||
b.appendChild(document.createTextNode('test'));
|
||||
|
||||
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
||||
});
|
||||
|
||||
it("fails for DOM nodes with different attributes or child nodes", function() {
|
||||
if (typeof document === 'undefined') {
|
||||
return;
|
||||
}
|
||||
var a = document.createElement("div");
|
||||
a.setAttribute("test-attr", "attr-value")
|
||||
a.appendChild(document.createTextNode('test'));
|
||||
|
||||
var b = document.createElement("div");
|
||||
b.setAttribute("test-attr", "attr-value2")
|
||||
b.appendChild(document.createTextNode('test'));
|
||||
|
||||
expect(j$.matchersUtil.equals(a,b)).toBe(false);
|
||||
|
||||
b.setAttribute("test-attr", "attr-value");
|
||||
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
||||
|
||||
b.appendChild(document.createTextNode('2'));
|
||||
expect(j$.matchersUtil.equals(a,b)).toBe(false);
|
||||
|
||||
a.appendChild(document.createTextNode('2'));
|
||||
expect(j$.matchersUtil.equals(a,b)).toBe(true);
|
||||
});
|
||||
|
||||
it("passes when Any is used", function() {
|
||||
var number = 3,
|
||||
anyNumber = new j$.Any(Number);
|
||||
|
||||
@@ -116,6 +116,31 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
||||
a.ignoreCase == b.ignoreCase;
|
||||
}
|
||||
if (typeof a != 'object' || typeof b != 'object') { return false; }
|
||||
// we don't need to compare DOM nodes in node.js
|
||||
var globalObject = typeof window == 'undefined' ? global : window;
|
||||
if (globalObject.Node && globalObject.HTMLElement) {
|
||||
var aIsDomNode = a instanceof Node;
|
||||
var bIsDomNode = b instanceof Node;
|
||||
if (aIsDomNode && bIsDomNode) {
|
||||
// At first try to use DOM3 method isEqualNode
|
||||
if (a.isEqualNode) {
|
||||
return a.isEqualNode(b);
|
||||
}
|
||||
// IE8 doesn't support isEqualNode, try to use outerHTML && innerText
|
||||
var aIsElement = a instanceof HTMLElement;
|
||||
var bIsElement = b instanceof HTMLElement;
|
||||
if (aIsElement && bIsElement) {
|
||||
return a.outerHTML == b.outerHTML;
|
||||
}
|
||||
if (aIsElement || bIsElement) {
|
||||
return false;
|
||||
}
|
||||
return a.innerText == b.innerText && a.textContent == b.textContent;
|
||||
}
|
||||
if (aIsDomNode || bIsDomNode) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
||||
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
||||
var length = aStack.length;
|
||||
|
||||
Reference in New Issue
Block a user