Skip to content

Commit

Permalink
fix: check equality in toHaveDisplayValue (fix #290)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmccurdy committed Dec 21, 2020
1 parent 2927c95 commit ae4374b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
1 change: 1 addition & 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
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, 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 ae4374b

Please sign in to comment.