Skip to content

fix: Adds expected text for toContainHTML checks that fail (#299) #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 94 additions & 65 deletions src/__tests__/to-contain-html.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {render} from './helpers/test-utils'

/* eslint-disable max-statements */
test('.toContainHTML', () => {
const {queryByTestId} = render(`
describe('.toContainHTML', () => {
test('handles positive and negative cases', () => {
const {queryByTestId} = render(`
<span data-testid="grandparent">
<span data-testid="parent">
<span data-testid="child"></span>
Expand All @@ -11,69 +12,97 @@ test('.toContainHTML', () => {
</span>
`)

const grandparent = queryByTestId('grandparent')
const parent = queryByTestId('parent')
const child = queryByTestId('child')
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 nonExistantString = '<span> Does not exists </span>'
const svgElement = queryByTestId('svg-element')
const grandparent = queryByTestId('grandparent')
const parent = queryByTestId('parent')
const child = queryByTestId('child')
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 nonExistantString = '<span> Does not exists </span>'
const svgElement = queryByTestId('svg-element')

expect(grandparent).toContainHTML(stringChildElement)
expect(parent).toContainHTML(stringChildElement)
expect(child).toContainHTML(stringChildElement)
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)
expect(grandparent).toContainHTML(stringChildElement)
expect(parent).toContainHTML(stringChildElement)
expect(child).toContainHTML(stringChildElement)
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(() =>
expect(nonExistantElement).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(nonExistantElement).not.toContainHTML(nonExistantElement),
).toThrowError()
expect(() =>
expect(stringChildElement).not.toContainHTML(fakeElement),
).toThrowError()
expect(() =>
expect(svgElement).toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(grandparent).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(parent).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() => expect(child).toContainHTML(nonExistantString)).toThrowError()
expect(() => expect(parent).toContainHTML(nonExistantString)).toThrowError()
expect(() =>
expect(grandparent).toContainHTML(nonExistantString),
).toThrowError()
expect(() => expect(child).toContainHTML(nonExistantElement)).toThrowError()
expect(() => expect(parent).toContainHTML(nonExistantElement)).toThrowError()
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()
// negative test cases wrapped in throwError assertions for coverage.
expect(() =>
expect(nonExistantElement).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(nonExistantElement).not.toContainHTML(nonExistantElement),
).toThrowError()
expect(() =>
expect(stringChildElement).not.toContainHTML(fakeElement),
).toThrowError()
expect(() =>
expect(svgElement).toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(grandparent).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(parent).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() => expect(child).toContainHTML(nonExistantString)).toThrowError()
expect(() => expect(parent).toContainHTML(nonExistantString)).toThrowError()
expect(() =>
expect(grandparent).toContainHTML(nonExistantString),
).toThrowError()
expect(() => expect(child).toContainHTML(nonExistantElement)).toThrowError()
expect(() =>
expect(parent).toContainHTML(nonExistantElement),
).toThrowError()
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', () => {
const {queryByTestId} = render('<span data-testid="child"></span>')
const htmlElement = queryByTestId('child')
const nonExistantString = '<div> non-existant element </div>'

let errorMessage
try {
expect(htmlElement).toContainHTML(nonExistantString)
} catch (error) {
errorMessage = error.message
}

expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).toContainHTML()</>
Expected:
<green><div> non-existant element </div></>
Received:
<red><span data-testid=\\"child\\" /></>"
`)
})
})
4 changes: 3 additions & 1 deletion src/to-contain-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export function toContainHTML(container, htmlText) {
'element',
'',
),
'',
'Expected:',
// eslint-disable-next-line babel/new-cap
` ${this.utils.EXPECTED_COLOR(htmlText)}`,
'Received:',
` ${this.utils.printReceived(container.cloneNode(true))}`,
].join('\n')
Expand Down