Skip to content

Commit b671eff

Browse files
authored
Merge branch 'master' into pr/contain-html
2 parents 39da976 + 4179117 commit b671eff

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/__tests__/to-have-display-value.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ test('it should work as expected', () => {
1111
`)
1212

1313
expect(queryByTestId('select')).toHaveDisplayValue('Select a fruit...')
14+
expect(queryByTestId('select')).not.toHaveDisplayValue('Select')
1415
expect(queryByTestId('select')).not.toHaveDisplayValue('Banana')
1516
expect(() =>
1617
expect(queryByTestId('select')).not.toHaveDisplayValue('Select a fruit...'),
@@ -151,3 +152,13 @@ test('it should throw if element is not valid', () => {
151152
`".toHaveDisplayValue() currently does not support input[type=\\"checkbox\\"], try with another matcher instead."`,
152153
)
153154
})
155+
156+
test('it should work with numbers', () => {
157+
const {queryByTestId} = render(`
158+
<select data-testid="select">
159+
<option value="">1</option>
160+
</select>
161+
`)
162+
163+
expect(queryByTestId('select')).toHaveDisplayValue(1)
164+
})

src/to-have-display-value.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {matches, checkHtmlElement, getMessage} from './utils'
1+
import {checkHtmlElement, getMessage} from './utils'
22

33
export function toHaveDisplayValue(htmlElement, expectedValue) {
44
checkHtmlElement(htmlElement, toHaveDisplayValue, this)
@@ -18,10 +18,13 @@ export function toHaveDisplayValue(htmlElement, expectedValue) {
1818

1919
const values = getValues(tagName, htmlElement)
2020
const expectedValues = getExpectedValues(expectedValue)
21-
const numberOfMatchesWithValues = getNumberOfMatchesBetweenArrays(
22-
values,
23-
expectedValues,
24-
)
21+
const numberOfMatchesWithValues = expectedValues.filter(expected =>
22+
values.some(value =>
23+
expected instanceof RegExp
24+
? expected.test(value)
25+
: this.equals(value, String(expected)),
26+
),
27+
).length
2528

2629
const matchedWithAllValues = numberOfMatchesWithValues === values.length
2730
const matchedWithAllExpectedValues =
@@ -56,9 +59,3 @@ function getValues(tagName, htmlElement) {
5659
function getExpectedValues(expectedValue) {
5760
return expectedValue instanceof Array ? expectedValue : [expectedValue]
5861
}
59-
60-
function getNumberOfMatchesBetweenArrays(arrayBase, array) {
61-
return array.filter(
62-
expected => arrayBase.filter(value => matches(value, expected)).length,
63-
).length
64-
}

0 commit comments

Comments
 (0)