diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 3905a5d7..eec02f48 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -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); } }; diff --git a/spec/core/matchers/toEqualSpec.js b/spec/core/matchers/toEqualSpec.js index 55c113d6..374e003a 100644 --- a/spec/core/matchers/toEqualSpec.js +++ b/spec/core/matchers/toEqualSpec.js @@ -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 =
to equal

.'; - 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 =

...
to equal
...
.'; + message = 'Expected $.a =
to equal
.'; - 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 =
to equal Object({ }).'; - expect(compareEquals(actual, expected).message).toEqual(message); + expect(compareEquals(actual, expected).message).toEqual(message); + }); }); it("reports asymmetric mismatches", function() { diff --git a/spec/html/PrettyPrintHtmlSpec.js b/spec/html/PrettyPrintHtmlSpec.js index caad7513..4ff969b3 100644 --- a/spec/html/PrettyPrintHtmlSpec.js +++ b/spec/html/PrettyPrintHtmlSpec.js @@ -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('
...
'); + }); }); diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index 0f289b0a..a5abcdd6 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -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); } }; diff --git a/src/core/base.js b/src/core/base.js index d01e6bfe..2c29d4da 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -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) {