Skip to content

Commit

Permalink
fix: normalize expected value in toContainHTML
Browse files Browse the repository at this point in the history
resolves #347
  • Loading branch information
just-boris committed Mar 19, 2021
1 parent 84fe8e0 commit a6a7a6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
22 changes: 5 additions & 17 deletions src/__tests__/to-contain-html.js
Expand Up @@ -18,21 +18,18 @@ describe('.toContainHTML', () => {
const nonExistantElement = queryByTestId('not-exists')
const fakeElement = {thisIsNot: 'an html element'}
const stringChildElement = '<span data-testid="child"></span>'
const incorrectStringHtml = '<span data-testid="child"></div>'
const stringChildElementSelfClosing = '<span data-testid="child" />'
const nonExistantString = '<span> Does not exists </span>'
const svgElement = queryByTestId('svg-element')

expect(grandparent).toContainHTML(stringChildElement)
expect(parent).toContainHTML(stringChildElement)
expect(child).toContainHTML(stringChildElement)
expect(child).toContainHTML(stringChildElementSelfClosing)
expect(grandparent).not.toContainHTML(nonExistantString)
expect(parent).not.toContainHTML(nonExistantString)
expect(child).not.toContainHTML(nonExistantString)
expect(child).not.toContainHTML(nonExistantString)
expect(grandparent).not.toContainHTML(incorrectStringHtml)
expect(parent).not.toContainHTML(incorrectStringHtml)
expect(child).not.toContainHTML(incorrectStringHtml)
expect(child).not.toContainHTML(incorrectStringHtml)

// negative test cases wrapped in throwError assertions for coverage.
expect(() =>
Expand All @@ -59,6 +56,9 @@ describe('.toContainHTML', () => {
expect(() =>
expect(child).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(stringChildElementSelfClosing),
).toThrowError()
expect(() => expect(child).toContainHTML(nonExistantString)).toThrowError()
expect(() => expect(parent).toContainHTML(nonExistantString)).toThrowError()
expect(() =>
Expand All @@ -71,18 +71,6 @@ describe('.toContainHTML', () => {
expect(() =>
expect(grandparent).toContainHTML(nonExistantElement),
).toThrowError()
expect(() =>
expect(nonExistantElement).toContainHTML(incorrectStringHtml),
).toThrowError()
expect(() =>
expect(grandparent).toContainHTML(incorrectStringHtml),
).toThrowError()
expect(() =>
expect(child).toContainHTML(incorrectStringHtml),
).toThrowError()
expect(() =>
expect(parent).toContainHTML(incorrectStringHtml),
).toThrowError()
})

test('throws with an expected text', () => {
Expand Down
12 changes: 11 additions & 1 deletion src/to-contain-html.js
@@ -1,10 +1,20 @@
import {checkHtmlElement} from './utils'

function getNormalizedHtml(container, htmlText) {
const div = container.ownerDocument.createElement('div')
div.innerHTML = htmlText
return div.innerHTML
}

export function toContainHTML(container, htmlText) {
checkHtmlElement(container, toContainHTML, this)

if (typeof htmlText !== 'string') {
throw new Error(`.toContainHTML() expects a string value, got ${htmlText}`)
}

return {
pass: container.outerHTML.includes(htmlText),
pass: container.outerHTML.includes(getNormalizedHtml(container, htmlText)),
message: () => {
return [
this.utils.matcherHint(
Expand Down

0 comments on commit a6a7a6f

Please sign in to comment.