Skip to content

Commit

Permalink
Merge 006c7ec into 54d428a
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjeffburke committed Oct 2, 2018
2 parents 54d428a + 006c7ec commit 1b6eff6
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 5 deletions.
33 changes: 29 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ module.exports = {
}
});

// Recognize <!-- ignore --> as a special subtype of DOMComment so it can be targeted by assertions:
expect.exportType({
name: 'DOMIgnoreComment',
base: 'DOMComment',
identify: function(obj) {
return (
this.baseType.identify(obj) && /^\s*ignore\s*$/.test(obj.nodeValue)
);
}
});

expect.exportType({
name: 'DOMTextNode',
base: 'DOMNode',
Expand Down Expand Up @@ -719,6 +730,13 @@ module.exports = {
}
);

expect.exportAssertion(
'<DOMComment> to [exhaustively] satisfy <DOMComment>',
function(expect, subject, value) {
return expect(subject.nodeValue, 'to equal', value.nodeValue);
}
);

// Avoid rendering a huge object diff when a text node is matched against a different node type:
expect.exportAssertion(
'<DOMTextNode> to [exhaustively] satisfy <object>',
Expand All @@ -727,6 +745,13 @@ module.exports = {
}
);

// Always passes:
expect.exportAssertion(
// Name each subject type to increase the specificity of the assertion
'<DOMComment|DOMElement|DOMTextNode|DOMDocument|HTMLDocType> to [exhaustively] satisfy <DOMIgnoreComment>',
function(expect, subject, value) {}
);

// Necessary because this case would otherwise be handled by the above catch-all for <object>:
expect.exportAssertion(
'<DOMTextNode> to [exhaustively] satisfy <regexp>',
Expand All @@ -743,10 +768,7 @@ module.exports = {
);

function convertDOMNodeToSatisfySpec(node, isHtml) {
if (node.nodeType === 8 && node.nodeValue.trim() === 'ignore') {
// Ignore subtree
return expect.it('to be an', 'DOMNode');
} else if (node.nodeType === 10) {
if (node.nodeType === 10) {
// HTMLDocType
return { name: node.nodeName };
} else if (node.nodeType === 1) {
Expand Down Expand Up @@ -778,6 +800,9 @@ module.exports = {
} else if (node.nodeType === 3) {
// DOMTextNode
return node.nodeValue;
} else if (node.nodeType === 8) {
// DOMComment
return node;
} else {
throw new Error(
'to satisfy: Node type ' +
Expand Down
94 changes: 93 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ function parseHtml(str) {
.window.document.body.firstChild;
}

function parseHtmlDocument(str) {
return new jsdom.JSDOM(str).window.document;
}

function parseHtmlFragment(str) {
str = '<html><head></head><body>' + str + '</body></html>';
var htmlDocument = new jsdom.JSDOM(str).window.document;
Expand All @@ -76,6 +80,10 @@ function parseHtmlFragment(str) {
return documentFragment;
}

function parseHtmlNode(str) {
return parseHtmlFragment(str).childNodes[0];
}

function parseXml(str) {
if (typeof DOMParser !== 'undefined') {
// eslint-disable-next-line no-undef
Expand Down Expand Up @@ -1468,7 +1476,7 @@ describe('unexpected-dom', function() {
);
});

describe('and it contain an ignore comment', function() {
describe('and it contains an ignore comment', function() {
it('ignores the corresponding subtree', () => {
expect(
[
Expand Down Expand Up @@ -2032,6 +2040,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 +2066,56 @@ 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'
}
]
});
});
});
});

describe('when matching against <!-- ignore -->', function() {
var ignoreComment = parseHtmlNode('<!--ignore-->');

it('should match a text node', function() {
expect(parseHtmlNode('foo'), 'to satisfy', ignoreComment);
});

it('should match an element', function() {
expect(parseHtmlNode('<div>foo</div>'), 'to satisfy', ignoreComment);
});

it('should match a comment', function() {
expect(parseHtmlNode('<!-- foo -->'), 'to satisfy', ignoreComment);
});

it('should match a doctype', function() {
expect(
parseHtmlDocument('<!DOCTYPE html>').firstChild,
'to satisfy',
ignoreComment
);
});

it('should match a document', function() {
expect(
'<?xml version="1.0"?><fooBar>abc<source></source></fooBar>',
'when parsed as xml to satisfy',
ignoreComment
);
});
});

it('should fail with a diff', function() {
Expand Down Expand Up @@ -2625,6 +2692,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 1b6eff6

Please sign in to comment.