Skip to content
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

fix: check equality in toHaveDisplayValue (fix #290) #319

Merged
merged 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/__tests__/to-have-display-value.js
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)
})
19 changes: 8 additions & 11 deletions src/to-have-display-value.js
@@ -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
}