Skip to content

Commit

Permalink
Don't allow to contain to match directly on the subject
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Feb 26, 2019
1 parent 9741b3e commit 67ec4d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion documentation/assertions/DOMElement/to-contain.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Assert that an element contains elements satisfying a given specification.
Assert that an element contains descendant elements satisfying a given specification.

```js
var element = createElement(`
Expand Down
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1625,9 +1625,8 @@ module.exports = {
expect.exportAssertion(
'<DOMDocument|DOMElement|DOMDocumentFragment|DOMNodeList> to contain <DOMElement|object|string>',
(expect, subject, value) => {
const isHtml = isInsideHtmlDocument(
typeof subject.length === 'number' ? subject[0] : subject
);
const nodes = subject.childNodes || subject;
const isHtml = isInsideHtmlDocument(nodes[0]);
const valueType = expect.findTypeOf(value);
let spec = value;

Expand All @@ -1652,7 +1651,7 @@ module.exports = {

ensureSupportedSpecOptions(spec);

const scoredElements = findMatchesWithGoodScore(subject, spec);
const scoredElements = findMatchesWithGoodScore(nodes, spec);

if (scoredElements.length === 0) {
expect.subjectOutput = output =>
Expand Down
26 changes: 26 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3147,6 +3147,32 @@ describe('unexpected-dom', () => {
);
});

it('should not match directly on the subject', () => {
expect(
() => {
expect(
parseHtmlNode(
'<span class="greeting"><span>Hello</span><span class="name">Jane Doe</span></span>'
),
'to contain',
'<span><span>Hello</span><!--ignore--></span>'
);
},
'to throw',
'expected\n' +
'<span class="greeting">\n' +
' <span>Hello</span>\n' +
' <span class="name">Jane Doe</span>\n' +
'</span>\n' +
"to contain '<span><span>Hello</span><!--ignore--></span>'\n" +
'\n' +
'<span>\n' +
" // missing { name: 'span', attributes: {}, children: [ 'Hello' ] }\n" +
' Hello\n' +
'</span>'
);
});

it('fails without a diff if no good candidates can be found in the given structure', () => {
expect(
() => {
Expand Down

0 comments on commit 67ec4d3

Please sign in to comment.