forked from testing-library/dom-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display-value.js
59 lines (54 loc) 路 1.62 KB
/
display-value.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
getNodeText,
matches,
fuzzyMatches,
makeNormalizer,
buildQueries,
} from './all-utils'
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
function queryAllByDisplayValue(
container,
value,
{exact = true, collapseWhitespace, trim, normalizer} = {},
) {
const matcher = exact ? matches : fuzzyMatches
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
return Array.from(container.querySelectorAll(`input,textarea,select`)).filter(
node => {
if (node.tagName === 'SELECT') {
const selectedOptions = Array.from(node.options).filter(
option => option.selected,
)
return selectedOptions.some(optionNode =>
matcher(getNodeText(optionNode), optionNode, value, matchNormalizer),
)
} else {
return matcher(node.value, node, value, matchNormalizer)
}
},
)
}
const getMultipleError = (c, value) =>
`Found multiple elements with the display value: ${value}.`
const getMissingError = (c, value) =>
`Unable to find an element with the display value: ${value}.`
const queryAllByDisplayValueWithSuggestions = wrapAllByQueryWithSuggestion(
queryAllByDisplayValue,
queryAllByDisplayValue.name,
'queryAll',
)
const [
queryByDisplayValue,
getAllByDisplayValue,
getByDisplayValue,
findAllByDisplayValue,
findByDisplayValue,
] = buildQueries(queryAllByDisplayValue, getMultipleError, getMissingError)
export {
queryByDisplayValue,
queryAllByDisplayValueWithSuggestions as queryAllByDisplayValue,
getByDisplayValue,
getAllByDisplayValue,
findAllByDisplayValue,
findByDisplayValue,
}