From 67ec4d3a29d6907ba84a8bfafbffe80e4f58b349 Mon Sep 17 00:00:00 2001 From: Sune Simonsen Date: Tue, 26 Feb 2019 09:19:15 +0100 Subject: [PATCH] Don't allow to contain to match directly on the subject --- .../assertions/DOMElement/to-contain.md | 2 +- src/index.js | 7 +++-- test/index.spec.js | 26 +++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/documentation/assertions/DOMElement/to-contain.md b/documentation/assertions/DOMElement/to-contain.md index 5d8cdbd3..e2699d15 100644 --- a/documentation/assertions/DOMElement/to-contain.md +++ b/documentation/assertions/DOMElement/to-contain.md @@ -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(` diff --git a/src/index.js b/src/index.js index 9bbfba8f..24218179 100644 --- a/src/index.js +++ b/src/index.js @@ -1625,9 +1625,8 @@ module.exports = { expect.exportAssertion( ' to contain ', (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; @@ -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 => diff --git a/test/index.spec.js b/test/index.spec.js index ac572195..dcb2f691 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -3147,6 +3147,32 @@ describe('unexpected-dom', () => { ); }); + it('should not match directly on the subject', () => { + expect( + () => { + expect( + parseHtmlNode( + 'HelloJane Doe' + ), + 'to contain', + 'Hello' + ); + }, + 'to throw', + 'expected\n' + + '\n' + + ' Hello\n' + + ' Jane Doe\n' + + '\n' + + "to contain 'Hello'\n" + + '\n' + + '\n' + + " // missing { name: 'span', attributes: {}, children: [ 'Hello' ] }\n" + + ' Hello\n' + + '' + ); + }); + it('fails without a diff if no good candidates can be found in the given structure', () => { expect( () => {