Better detection of DOM Nodes for equality

- Also use JSDom if a real one isn't present to get some more coverage
  there

- Fixes #1172
This commit is contained in:
Gregg Van Hove
2018-05-04 18:01:08 -07:00
parent f7097281c9
commit ced2b114e4
5 changed files with 75 additions and 37 deletions

View File

@@ -241,7 +241,14 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
};
j$.isDomNode = function(obj) {
return obj.nodeType > 0;
// Node is a function, because constructors
return typeof jasmineGlobal.Node !== 'undefined' ?
obj instanceof jasmineGlobal.Node :
obj !== null &&
typeof obj === 'object' &&
typeof obj.nodeType === 'number' &&
typeof obj.nodeName === 'string';
// return obj.nodeType > 0;
};
j$.isMap = function(obj) {
@@ -4657,8 +4664,8 @@ getJasmineRequireObj().pp = function(j$) {
if (el.innerHTML === '') {
this.append(el.outerHTML.replace(closingTag, ''));
} else {
var tagEnd = el.outerHTML.indexOf(el.innerHTML);
this.append(el.outerHTML.substring(0, tagEnd));
var tagEnd = el.outerHTML.indexOf('>');
this.append(el.outerHTML.substring(0, tagEnd + 1));
this.append('...' + closingTag);
}
};

View File

@@ -570,50 +570,67 @@ describe("toEqual", function() {
expect(compareEquals(actual, expected).pass).toBe(true);
});
function isNotRunningInBrowser() {
return typeof document === 'undefined'
}
it("reports mismatches between DOM nodes with different tags", function() {
if(isNotRunningInBrowser()) {
return;
describe('DOM nodes', function() {
function isNotRunningInBrowser() {
return typeof document === 'undefined'
}
var actual = {a: document.createElement('div')},
expected = {a: document.createElement('p')},
beforeEach(function(done) {
this.nonBrowser = isNotRunningInBrowser();
if (this.nonBrowser) {
var jsdom = require('jsdom');
var self = this;
jsdom.env('', function(err, win) {
if (err) {
done.fail(err);
} else {
jasmineUnderTest.getGlobal().Node = win.Node;
self.doc = win.document;
done();
}
});
} else {
this.doc = document;
done();
}
});
afterEach(function() {
if (this.nonBrowser) {
delete jasmineUnderTest.getGlobal().Node;
}
});
it("reports mismatches between DOM nodes with different tags", function() {
var actual = {a: this.doc.createElement('div')},
expected = {a: this.doc.createElement('p')},
message = 'Expected $.a = <div> to equal <p>.';
expect(compareEquals(actual, expected).message).toEqual(message);
});
expect(compareEquals(actual, expected).message).toEqual(message);
});
it('reports mismatches between DOM nodes with different content', function() {
if(isNotRunningInBrowser()) {
return;
}
it('reports mismatches between DOM nodes with different content', function() {
var nodeA = this.doc.createElement('div'),
nodeB = this.doc.createElement('div');
var nodeA = document.createElement('div'),
nodeB = document.createElement('div');
nodeA.setAttribute('thing', 'foo');
nodeB.setAttribute('thing', 'bar');
nodeA.innerText = 'foo';
nodeB.innerText = 'bar';
var actual = {a: nodeA},
expect(nodeA.isEqualNode(nodeB)).toBe(false);
var actual = {a: nodeA},
expected = {a: nodeB},
message = 'Expected $.a = <div>...</div> to equal <div>...</div>.';
message = 'Expected $.a = <div thing="foo"> to equal <div thing="bar">.';
expect(compareEquals(actual, expected).message).toEqual(message);
});
expect(compareEquals(actual, expected).message).toEqual(message);
});
it("reports mismatches between a DOM node and a bare Object", function() {
if(isNotRunningInBrowser()) {
return;
}
var actual = {a: document.createElement('div')},
it("reports mismatches between a DOM node and a bare Object", function() {
var actual = {a: this.doc.createElement('div')},
expected = {a: {}},
message = 'Expected $.a = <div> to equal Object({ }).';
expect(compareEquals(actual, expected).message).toEqual(message);
expect(compareEquals(actual, expected).message).toEqual(message);
});
});
it("reports asymmetric mismatches", function() {

View File

@@ -24,4 +24,11 @@ describe("jasmineUnderTest.pp (HTML Dependent)", function () {
expect(jasmineUnderTest.pp(err)).toMatch(/Not enough arguments/);
}
});
it("should stringify HTML element with text and attributes", function() {
var el = document.createElement('div');
el.setAttribute('things', 'foo');
el.innerHTML = 'foo';
expect(jasmineUnderTest.pp(el)).toEqual('<div things="foo">...</div>');
});
});

View File

@@ -233,8 +233,8 @@ getJasmineRequireObj().pp = function(j$) {
if (el.innerHTML === '') {
this.append(el.outerHTML.replace(closingTag, ''));
} else {
var tagEnd = el.outerHTML.indexOf(el.innerHTML);
this.append(el.outerHTML.substring(0, tagEnd));
var tagEnd = el.outerHTML.indexOf('>');
this.append(el.outerHTML.substring(0, tagEnd + 1));
this.append('...' + closingTag);
}
};

View File

@@ -101,7 +101,14 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
};
j$.isDomNode = function(obj) {
return obj.nodeType > 0;
// Node is a function, because constructors
return typeof jasmineGlobal.Node !== 'undefined' ?
obj instanceof jasmineGlobal.Node :
obj !== null &&
typeof obj === 'object' &&
typeof obj.nodeType === 'number' &&
typeof obj.nodeName === 'string';
// return obj.nodeType > 0;
};
j$.isMap = function(obj) {