Skip to content

Commit bb79e30

Browse files
eps1lontimdeschryverRayat Rahman
authored
chore(types): Convert src/query-helpers to TS (#1016)
Co-authored-by: Tim Deschryver <[email protected]> Co-authored-by: Rayat Rahman <[email protected]> Co-authored-by: Tim Deschryver <[email protected]>
1 parent ff84a50 commit bb79e30

File tree

10 files changed

+202
-111
lines changed

10 files changed

+202
-111
lines changed

src/queries/alt-text.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ const queryAllByAltText: AllByBoundAttribute = (
1818
)
1919
}
2020

21-
const getMultipleError: GetErrorFunction = (c, alt) =>
21+
const getMultipleError: GetErrorFunction<[unknown]> = (c, alt) =>
2222
`Found multiple elements with the alt text: ${alt}`
23-
const getMissingError: GetErrorFunction = (c, alt) =>
23+
const getMissingError: GetErrorFunction<[unknown]> = (c, alt) =>
2424
`Unable to find an element with the alt text: ${alt}`
2525

26-
const queryAllByAltTextWithSuggestions = wrapAllByQueryWithSuggestion(
27-
queryAllByAltText,
28-
queryAllByAltText.name,
29-
'queryAll',
30-
)
26+
const queryAllByAltTextWithSuggestions = wrapAllByQueryWithSuggestion<
27+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
28+
[altText: Matcher, options?: SelectorMatcherOptions]
29+
>(queryAllByAltText, queryAllByAltText.name, 'queryAll')
3130
const [
3231
queryByAltText,
3332
getAllByAltText,

src/queries/display-value.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
22
import {checkContainerType} from '../helpers'
3-
import {AllByBoundAttribute, GetErrorFunction} from '../../types'
3+
import {
4+
AllByBoundAttribute,
5+
GetErrorFunction,
6+
Matcher,
7+
MatcherOptions,
8+
} from '../../types'
49
import {
510
getNodeText,
611
matches,
@@ -38,16 +43,15 @@ const queryAllByDisplayValue: AllByBoundAttribute = (
3843
})
3944
}
4045

41-
const getMultipleError: GetErrorFunction = (c, value) =>
46+
const getMultipleError: GetErrorFunction<[unknown]> = (c, value) =>
4247
`Found multiple elements with the display value: ${value}.`
43-
const getMissingError: GetErrorFunction = (c, value) =>
48+
const getMissingError: GetErrorFunction<[unknown]> = (c, value) =>
4449
`Unable to find an element with the display value: ${value}.`
4550

46-
const queryAllByDisplayValueWithSuggestions = wrapAllByQueryWithSuggestion(
47-
queryAllByDisplayValue,
48-
queryAllByDisplayValue.name,
49-
'queryAll',
50-
)
51+
const queryAllByDisplayValueWithSuggestions = wrapAllByQueryWithSuggestion<
52+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
53+
[value: Matcher, options?: MatcherOptions]
54+
>(queryAllByDisplayValue, queryAllByDisplayValue.name, 'queryAll')
5155

5256
const [
5357
queryByDisplayValue,

src/queries/label-text.ts

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import {getConfig} from '../config'
22
import {checkContainerType} from '../helpers'
33
import {getLabels, getRealLabels, getLabelContent} from '../label-helpers'
4-
import {AllByText, GetErrorFunction} from '../../types'
4+
import {
5+
AllByText,
6+
GetErrorFunction,
7+
Matcher,
8+
MatcherOptions,
9+
SelectorMatcherOptions,
10+
} from '../../types'
511
import {
612
fuzzyMatches,
713
matches,
@@ -100,9 +106,6 @@ const queryAllByLabelText: AllByText = (
100106
return labelledElements
101107
}, [])
102108
.concat(
103-
// TODO: Remove ignore after `queryAllByAttribute` will be moved to TS
104-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
105-
// @ts-expect-error
106109
queryAllByAttribute('aria-label', container, text, {
107110
exact,
108111
normalizer: matchNormalizer,
@@ -171,42 +174,44 @@ function getTagNameOfElementAssociatedWithLabelViaFor(
171174
}
172175

173176
// the reason mentioned above is the same reason we're not using buildQueries
174-
const getMultipleError: GetErrorFunction = (c, text) =>
177+
const getMultipleError: GetErrorFunction<[unknown]> = (c, text) =>
175178
`Found multiple elements with the text of: ${text}`
176-
const queryByLabelText = wrapSingleQueryWithSuggestion(
179+
const queryByLabelText = wrapSingleQueryWithSuggestion<
180+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
181+
[labelText: Matcher, options?: SelectorMatcherOptions]
182+
>(
177183
makeSingleQuery(queryAllByLabelText, getMultipleError),
178184
queryAllByLabelText.name,
179185
'query',
180186
)
181187
const getByLabelText = makeSingleQuery(getAllByLabelText, getMultipleError)
182188

183189
const findAllByLabelText = makeFindQuery(
184-
wrapAllByQueryWithSuggestion(
185-
getAllByLabelText,
186-
getAllByLabelText.name,
187-
'findAll',
188-
),
190+
wrapAllByQueryWithSuggestion<
191+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
192+
[labelText: Matcher, options?: SelectorMatcherOptions]
193+
>(getAllByLabelText, getAllByLabelText.name, 'findAll'),
189194
)
190195
const findByLabelText = makeFindQuery(
191-
wrapSingleQueryWithSuggestion(getByLabelText, getAllByLabelText.name, 'find'),
192-
)
193-
194-
const getAllByLabelTextWithSuggestions = wrapAllByQueryWithSuggestion(
195-
getAllByLabelText,
196-
getAllByLabelText.name,
197-
'getAll',
198-
)
199-
const getByLabelTextWithSuggestions = wrapSingleQueryWithSuggestion(
200-
getByLabelText,
201-
getAllByLabelText.name,
202-
'get',
196+
wrapSingleQueryWithSuggestion<
197+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
198+
[labelText: Matcher, options?: SelectorMatcherOptions]
199+
>(getByLabelText, getAllByLabelText.name, 'find'),
203200
)
204201

205-
const queryAllByLabelTextWithSuggestions = wrapAllByQueryWithSuggestion(
206-
queryAllByLabelText,
207-
queryAllByLabelText.name,
208-
'queryAll',
209-
)
202+
const getAllByLabelTextWithSuggestions = wrapAllByQueryWithSuggestion<
203+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
204+
[labelText: Matcher, options?: MatcherOptions]
205+
>(getAllByLabelText, getAllByLabelText.name, 'getAll')
206+
const getByLabelTextWithSuggestions = wrapSingleQueryWithSuggestion<
207+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
208+
[labelText: Matcher, options?: SelectorMatcherOptions]
209+
>(getByLabelText, getAllByLabelText.name, 'get')
210+
211+
const queryAllByLabelTextWithSuggestions = wrapAllByQueryWithSuggestion<
212+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
213+
[labelText: Matcher, options?: SelectorMatcherOptions]
214+
>(queryAllByLabelText, queryAllByLabelText.name, 'queryAll')
210215

211216
export {
212217
queryAllByLabelTextWithSuggestions as queryAllByLabelText,

src/queries/placeholder-text.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@ import {queryAllByAttribute, buildQueries} from './all-utils'
55

66
const queryAllByPlaceholderText: AllByBoundAttribute = (...args) => {
77
checkContainerType(args[0])
8-
// TODO: Remove ignore after `queryAllByAttribute` will be moved to TS
9-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
10-
// @ts-expect-error
118
return queryAllByAttribute('placeholder', ...args)
129
}
13-
const getMultipleError: GetErrorFunction = (c, text) =>
10+
const getMultipleError: GetErrorFunction<[unknown]> = (c, text) =>
1411
`Found multiple elements with the placeholder text of: ${text}`
15-
const getMissingError: GetErrorFunction = (c, text) =>
12+
const getMissingError: GetErrorFunction<[unknown]> = (c, text) =>
1613
`Unable to find an element with the placeholder text of: ${text}`
1714

18-
const queryAllByPlaceholderTextWithSuggestions = wrapAllByQueryWithSuggestion(
19-
queryAllByPlaceholderText,
20-
queryAllByPlaceholderText.name,
21-
'queryAll',
22-
)
15+
const queryAllByPlaceholderTextWithSuggestions = wrapAllByQueryWithSuggestion<
16+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
17+
[placeholderText: Matcher, options?: MatcherOptions]
18+
>(queryAllByPlaceholderText, queryAllByPlaceholderText.name, 'queryAll')
2319

2420
const [
2521
queryByPlaceholderText,

src/queries/test-id.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@ const getTestIdAttribute = () => getConfig().testIdAttribute
77

88
const queryAllByTestId: AllByBoundAttribute = (...args) => {
99
checkContainerType(args[0])
10-
// TODO: Remove ignore after `queryAllByAttribute` will be moved to TS
11-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
12-
// @ts-expect-error
1310
return queryAllByAttribute(getTestIdAttribute(), ...args)
1411
}
1512

16-
const getMultipleError: GetErrorFunction = (c, id) =>
13+
const getMultipleError: GetErrorFunction<[unknown]> = (c, id) =>
1714
`Found multiple elements by: [${getTestIdAttribute()}="${id}"]`
18-
const getMissingError: GetErrorFunction = (c, id) =>
15+
const getMissingError: GetErrorFunction<[unknown]> = (c, id) =>
1916
`Unable to find an element by: [${getTestIdAttribute()}="${id}"]`
2017

21-
const queryAllByTestIdWithSuggestions = wrapAllByQueryWithSuggestion(
22-
queryAllByTestId,
23-
queryAllByTestId.name,
24-
'queryAll',
25-
)
18+
const queryAllByTestIdWithSuggestions = wrapAllByQueryWithSuggestion<
19+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
20+
[testId: Matcher, options?: MatcherOptions]
21+
>(queryAllByTestId, queryAllByTestId.name, 'queryAll')
2622

2723
const [
2824
queryByTestId,

src/queries/text.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,15 @@ const queryAllByText: AllByText = (
4040
)
4141
}
4242

43-
const getMultipleError: GetErrorFunction = (c, text) =>
43+
const getMultipleError: GetErrorFunction<[unknown]> = (c, text) =>
4444
`Found multiple elements with the text: ${text}`
45-
const getMissingError: GetErrorFunction = (c, text) =>
45+
const getMissingError: GetErrorFunction<[unknown]> = (c, text) =>
4646
`Unable to find an element with the text: ${text}. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.`
4747

48-
const queryAllByTextWithSuggestions = wrapAllByQueryWithSuggestion(
49-
queryAllByText,
50-
queryAllByText.name,
51-
'queryAll',
52-
)
48+
const queryAllByTextWithSuggestions = wrapAllByQueryWithSuggestion<
49+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
50+
[text: Matcher, options?: MatcherOptions]
51+
>(queryAllByText, queryAllByText.name, 'queryAll')
5352

5453
const [queryByText, getAllByText, getByText, findAllByText, findByText] =
5554
buildQueries(queryAllByText, getMultipleError, getMissingError)

src/queries/title.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
22
import {checkContainerType} from '../helpers'
3-
import {AllByBoundAttribute, GetErrorFunction} from '../../types'
3+
import {
4+
AllByBoundAttribute,
5+
GetErrorFunction,
6+
Matcher,
7+
MatcherOptions,
8+
} from '../../types'
49
import {
510
fuzzyMatches,
611
matches,
@@ -31,16 +36,15 @@ const queryAllByTitle: AllByBoundAttribute = (
3136
)
3237
}
3338

34-
const getMultipleError: GetErrorFunction = (c, title) =>
39+
const getMultipleError: GetErrorFunction<[unknown]> = (c, title) =>
3540
`Found multiple elements with the title: ${title}.`
36-
const getMissingError: GetErrorFunction = (c, title) =>
41+
const getMissingError: GetErrorFunction<[unknown]> = (c, title) =>
3742
`Unable to find an element with the title: ${title}.`
3843

39-
const queryAllByTitleWithSuggestions = wrapAllByQueryWithSuggestion(
40-
queryAllByTitle,
41-
queryAllByTitle.name,
42-
'queryAll',
43-
)
44+
const queryAllByTitleWithSuggestions = wrapAllByQueryWithSuggestion<
45+
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
46+
[title: Matcher, options?: MatcherOptions]
47+
>(queryAllByTitle, queryAllByTitle.name, 'queryAll')
4448

4549
const [queryByTitle, getAllByTitle, getByTitle, findAllByTitle, findByTitle] =
4650
buildQueries(queryAllByTitle, getMultipleError, getMissingError)

0 commit comments

Comments
 (0)