Skip to content

Commit a6a7a6f

Browse files
committed
fix: normalize expected value in toContainHTML
resolves #347
1 parent 84fe8e0 commit a6a7a6f

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/__tests__/to-contain-html.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,18 @@ 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 incorrectStringHtml = '<span data-testid="child"></div>'
21+
const stringChildElementSelfClosing = '<span data-testid="child" />'
2222
const nonExistantString = '<span> Does not exists </span>'
2323
const svgElement = queryByTestId('svg-element')
2424

2525
expect(grandparent).toContainHTML(stringChildElement)
2626
expect(parent).toContainHTML(stringChildElement)
2727
expect(child).toContainHTML(stringChildElement)
28+
expect(child).toContainHTML(stringChildElementSelfClosing)
2829
expect(grandparent).not.toContainHTML(nonExistantString)
2930
expect(parent).not.toContainHTML(nonExistantString)
3031
expect(child).not.toContainHTML(nonExistantString)
3132
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)
3633

3734
// negative test cases wrapped in throwError assertions for coverage.
3835
expect(() =>
@@ -59,6 +56,9 @@ describe('.toContainHTML', () => {
5956
expect(() =>
6057
expect(child).not.toContainHTML(stringChildElement),
6158
).toThrowError()
59+
expect(() =>
60+
expect(child).not.toContainHTML(stringChildElementSelfClosing),
61+
).toThrowError()
6262
expect(() => expect(child).toContainHTML(nonExistantString)).toThrowError()
6363
expect(() => expect(parent).toContainHTML(nonExistantString)).toThrowError()
6464
expect(() =>
@@ -71,18 +71,6 @@ describe('.toContainHTML', () => {
7171
expect(() =>
7272
expect(grandparent).toContainHTML(nonExistantElement),
7373
).toThrowError()
74-
expect(() =>
75-
expect(nonExistantElement).toContainHTML(incorrectStringHtml),
76-
).toThrowError()
77-
expect(() =>
78-
expect(grandparent).toContainHTML(incorrectStringHtml),
79-
).toThrowError()
80-
expect(() =>
81-
expect(child).toContainHTML(incorrectStringHtml),
82-
).toThrowError()
83-
expect(() =>
84-
expect(parent).toContainHTML(incorrectStringHtml),
85-
).toThrowError()
8674
})
8775

8876
test('throws with an expected text', () => {

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)