Skip to content

Commit

Permalink
Added workaround for unfixed jsdom bug jsdom/jsdom#1107
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Apr 27, 2015
1 parent 9ccd31d commit 83f719a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ module.exports = {
name: 'DOMComment',
base: 'DOMNode',
identify: function (obj) {
return obj && obj.nodeType === 8;
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 8;
},
equal: function (a, b) {
return a.nodeValue === b.nodeValue;
Expand All @@ -158,7 +158,7 @@ module.exports = {
name: 'DOMTextNode',
base: 'DOMNode',
identify: function (obj) {
return obj && obj.nodeType === 3;
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 3;
},
equal: function (a, b) {
return a.nodeValue.trim() === b.nodeValue.trim();
Expand Down Expand Up @@ -200,7 +200,7 @@ module.exports = {
name: 'HTMLDocType',
base: 'DOMNode',
identify: function (obj) {
return obj && obj.nodeType === 10 && 'publicId' in obj;
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 10 && 'publicId' in obj;
},
inspect: function (doctype, depth, output, inspect) {
output.code('<!DOCTYPE ' + doctype.name + '>', 'html');
Expand All @@ -219,7 +219,7 @@ module.exports = {
name: 'HTMLDocument',
base: 'DOMNode',
identify: function (obj) {
return obj && obj.nodeType === 9 && obj.documentElement && obj.implementation;
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 9 && obj.documentElement && obj.implementation;
},
inspect: function (document, depth, output, inspect) {
for (var i = 0 ; i < document.childNodes.length ; i += 1) {
Expand All @@ -240,7 +240,7 @@ module.exports = {
name: 'HTMLElement',
base: 'DOMNode',
identify: function (obj) {
return obj && obj.nodeType === 1 && obj.nodeName && obj.attributes && obj.outerHTML;
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 1 && obj.nodeName && obj.attributes;
},
equal: function (a, b, equal) {
return a.nodeName.toLowerCase() === b.nodeName.toLowerCase() && equal(getAttributes(a), getAttributes(b)) && equal(a.childNodes, b.childNodes);
Expand Down
31 changes: 31 additions & 0 deletions test/jsdom-compatibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*global describe, it*/
var unexpected = require('unexpected'),
unexpectedDom = require('../lib/index'),
jsdom = require('jsdom');

var expect = unexpected.clone().installPlugin(unexpectedDom);
expect.output.installPlugin(require('magicpen-prism'));

expect.addAssertion('to inspect as [itself]', function (expect, subject, value) {
var originalSubject = subject;
if (typeof subject === 'string') {
subject = jsdom.jsdom('<!DOCTYPE html><html><head></head><body>' + subject + '</body></html>').body.firstChild;
}
if (this.flags.itself) {
if (typeof originalSubject === 'string') {
expect(expect.inspect(subject).toString(), 'to equal', originalSubject);
} else {
throw new Error('subject must be given as a string when expected to inspect as itself');
}
} else {
expect(expect.inspect(subject).toString(), 'to equal', value);
}
});

describe('jsdom bug compatibility', function () {
it('should work without issue #1107 fixed', function() {
// https://github.com/tmpvar/jsdom/issues/1107
expect('<select><option value="foo">bar</option></select>', 'to inspect as itself');
expect('<form><p>foo</p></form>', 'to inspect as itself');
});
});

0 comments on commit 83f719a

Please sign in to comment.