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
feat: Add toHaveDisplayValue()
matcher
#223
Merged
gnapse
merged 13 commits into
testing-library:master
from
cloud-walker:to-have-display-value
Apr 9, 2020
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
39bbc42
Introduce .toHaveDisplay() custom matcher
cloud-walker 4b9439d
Merge branch 'master' into to-have-display-value
cloud-walker 9be2735
Improve docs by showing getByLabelText usage
cloud-walker 4d870ce
Merge branch 'master' into to-have-display-value
cloud-walker ceb4214
Apply suggestions from code review
cloud-walker 8ba75ee
Add input and textarea examples to readme
cloud-walker 8fc06d7
Add support for input and textarea elements
cloud-walker 2836b14
Merge branch 'master' into to-have-display-value
cloud-walker f69457f
Update README.md
cloud-walker 53edd0a
Update README.md
cloud-walker 3412047
Update README.md
cloud-walker 80534cf
Update docs description to match the new elements supported
cloud-walker 5e37a9e
Add value change expects for input, select and textarea
cloud-walker File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import {render} from './helpers/test-utils' | ||
|
||
test('it should work as expected', () => { | ||
const {queryByTestId} = render(` | ||
<select id="fruits" data-testid="select"> | ||
<option value="">Select a fruit...</option> | ||
<option value="ananas">Ananas</option> | ||
<option value="banana">Banana</option> | ||
<option value="avocado">Avocado</option> | ||
</select> | ||
`) | ||
|
||
expect(queryByTestId('select')).toHaveDisplayValue('Select a fruit...') | ||
expect(queryByTestId('select')).not.toHaveDisplayValue('Banana') | ||
expect(() => | ||
expect(queryByTestId('select')).not.toHaveDisplayValue('Select a fruit...'), | ||
).toThrow() | ||
expect(() => | ||
expect(queryByTestId('select')).toHaveDisplayValue('Ananas'), | ||
).toThrow() | ||
|
||
queryByTestId('select').value = 'banana' | ||
expect(queryByTestId('select')).toHaveDisplayValue('Banana') | ||
}) | ||
|
||
test('it should work with select multiple', () => { | ||
const {queryByTestId} = render(` | ||
<select id="fruits" data-testid="select" multiple> | ||
<option value="">Select a fruit...</option> | ||
<option value="ananas" selected>Ananas</option> | ||
<option value="banana">Banana</option> | ||
<option value="avocado" selected>Avocado</option> | ||
</select> | ||
`) | ||
|
||
expect(queryByTestId('select')).toHaveDisplayValue(['Ananas', 'Avocado']) | ||
expect(() => | ||
expect(queryByTestId('select')).not.toHaveDisplayValue([ | ||
'Ananas', | ||
'Avocado', | ||
]), | ||
).toThrow() | ||
|
||
expect(queryByTestId('select')).not.toHaveDisplayValue('Ananas') | ||
expect(() => | ||
expect(queryByTestId('select')).toHaveDisplayValue('Ananas'), | ||
).toThrow() | ||
|
||
Array.from(queryByTestId('select').options).forEach(option => { | ||
option.selected = ['ananas', 'banana'].includes(option.value) | ||
}) | ||
|
||
expect(queryByTestId('select')).toHaveDisplayValue(['Ananas', 'Banana']) | ||
}) | ||
|
||
test('it should work with input elements', () => { | ||
const {queryByTestId} = render(` | ||
<input type="text" data-testid="input" value="Luca" /> | ||
`) | ||
|
||
expect(queryByTestId('input')).toHaveDisplayValue('Luca') | ||
|
||
queryByTestId('input').value = 'Piero' | ||
expect(queryByTestId('input')).toHaveDisplayValue('Piero') | ||
}) | ||
|
||
test('it should work with textarea elements', () => { | ||
const {queryByTestId} = render( | ||
'<textarea data-testid="textarea-example">An example description here.</textarea>', | ||
) | ||
|
||
expect(queryByTestId('textarea-example')).toHaveDisplayValue( | ||
'An example description here.', | ||
) | ||
|
||
queryByTestId('textarea-example').value = 'Another example' | ||
expect(queryByTestId('textarea-example')).toHaveDisplayValue( | ||
'Another example', | ||
) | ||
}) | ||
|
||
test('it should throw if element is not valid', () => { | ||
const {queryByTestId} = render(` | ||
<div data-testid="div">Banana</div> | ||
<input type="radio" data-testid="radio" value="Something" /> | ||
<input type="checkbox" data-testid="checkbox" /> | ||
`) | ||
|
||
let errorMessage | ||
try { | ||
expect(queryByTestId('div')).toHaveDisplayValue('Banana') | ||
} catch (err) { | ||
errorMessage = err.message | ||
} | ||
|
||
expect(errorMessage).toMatchInlineSnapshot( | ||
`".toHaveDisplayValue() currently supports only input, textarea or select elements, try with another matcher instead."`, | ||
) | ||
|
||
try { | ||
expect(queryByTestId('radio')).toHaveDisplayValue('Something') | ||
} catch (err) { | ||
errorMessage = err.message | ||
} | ||
|
||
expect(errorMessage).toMatchInlineSnapshot( | ||
`".toHaveDisplayValue() currently does not support input[type=\\"radio\\"], try with another matcher instead."`, | ||
) | ||
|
||
try { | ||
expect(queryByTestId('checkbox')).toHaveDisplayValue(true) | ||
} catch (err) { | ||
errorMessage = err.message | ||
} | ||
|
||
expect(errorMessage).toMatchInlineSnapshot( | ||
`".toHaveDisplayValue() currently does not support input[type=\\"checkbox\\"], try with another matcher instead."`, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {matcherHint} from 'jest-matcher-utils' | ||
|
||
import {checkHtmlElement, getMessage} from './utils' | ||
|
||
export function toHaveDisplayValue(htmlElement, expectedValue) { | ||
checkHtmlElement(htmlElement, toHaveDisplayValue, this) | ||
const tagName = htmlElement.tagName.toLowerCase() | ||
|
||
if (!['select', 'input', 'textarea'].includes(tagName)) { | ||
throw new Error( | ||
'.toHaveDisplayValue() currently supports only input, textarea or select elements, try with another matcher instead.', | ||
) | ||
} | ||
|
||
if (tagName === 'input' && ['radio', 'checkbox'].includes(htmlElement.type)) { | ||
throw new Error( | ||
`.toHaveDisplayValue() currently does not support input[type="${htmlElement.type}"], try with another matcher instead.`, | ||
) | ||
} | ||
|
||
const value = | ||
tagName === 'select' | ||
? Array.from(htmlElement) | ||
.filter(option => option.selected) | ||
.map(option => option.textContent) | ||
.toString() | ||
: htmlElement.value | ||
|
||
return { | ||
pass: value === expectedValue.toString(), | ||
gnapse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
message: () => | ||
getMessage( | ||
matcherHint( | ||
`${this.isNot ? '.not' : ''}.toHaveDisplayValue`, | ||
'element', | ||
'', | ||
), | ||
`Expected element ${this.isNot ? 'not ' : ''}to have display value`, | ||
expectedValue, | ||
'Received', | ||
value, | ||
), | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice if the test also covered the case of changing the selected option programmatically to
banana
and then check thattoHaveDisplayValue('Banana')
pass.And maybe do it for the other cases as well (textarea and input).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I'm wondering why the repository is not using
fireEvent
from the testing-library core (or even userEvent) 🤔