Skip to content

Commit 21ad89b

Browse files
authored
fix: normalize expected value in toContainHTML (#349)
resolves #347
1 parent 84fe8e0 commit 21ad89b

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/__tests__/to-contain-html.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ describe('.toContainHTML', () => {
1818
const nonExistantElement = queryByTestId('not-exists')
1919
const fakeElement = {thisIsNot: 'an html element'}
2020
const stringChildElement = '<span data-testid="child"></span>'
21+
const stringChildElementSelfClosing = '<span data-testid="child" />'
2122
const incorrectStringHtml = '<span data-testid="child"></div>'
2223
const nonExistantString = '<span> Does not exists </span>'
2324
const svgElement = queryByTestId('svg-element')
2425

2526
expect(grandparent).toContainHTML(stringChildElement)
2627
expect(parent).toContainHTML(stringChildElement)
2728
expect(child).toContainHTML(stringChildElement)
29+
expect(child).toContainHTML(stringChildElementSelfClosing)
2830
expect(grandparent).not.toContainHTML(nonExistantString)
2931
expect(parent).not.toContainHTML(nonExistantString)
3032
expect(child).not.toContainHTML(nonExistantString)
3133
expect(child).not.toContainHTML(nonExistantString)
32-
expect(grandparent).not.toContainHTML(incorrectStringHtml)
33-
expect(parent).not.toContainHTML(incorrectStringHtml)
34-
expect(child).not.toContainHTML(incorrectStringHtml)
35-
expect(child).not.toContainHTML(incorrectStringHtml)
34+
expect(grandparent).toContainHTML(incorrectStringHtml)
35+
expect(parent).toContainHTML(incorrectStringHtml)
36+
expect(child).toContainHTML(incorrectStringHtml)
3637

3738
// negative test cases wrapped in throwError assertions for coverage.
3839
expect(() =>
@@ -59,6 +60,9 @@ describe('.toContainHTML', () => {
5960
expect(() =>
6061
expect(child).not.toContainHTML(stringChildElement),
6162
).toThrowError()
63+
expect(() =>
64+
expect(child).not.toContainHTML(stringChildElementSelfClosing),
65+
).toThrowError()
6266
expect(() => expect(child).toContainHTML(nonExistantString)).toThrowError()
6367
expect(() => expect(parent).toContainHTML(nonExistantString)).toThrowError()
6468
expect(() =>
@@ -72,16 +76,16 @@ describe('.toContainHTML', () => {
7276
expect(grandparent).toContainHTML(nonExistantElement),
7377
).toThrowError()
7478
expect(() =>
75-
expect(nonExistantElement).toContainHTML(incorrectStringHtml),
79+
expect(nonExistantElement).not.toContainHTML(incorrectStringHtml),
7680
).toThrowError()
7781
expect(() =>
78-
expect(grandparent).toContainHTML(incorrectStringHtml),
82+
expect(grandparent).not.toContainHTML(incorrectStringHtml),
7983
).toThrowError()
8084
expect(() =>
81-
expect(child).toContainHTML(incorrectStringHtml),
85+
expect(child).not.toContainHTML(incorrectStringHtml),
8286
).toThrowError()
8387
expect(() =>
84-
expect(parent).toContainHTML(incorrectStringHtml),
88+
expect(parent).not.toContainHTML(incorrectStringHtml),
8589
).toThrowError()
8690
})
8791

src/to-contain-html.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import {checkHtmlElement} from './utils'
22

3+
function getNormalizedHtml(container, htmlText) {
4+
const div = container.ownerDocument.createElement('div')
5+
div.innerHTML = htmlText
6+
return div.innerHTML
7+
}
8+
39
export function toContainHTML(container, htmlText) {
410
checkHtmlElement(container, toContainHTML, this)
511

12+
if (typeof htmlText !== 'string') {
13+
throw new Error(`.toContainHTML() expects a string value, got ${htmlText}`)
14+
}
15+
616
return {
7-
pass: container.outerHTML.includes(htmlText),
17+
pass: container.outerHTML.includes(getNormalizedHtml(container, htmlText)),
818
message: () => {
919
return [
1020
this.utils.matcherHint(

0 commit comments

Comments
 (0)