forked from testing-library/dom-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alt-text.js
41 lines (37 loc) 路 1.16 KB
/
alt-text.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
import {matches, fuzzyMatches, makeNormalizer, buildQueries} from './all-utils'
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
function queryAllByAltText(
container,
alt,
{exact = true, collapseWhitespace, trim, normalizer} = {},
) {
const matcher = exact ? matches : fuzzyMatches
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
return Array.from(container.querySelectorAll('img,input,area')).filter(node =>
matcher(node.getAttribute('alt'), node, alt, matchNormalizer),
)
}
const getMultipleError = (c, alt) =>
`Found multiple elements with the alt text: ${alt}`
const getMissingError = (c, alt) =>
`Unable to find an element with the alt text: ${alt}`
const queryAllByAltTextWithSuggestions = wrapAllByQueryWithSuggestion(
queryAllByAltText,
queryAllByAltText.name,
'queryAll',
)
const [
queryByAltText,
getAllByAltText,
getByAltText,
findAllByAltText,
findByAltText,
] = buildQueries(queryAllByAltText, getMultipleError, getMissingError)
export {
queryByAltText,
queryAllByAltTextWithSuggestions as queryAllByAltText,
getByAltText,
getAllByAltText,
findAllByAltText,
findByAltText,
}