-
Notifications
You must be signed in to change notification settings - Fork 0
/
title.js
52 lines (47 loc) · 1.31 KB
/
title.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
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
import {checkContainerType} from '../helpers'
import {
fuzzyMatches,
matches,
makeNormalizer,
getNodeText,
buildQueries,
} from './all-utils'
function queryAllByTitle(
container,
text,
{exact = true, collapseWhitespace, trim, normalizer} = {},
) {
checkContainerType(container)
const matcher = exact ? matches : fuzzyMatches
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
return Array.from(container.querySelectorAll('[title], svg > title')).filter(
node =>
matcher(node.getAttribute('title'), node, text, matchNormalizer) ||
matcher(getNodeText(node), node, text, matchNormalizer),
)
}
const getMultipleError = (c, title) =>
`Found multiple elements with the title: ${title}.`
const getMissingError = (c, title) =>
`Unable to find an element with the title: ${title}.`
const queryAllByTitleWithSuggestions = wrapAllByQueryWithSuggestion(
queryAllByTitle,
queryAllByTitle.name,
'queryAll',
)
const [
queryByTitle,
getAllByTitle,
getByTitle,
findAllByTitle,
findByTitle,
] = buildQueries(queryAllByTitle, getMultipleError, getMissingError)
export {
queryByTitle,
queryAllByTitleWithSuggestions as queryAllByTitle,
getByTitle,
getAllByTitle,
findAllByTitle,
findByTitle,
}