Skip to content

Commit

Permalink
Fix comment nodes being specified as children in a satisfy spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjeffburke committed Sep 29, 2018
1 parent 54d428a commit 25bd2b4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') {
Expand Down
52 changes: 52 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div foo="bar">hey</div>';
body.innerHTML = '<div><div foo="bar">hey</div></div>';
expect(body.firstChild, 'to satisfy', {
children: [node.firstChild]
});
});

it('should fail with a diff', function() {
body.innerHTML = '<div foo="bar">hey</div>';
expect(
Expand All @@ -2049,6 +2058,24 @@ describe('unexpected-dom', function() {
'</div>'
);
});

describe('when using ignore', function() {
it('should succeed', function() {
var node = document.createElement('div');
node.innerHTML = '<!-- ignore -->';
var commentNode = node.firstChild;
body.innerHTML =
'<div><span>ignore</span><span>important</span></div>';
expect(body.firstChild, 'to satisfy', {
children: [
commentNode,
{
children: 'important'
}
]
});
});
});
});

it('should fail with a diff', function() {
Expand Down Expand Up @@ -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(
[
'<?xml version="1.0"?>',
'<content>',
' <hello type="greeting">World</hello>',
'</content>'
].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;

Expand Down

0 comments on commit 25bd2b4

Please sign in to comment.