From 25bd2b40870fa559728150b1abf08495324ec69e Mon Sep 17 00:00:00 2001 From: Alex J Burke Date: Sat, 29 Sep 2018 15:37:32 +0200 Subject: [PATCH] Fix comment nodes being specified as children in a satisfy spec. --- lib/index.js | 14 ++++++++++++- test/index.spec.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 04bf728a..470352e0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -742,6 +742,16 @@ module.exports = { } ); + function convertChildrenToSatisfySpec(children, isHtml) { + return children.map(function(child) { + if (expect.findTypeOf(child).is('DOMNode')) { + return convertDOMNodeToSatisfySpec(child, isHtml); + } else { + return child; + } + }); + } + function convertDOMNodeToSatisfySpec(node, isHtml) { if (node.nodeType === 8 && node.nodeValue.trim() === 'ignore') { // Ignore subtree @@ -991,7 +1001,9 @@ module.exports = { subject.ownerDocument.contentType ), 'to satisfy', - value.children + Array.isArray(value.children) + ? convertChildrenToSatisfySpec(value.children, isHtml) + : value.children ); }); } else if (typeof value.textContent !== 'undefined') { diff --git a/test/index.spec.js b/test/index.spec.js index 6e50d14a..952fb197 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -2032,6 +2032,15 @@ describe('unexpected-dom', function() { expect(body.firstChild, 'to satisfy', { children: ['hey'] }); }); + it('should succeed with a node child', function() { + var node = document.createElement('div'); + node.innerHTML = '
hey
'; + body.innerHTML = '
hey
'; + expect(body.firstChild, 'to satisfy', { + children: [node.firstChild] + }); + }); + it('should fail with a diff', function() { body.innerHTML = '
hey
'; expect( @@ -2049,6 +2058,24 @@ describe('unexpected-dom', function() { '' ); }); + + describe('when using ignore', function() { + it('should succeed', function() { + var node = document.createElement('div'); + node.innerHTML = ''; + var commentNode = node.firstChild; + body.innerHTML = + '
ignoreimportant
'; + expect(body.firstChild, 'to satisfy', { + children: [ + commentNode, + { + children: 'important' + } + ] + }); + }); + }); }); it('should fail with a diff', function() { @@ -2625,6 +2652,31 @@ describe('unexpected-dom', function() { }); }); + describe('to satisfy', function() { + describe('when comparing an array of children', function() { + it('should succeed with a text child', function() { + expect( + [ + '', + '', + ' World', + '' + ].join('\n'), + 'when parsed as XML', + 'queried for first', + 'hello', + 'to satisfy', + { + attributes: { + type: 'greeting' + }, + children: ['World'] + } + ); + }); + }); + }); + describe('when the DOMParser global is available', function() { var originalDOMParser, DOMParserSpy, parseFromStringSpy;