Skip to content

Commit 385625e

Browse files
committed
Migrate the rest of src (not stable yet)
1 parent b4e8342 commit 385625e

34 files changed

+235
-771
lines changed

src/get-queries-for-element.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

types/get-queries-for-element.d.ts renamed to src/get-queries-for-element.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as queries from './queries'
1+
import * as defaultQueries from './queries'
22

33
export type BoundFunction<T> = T extends (
44
attribute: string,
@@ -34,7 +34,22 @@ export interface Queries {
3434
[T: string]: Query
3535
}
3636

37-
export function getQueriesForElement<T extends Queries = typeof queries>(
37+
/**
38+
* @param {HTMLElement} element container
39+
* @param {FuncMap} queries object of functions
40+
* @param {Object} initialValue for reducer
41+
* @returns {FuncMap} returns object of functions bound to container
42+
*/
43+
function getQueriesForElement<T extends Queries>(
3844
element: HTMLElement,
39-
queriesToBind?: T,
40-
): BoundFunctions<T>
45+
queries: Queries = defaultQueries,
46+
initialValue = {},
47+
): BoundFunctions<T> {
48+
return Object.keys(queries).reduce((helpers, key) => {
49+
const fn: Query = queries[key]
50+
helpers[key] = fn.bind(null, element)
51+
return helpers
52+
}, initialValue) as BoundFunctions<T>
53+
}
54+
55+
export {getQueriesForElement}
File renamed without changes.

src/matches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function fuzzyMatches(
3636
}
3737

3838
function matches(
39-
textToMatch: string,
39+
textToMatch: string | undefined,
4040
node: HTMLElement | null,
4141
matcher: Matcher,
4242
normalizer: NormalizerFn,
File renamed without changes.

src/queries/alt-text.js renamed to src/queries/alt-text.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
22
import {checkContainerType} from '../helpers'
3-
import {matches, fuzzyMatches, makeNormalizer, buildQueries} from './all-utils'
3+
import {
4+
matches,
5+
fuzzyMatches,
6+
makeNormalizer,
7+
buildQueries,
8+
MatcherOptions,
9+
Matcher,
10+
} from './all-utils'
411

512
function queryAllByAltText(
6-
container,
7-
alt,
8-
{exact = true, collapseWhitespace, trim, normalizer} = {},
13+
container: HTMLElement,
14+
alt: Matcher,
15+
{exact = true, collapseWhitespace, trim, normalizer}: MatcherOptions = {},
916
) {
1017
checkContainerType(container)
1118
const matcher = exact ? matches : fuzzyMatches
1219
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
13-
return Array.from(container.querySelectorAll('img,input,area')).filter(node =>
20+
return Array.from(
21+
container.querySelectorAll('img,input,area'),
22+
).filter((node: HTMLElement) =>
1423
matcher(node.getAttribute('alt'), node, alt, matchNormalizer),
15-
)
24+
) as HTMLElement[]
1625
}
1726

1827
const getMultipleError = (c, alt) =>

src/queries/display-value.js renamed to src/queries/display-value.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,37 @@ import {
66
fuzzyMatches,
77
makeNormalizer,
88
buildQueries,
9+
Matcher,
10+
MatcherOptions,
911
} from './all-utils'
1012

13+
function getElementValue(element: Element): string | undefined {
14+
return (element as any).value
15+
}
16+
1117
function queryAllByDisplayValue(
12-
container,
13-
value,
14-
{exact = true, collapseWhitespace, trim, normalizer} = {},
18+
container: HTMLElement,
19+
value: Matcher,
20+
{exact = true, collapseWhitespace, trim, normalizer}: MatcherOptions = {},
1521
) {
1622
checkContainerType(container)
1723
const matcher = exact ? matches : fuzzyMatches
1824
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
1925
return Array.from(container.querySelectorAll(`input,textarea,select`)).filter(
20-
node => {
26+
(node: HTMLElement) => {
2127
if (node.tagName === 'SELECT') {
22-
const selectedOptions = Array.from(node.options).filter(
28+
const selectElement = node as HTMLSelectElement
29+
const selectedOptions = Array.from(selectElement.options).filter(
2330
option => option.selected,
2431
)
2532
return selectedOptions.some(optionNode =>
2633
matcher(getNodeText(optionNode), optionNode, value, matchNormalizer),
2734
)
2835
} else {
29-
return matcher(node.value, node, value, matchNormalizer)
36+
return matcher(getElementValue(node), node, value, matchNormalizer)
3037
}
3138
},
32-
)
39+
) as HTMLElement[]
3340
}
3441

3542
const getMultipleError = (c, value) =>
File renamed without changes.

src/queries/label-text.js renamed to src/queries/label-text.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import {
99
makeSingleQuery,
1010
wrapAllByQueryWithSuggestion,
1111
wrapSingleQueryWithSuggestion,
12+
Matcher,
13+
MatcherOptions,
14+
SelectorMatcherOptions,
1215
} from './all-utils'
1316
import {queryAllByText} from './text'
1417

1518
function queryAllLabelsByText(
16-
container,
17-
text,
18-
{exact = true, trim, collapseWhitespace, normalizer} = {},
19+
container: HTMLElement,
20+
text: Matcher,
21+
{exact = true, trim, collapseWhitespace, normalizer}: MatcherOptions = {},
1922
) {
2023
const matcher = exact ? matches : fuzzyMatches
2124
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
@@ -35,13 +38,19 @@ function queryAllLabelsByText(
3538
})
3639

3740
return matcher(textToMatch, label, text, matchNormalizer)
38-
})
41+
}) as HTMLElement[]
3942
}
4043

4144
function queryAllByLabelText(
42-
container,
43-
text,
44-
{selector = '*', exact = true, collapseWhitespace, trim, normalizer} = {},
45+
container: HTMLElement,
46+
text: Matcher,
47+
{
48+
selector = '*',
49+
exact = true,
50+
collapseWhitespace,
51+
trim,
52+
normalizer,
53+
}: SelectorMatcherOptions = {},
4554
) {
4655
checkContainerType(container)
4756

@@ -51,7 +60,7 @@ function queryAllByLabelText(
5160
normalizer: matchNormalizer,
5261
})
5362
const labelledElements = labels
54-
.reduce((matchedElements, label) => {
63+
.reduce((matchedElements, label: HTMLLabelElement) => {
5564
const elementsForLabel = []
5665
if (label.control) {
5766
elementsForLabel.push(label.control)
@@ -112,7 +121,7 @@ function queryAllByLabelText(
112121

113122
return Array.from(
114123
new Set([...labelledElements, ...ariaLabelledElements]),
115-
).filter(element => element.matches(selector))
124+
).filter(element => element.matches(selector)) as HTMLElement[]
116125
}
117126

118127
// the getAll* query would normally look like this:

src/queries/placeholder-text.js renamed to src/queries/placeholder-text.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
22
import {checkContainerType} from '../helpers'
3-
import {queryAllByAttribute, buildQueries} from './all-utils'
3+
import {
4+
queryAllByAttribute,
5+
buildQueries,
6+
Matcher,
7+
MatcherOptions,
8+
} from './all-utils'
49

5-
function queryAllByPlaceholderText(...args) {
6-
checkContainerType(...args)
7-
return queryAllByAttribute('placeholder', ...args)
10+
function queryAllByPlaceholderText(
11+
container: HTMLElement,
12+
placeholder: Matcher,
13+
options?: MatcherOptions,
14+
) {
15+
checkContainerType(container)
16+
return queryAllByAttribute('placeholder', container, placeholder, options)
817
}
918
const getMultipleError = (c, text) =>
1019
`Found multiple elements with the placeholder text of: ${text}`

src/queries/role.js renamed to src/queries/role.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,40 @@ import {
1414
fuzzyMatches,
1515
getConfig,
1616
makeNormalizer,
17+
Matcher,
18+
MatcherOptions,
1719
matches,
1820
} from './all-utils'
1921

22+
export interface ByRoleOptions extends MatcherOptions {
23+
/**
24+
* If true includes elements in the query set that are usually excluded from
25+
* the accessibility tree. `role="none"` or `role="presentation"` are included
26+
* in either case.
27+
*/
28+
hidden?: boolean
29+
/**
30+
* If true only includes elements in the query set that are marked as
31+
* selected in the accessibility tree, i.e., `aria-selected="true"`
32+
*/
33+
selected?: boolean
34+
/**
35+
* Includes every role used in the `role` attribute
36+
* For example *ByRole('progressbar', {queryFallbacks: true})` will find <div role="meter progressbar">`.
37+
*/
38+
queryFallbacks?: boolean
39+
/**
40+
* Only considers elements with the specified accessible name.
41+
*/
42+
name?:
43+
| string
44+
| RegExp
45+
| ((accessibleName: string, element: Element) => boolean)
46+
}
47+
2048
function queryAllByRole(
21-
container,
22-
role,
49+
container: HTMLElement,
50+
role: Matcher,
2351
{
2452
exact = true,
2553
collapseWhitespace,
@@ -29,7 +57,7 @@ function queryAllByRole(
2957
normalizer,
3058
queryFallbacks = false,
3159
selected,
32-
} = {},
60+
}: ByRoleOptions = {},
3361
) {
3462
checkContainerType(container)
3563
const matcher = exact ? matches : fuzzyMatches
@@ -52,7 +80,7 @@ function queryAllByRole(
5280
}
5381

5482
return Array.from(container.querySelectorAll('*'))
55-
.filter(node => {
83+
.filter((node: HTMLElement) => {
5684
const isRoleSpecifiedExplicitly = node.hasAttribute('role')
5785

5886
if (isRoleSpecifiedExplicitly) {
@@ -85,14 +113,14 @@ function queryAllByRole(
85113
// don't care if aria attributes are unspecified
86114
return true
87115
})
88-
.filter(element => {
116+
.filter((element: HTMLElement) => {
89117
return hidden === false
90118
? isInaccessible(element, {
91119
isSubtreeInaccessible: cachedIsSubtreeInaccessible,
92120
}) === false
93121
: true
94122
})
95-
.filter(element => {
123+
.filter((element: HTMLElement) => {
96124
if (name === undefined) {
97125
// Don't care
98126
return true
@@ -104,7 +132,7 @@ function queryAllByRole(
104132
name,
105133
text => text,
106134
)
107-
})
135+
}) as HTMLElement[]
108136
}
109137

110138
const getMultipleError = (c, role) =>
@@ -113,16 +141,17 @@ const getMultipleError = (c, role) =>
113141
const getMissingError = (
114142
container,
115143
role,
116-
{hidden = getConfig().defaultHidden, name} = {},
144+
{hidden = getConfig().defaultHidden, name}: ByRoleOptions = {},
117145
) => {
118146
if (getConfig()._disableExpensiveErrorDiagnostics) {
119147
return `Unable to find role="${role}"`
120148
}
121149

122150
let roles = ''
123-
Array.from(container.children).forEach(childElement => {
151+
Array.from(container.children).forEach((childElement: HTMLElement) => {
124152
roles += prettyRoles(childElement, {
125153
hidden,
154+
// @ts-ignore FIXME remove this code? prettyRoles does not seem handle 'includeName'
126155
includeName: name !== undefined,
127156
})
128157
})

src/queries/test-id.js renamed to src/queries/test-id.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import {checkContainerType} from '../helpers'
22
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
3-
import {queryAllByAttribute, getConfig, buildQueries} from './all-utils'
3+
import {
4+
queryAllByAttribute,
5+
getConfig,
6+
buildQueries,
7+
Matcher,
8+
MatcherOptions,
9+
} from './all-utils'
410

511
const getTestIdAttribute = () => getConfig().testIdAttribute
612

7-
function queryAllByTestId(...args) {
8-
checkContainerType(...args)
9-
return queryAllByAttribute(getTestIdAttribute(), ...args)
13+
function queryAllByTestId(
14+
container: HTMLElement,
15+
testId: Matcher,
16+
options?: MatcherOptions,
17+
) {
18+
checkContainerType(container)
19+
return queryAllByAttribute(getTestIdAttribute(), container, testId, options)
1020
}
1121

1222
const getMultipleError = (c, id) =>

0 commit comments

Comments
 (0)