diff --git a/src/__tests__/to-have-display-value.js b/src/__tests__/to-have-display-value.js index 8cae8734..90802706 100644 --- a/src/__tests__/to-have-display-value.js +++ b/src/__tests__/to-have-display-value.js @@ -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...'), diff --git a/src/to-have-display-value.js b/src/to-have-display-value.js index bdfd2c78..30b260c3 100644 --- a/src/to-have-display-value.js +++ b/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) @@ -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 = @@ -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 -}