Skip to content

Commit

Permalink
Merge branch 'master' into only-contains-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gnapse authored Jan 12, 2021
2 parents f352858 + f930668 commit 559bc43
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 136 deletions.
20 changes: 20 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,26 @@
"contributions": [
"code"
]
},
{
"login": "nickmccurdy",
"name": "Nick McCurdy",
"avatar_url": "https://avatars0.githubusercontent.com/u/927220?v=4",
"profile": "https://nickmccurdy.com/",
"contributions": [
"doc",
"bug",
"code"
]
},
{
"login": "obedparla",
"name": "Obed Marquez Parlapiano",
"avatar_url": "https://avatars1.githubusercontent.com/u/10674462?v=4",
"profile": "https://obedparla.com/",
"contributions": [
"doc"
]
}
],
"repoHost": "https://github.com",
Expand Down
139 changes: 80 additions & 59 deletions README.md

Large diffs are not rendered by default.

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\\" /></>"
`)
})
})
11 changes: 11 additions & 0 deletions src/__tests__/to-have-display-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test('it should work as expected', () => {
`)

expect(queryByTestId('select')).toHaveDisplayValue('Select a fruit...')
expect(queryByTestId('select')).not.toHaveDisplayValue('Select')
expect(queryByTestId('select')).not.toHaveDisplayValue('Banana')
expect(() =>
expect(queryByTestId('select')).not.toHaveDisplayValue('Select a fruit...'),
Expand Down Expand Up @@ -151,3 +152,13 @@ test('it should throw if element is not valid', () => {
`".toHaveDisplayValue() currently does not support input[type=\\"checkbox\\"], try with another matcher instead."`,
)
})

test('it should work with numbers', () => {
const {queryByTestId} = render(`
<select data-testid="select">
<option value="">1</option>
</select>
`)

expect(queryByTestId('select')).toHaveDisplayValue(1)
})
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
19 changes: 8 additions & 11 deletions src/to-have-display-value.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {matches, checkHtmlElement, getMessage} from './utils'
import {checkHtmlElement, getMessage} from './utils'

export function toHaveDisplayValue(htmlElement, expectedValue) {
checkHtmlElement(htmlElement, toHaveDisplayValue, this)
Expand All @@ -18,10 +18,13 @@ export function toHaveDisplayValue(htmlElement, expectedValue) {

const values = getValues(tagName, htmlElement)
const expectedValues = getExpectedValues(expectedValue)
const numberOfMatchesWithValues = getNumberOfMatchesBetweenArrays(
values,
expectedValues,
)
const numberOfMatchesWithValues = expectedValues.filter(expected =>
values.some(value =>
expected instanceof RegExp
? expected.test(value)
: this.equals(value, String(expected)),
),
).length

const matchedWithAllValues = numberOfMatchesWithValues === values.length
const matchedWithAllExpectedValues =
Expand Down Expand Up @@ -56,9 +59,3 @@ function getValues(tagName, htmlElement) {
function getExpectedValues(expectedValue) {
return expectedValue instanceof Array ? expectedValue : [expectedValue]
}

function getNumberOfMatchesBetweenArrays(arrayBase, array) {
return array.filter(
expected => arrayBase.filter(value => matches(value, expected)).length,
).length
}

0 comments on commit 559bc43

Please sign in to comment.