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
Allow to use regex on toHaveDisplayValue #242
Merged
gnapse
merged 10 commits into
testing-library:master
from
lourenci:to-have-display-value-regex
May 7, 2020
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f4f4173
Allow to use regex on non-multiple value on toHaveDisplayValue
lourenci 26c6e71
Allow to use regex on multiple value on toHaveDisplayValue
lourenci c717de7
Allow to use regex on multiple value on toHaveDisplayValue
lourenci b713864
Do some refactor
lourenci cc3ebdc
Split specs in contexts
lourenci 259552b
Add to README.md
lourenci d014eec
Add to README.md
lourenci ca2c16d
Update docs to describe the unordered multiple match
lourenci fb33aa8
Merge branch 'master' into to-have-display-value-regex
gnapse 1e63854
Add review
lourenci 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
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import {matcherHint} from 'jest-matcher-utils' | ||
|
||
import {checkHtmlElement, getMessage} from './utils' | ||
import {matches, checkHtmlElement, getMessage} from './utils' | ||
|
||
export function toHaveDisplayValue(htmlElement, expectedValue) { | ||
checkHtmlElement(htmlElement, toHaveDisplayValue, this) | ||
|
@@ -18,16 +17,19 @@ export function toHaveDisplayValue(htmlElement, expectedValue) { | |
) | ||
} | ||
|
||
const value = | ||
tagName === 'select' | ||
? Array.from(htmlElement) | ||
.filter(option => option.selected) | ||
.map(option => option.textContent) | ||
.toString() | ||
: htmlElement.value | ||
const values = getValues(tagName, htmlElement) | ||
const expectedValues = getExpectedValues(expectedValue) | ||
const qtyOfMatchesWithValues = getQtyOfMatchesBetweenArrays( | ||
values, | ||
expectedValues, | ||
) | ||
|
||
const matchedWithAllValues = qtyOfMatchesWithValues === values.length | ||
const matchedWithAllExpectedValues = | ||
qtyOfMatchesWithValues === expectedValues.length | ||
|
||
return { | ||
pass: value === expectedValue.toString(), | ||
pass: matchedWithAllValues && matchedWithAllExpectedValues, | ||
message: () => | ||
getMessage( | ||
matcherHint( | ||
|
@@ -38,7 +40,25 @@ export function toHaveDisplayValue(htmlElement, expectedValue) { | |
`Expected element ${this.isNot ? 'not ' : ''}to have display value`, | ||
expectedValue, | ||
'Received', | ||
value, | ||
values, | ||
), | ||
} | ||
} | ||
|
||
function getValues(tagName, htmlElement) { | ||
return tagName === 'select' | ||
? Array.from(htmlElement) | ||
.filter(option => option.selected) | ||
.map(option => option.textContent) | ||
: [htmlElement.value] | ||
} | ||
|
||
function getExpectedValues(expectedValue) { | ||
return expectedValue instanceof Array ? expectedValue : [expectedValue] | ||
} | ||
|
||
function getQtyOfMatchesBetweenArrays(arrayBase, array) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have a fully readable name like |
||
return array.filter( | ||
expected => arrayBase.filter(value => matches(value, expected)).length, | ||
).length | ||
} |
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.
We'd rather not share a variable across tests. No need for this or the
afterEach
that resets it.If you need to reuse the rendering part, extract it to a function in the module scope and call that function on each test that needs it.