Skip to content

Commit f55f19b

Browse files
dfoverdxeps1lon
andauthored
feat: Add element type generic argument to queries (#1023)
Co-authored-by: Beth Hitch <bethany.hitch@milliman.com> Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
1 parent 8311c8d commit f55f19b

File tree

1 file changed

+176
-80
lines changed

1 file changed

+176
-80
lines changed

types/queries.d.ts

Lines changed: 176 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
1-
import {Matcher, MatcherOptions, ByRoleMatcher} from './matches'
1+
import {ByRoleMatcher, Matcher, MatcherOptions} from './matches'
22
import {SelectorMatcherOptions} from './query-helpers'
33
import {waitForOptions} from './wait-for'
44

5-
export type QueryByBoundAttribute = (
5+
export type QueryByBoundAttribute<T extends HTMLElement = HTMLElement> = (
66
container: HTMLElement,
77
id: Matcher,
88
options?: MatcherOptions,
9-
) => HTMLElement | null
9+
) => T | null
1010

11-
export type AllByBoundAttribute = (
11+
export type AllByBoundAttribute<T extends HTMLElement = HTMLElement> = (
1212
container: HTMLElement,
1313
id: Matcher,
1414
options?: MatcherOptions,
15-
) => HTMLElement[]
15+
) => T[]
1616

17-
export type FindAllByBoundAttribute = (
17+
export type FindAllByBoundAttribute<T extends HTMLElement = HTMLElement> = (
1818
container: HTMLElement,
1919
id: Matcher,
2020
options?: MatcherOptions,
2121
waitForElementOptions?: waitForOptions,
22-
) => Promise<HTMLElement[]>
22+
) => Promise<T[]>
2323

24-
export type GetByBoundAttribute = (
24+
export type GetByBoundAttribute<T extends HTMLElement = HTMLElement> = (
2525
container: HTMLElement,
2626
id: Matcher,
2727
options?: MatcherOptions,
28-
) => HTMLElement
28+
) => T
2929

30-
export type FindByBoundAttribute = (
30+
export type FindByBoundAttribute<T extends HTMLElement = HTMLElement> = (
3131
container: HTMLElement,
3232
id: Matcher,
3333
options?: MatcherOptions,
3434
waitForElementOptions?: waitForOptions,
35-
) => Promise<HTMLElement>
35+
) => Promise<T>
3636

37-
export type QueryByText = (
37+
export type QueryByText<T extends HTMLElement = HTMLElement> = (
3838
container: HTMLElement,
3939
id: Matcher,
4040
options?: SelectorMatcherOptions,
41-
) => HTMLElement | null
41+
) => T | null
4242

43-
export type AllByText = (
43+
export type AllByText<T extends HTMLElement = HTMLElement> = (
4444
container: HTMLElement,
4545
id: Matcher,
4646
options?: SelectorMatcherOptions,
47-
) => HTMLElement[]
47+
) => T[]
4848

49-
export type FindAllByText = (
49+
export type FindAllByText<T extends HTMLElement = HTMLElement> = (
5050
container: HTMLElement,
5151
id: Matcher,
5252
options?: SelectorMatcherOptions,
5353
waitForElementOptions?: waitForOptions,
54-
) => Promise<HTMLElement[]>
54+
) => Promise<T[]>
5555

56-
export type GetByText = (
56+
export type GetByText<T extends HTMLElement = HTMLElement> = (
5757
container: HTMLElement,
5858
id: Matcher,
5959
options?: SelectorMatcherOptions,
60-
) => HTMLElement
60+
) => T
6161

62-
export type FindByText = (
62+
export type FindByText<T extends HTMLElement = HTMLElement> = (
6363
container: HTMLElement,
6464
id: Matcher,
6565
options?: SelectorMatcherOptions,
6666
waitForElementOptions?: waitForOptions,
67-
) => Promise<HTMLElement>
67+
) => Promise<T>
6868

6969
export interface ByRoleOptions extends MatcherOptions {
7070
/**
@@ -117,83 +117,179 @@ export interface ByRoleOptions extends MatcherOptions {
117117
| ((accessibleName: string, element: Element) => boolean)
118118
}
119119

120-
export type AllByRole = (
120+
export type AllByRole<T extends HTMLElement = HTMLElement> = (
121121
container: HTMLElement,
122122
role: ByRoleMatcher,
123123
options?: ByRoleOptions,
124-
) => HTMLElement[]
124+
) => T[]
125125

126-
export type GetByRole = (
126+
export type GetByRole<T extends HTMLElement = HTMLElement> = (
127127
container: HTMLElement,
128128
role: ByRoleMatcher,
129129
options?: ByRoleOptions,
130-
) => HTMLElement
130+
) => T
131131

132-
export type QueryByRole = (
132+
export type QueryByRole<T extends HTMLElement = HTMLElement> = (
133133
container: HTMLElement,
134134
role: ByRoleMatcher,
135135
options?: ByRoleOptions,
136-
) => HTMLElement | null
136+
) => T | null
137137

138-
export type FindByRole = (
138+
export type FindByRole<T extends HTMLElement = HTMLElement> = (
139139
container: HTMLElement,
140140
role: ByRoleMatcher,
141141
options?: ByRoleOptions,
142142
waitForElementOptions?: waitForOptions,
143-
) => Promise<HTMLElement>
143+
) => Promise<T>
144144

145-
export type FindAllByRole = (
145+
export type FindAllByRole<T extends HTMLElement = HTMLElement> = (
146146
container: HTMLElement,
147147
role: ByRoleMatcher,
148148
options?: ByRoleOptions,
149149
waitForElementOptions?: waitForOptions,
150-
) => Promise<HTMLElement[]>
151-
152-
export const getByLabelText: GetByText
153-
export const getAllByLabelText: AllByText
154-
export const queryByLabelText: QueryByText
155-
export const queryAllByLabelText: AllByText
156-
export const findByLabelText: FindByText
157-
export const findAllByLabelText: FindAllByText
158-
export const getByPlaceholderText: GetByBoundAttribute
159-
export const getAllByPlaceholderText: AllByBoundAttribute
160-
export const queryByPlaceholderText: QueryByBoundAttribute
161-
export const queryAllByPlaceholderText: AllByBoundAttribute
162-
export const findByPlaceholderText: FindByBoundAttribute
163-
export const findAllByPlaceholderText: FindAllByBoundAttribute
164-
export const getByText: GetByText
165-
export const getAllByText: AllByText
166-
export const queryByText: QueryByText
167-
export const queryAllByText: AllByText
168-
export const findByText: FindByText
169-
export const findAllByText: FindAllByText
170-
export const getByAltText: GetByBoundAttribute
171-
export const getAllByAltText: AllByBoundAttribute
172-
export const queryByAltText: QueryByBoundAttribute
173-
export const queryAllByAltText: AllByBoundAttribute
174-
export const findByAltText: FindByBoundAttribute
175-
export const findAllByAltText: FindAllByBoundAttribute
176-
export const getByTitle: GetByBoundAttribute
177-
export const getAllByTitle: AllByBoundAttribute
178-
export const queryByTitle: QueryByBoundAttribute
179-
export const queryAllByTitle: AllByBoundAttribute
180-
export const findByTitle: FindByBoundAttribute
181-
export const findAllByTitle: FindAllByBoundAttribute
182-
export const getByDisplayValue: GetByBoundAttribute
183-
export const getAllByDisplayValue: AllByBoundAttribute
184-
export const queryByDisplayValue: QueryByBoundAttribute
185-
export const queryAllByDisplayValue: AllByBoundAttribute
186-
export const findByDisplayValue: FindByBoundAttribute
187-
export const findAllByDisplayValue: FindAllByBoundAttribute
188-
export const getByRole: GetByRole
189-
export const getAllByRole: AllByRole
190-
export const queryByRole: QueryByRole
191-
export const queryAllByRole: AllByRole
192-
export const findByRole: FindByRole
193-
export const findAllByRole: FindAllByRole
194-
export const getByTestId: GetByBoundAttribute
195-
export const getAllByTestId: AllByBoundAttribute
196-
export const queryByTestId: QueryByBoundAttribute
197-
export const queryAllByTestId: AllByBoundAttribute
198-
export const findByTestId: FindByBoundAttribute
199-
export const findAllByTestId: FindAllByBoundAttribute
150+
) => Promise<T[]>
151+
152+
export function getByLabelText<T extends HTMLElement = HTMLElement>(
153+
...args: Parameters<GetByText<T>>
154+
): ReturnType<GetByText<T>>
155+
export function getAllByLabelText<T extends HTMLElement = HTMLElement>(
156+
...args: Parameters<AllByText<T>>
157+
): ReturnType<AllByText<T>>
158+
export function queryByLabelText<T extends HTMLElement = HTMLElement>(
159+
...args: Parameters<QueryByText<T>>
160+
): ReturnType<QueryByText<T>>
161+
export function queryAllByLabelText<T extends HTMLElement = HTMLElement>(
162+
...args: Parameters<AllByText<T>>
163+
): ReturnType<AllByText<T>>
164+
export function findByLabelText<T extends HTMLElement = HTMLElement>(
165+
...args: Parameters<FindByText<T>>
166+
): ReturnType<FindByText<T>>
167+
export function findAllByLabelText<T extends HTMLElement = HTMLElement>(
168+
...args: Parameters<FindAllByText<T>>
169+
): ReturnType<FindAllByText<T>>
170+
export function getByPlaceholderText<T extends HTMLElement = HTMLElement>(
171+
...args: Parameters<GetByBoundAttribute<T>>
172+
): ReturnType<GetByBoundAttribute<T>>
173+
export function getAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
174+
...args: Parameters<AllByBoundAttribute<T>>
175+
): ReturnType<AllByBoundAttribute<T>>
176+
export function queryByPlaceholderText<T extends HTMLElement = HTMLElement>(
177+
...args: Parameters<QueryByBoundAttribute<T>>
178+
): ReturnType<QueryByBoundAttribute<T>>
179+
export function queryAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
180+
...args: Parameters<AllByBoundAttribute<T>>
181+
): ReturnType<AllByBoundAttribute<T>>
182+
export function findByPlaceholderText<T extends HTMLElement = HTMLElement>(
183+
...args: Parameters<FindByBoundAttribute<T>>
184+
): ReturnType<FindByBoundAttribute<T>>
185+
export function findAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
186+
...args: Parameters<FindAllByBoundAttribute<T>>
187+
): ReturnType<FindAllByBoundAttribute<T>>
188+
export function getByText<T extends HTMLElement = HTMLElement>(
189+
...args: Parameters<GetByText<T>>
190+
): ReturnType<GetByText<T>>
191+
export function getAllByText<T extends HTMLElement = HTMLElement>(
192+
...args: Parameters<AllByText<T>>
193+
): ReturnType<AllByText<T>>
194+
export function queryByText<T extends HTMLElement = HTMLElement>(
195+
...args: Parameters<QueryByText<T>>
196+
): ReturnType<QueryByText<T>>
197+
export function queryAllByText<T extends HTMLElement = HTMLElement>(
198+
...args: Parameters<AllByText<T>>
199+
): ReturnType<AllByText<T>>
200+
export function findByText<T extends HTMLElement = HTMLElement>(
201+
...args: Parameters<FindByText<T>>
202+
): ReturnType<FindByText<T>>
203+
export function findAllByText<T extends HTMLElement = HTMLElement>(
204+
...args: Parameters<FindAllByText<T>>
205+
): ReturnType<FindAllByText<T>>
206+
export function getByAltText<T extends HTMLElement = HTMLElement>(
207+
...args: Parameters<GetByBoundAttribute<T>>
208+
): ReturnType<GetByBoundAttribute<T>>
209+
export function getAllByAltText<T extends HTMLElement = HTMLElement>(
210+
...args: Parameters<AllByBoundAttribute<T>>
211+
): ReturnType<AllByBoundAttribute<T>>
212+
export function queryByAltText<T extends HTMLElement = HTMLElement>(
213+
...args: Parameters<QueryByBoundAttribute<T>>
214+
): ReturnType<QueryByBoundAttribute<T>>
215+
export function queryAllByAltText<T extends HTMLElement = HTMLElement>(
216+
...args: Parameters<AllByBoundAttribute<T>>
217+
): ReturnType<AllByBoundAttribute<T>>
218+
export function findByAltText<T extends HTMLElement = HTMLElement>(
219+
...args: Parameters<FindByBoundAttribute<T>>
220+
): ReturnType<FindByBoundAttribute<T>>
221+
export function findAllByAltText<T extends HTMLElement = HTMLElement>(
222+
...args: Parameters<FindAllByBoundAttribute<T>>
223+
): ReturnType<FindAllByBoundAttribute<T>>
224+
export function getByTitle<T extends HTMLElement = HTMLElement>(
225+
...args: Parameters<GetByBoundAttribute<T>>
226+
): ReturnType<GetByBoundAttribute<T>>
227+
export function getAllByTitle<T extends HTMLElement = HTMLElement>(
228+
...args: Parameters<AllByBoundAttribute<T>>
229+
): ReturnType<AllByBoundAttribute<T>>
230+
export function queryByTitle<T extends HTMLElement = HTMLElement>(
231+
...args: Parameters<QueryByBoundAttribute<T>>
232+
): ReturnType<QueryByBoundAttribute<T>>
233+
export function queryAllByTitle<T extends HTMLElement = HTMLElement>(
234+
...args: Parameters<AllByBoundAttribute<T>>
235+
): ReturnType<AllByBoundAttribute<T>>
236+
export function findByTitle<T extends HTMLElement = HTMLElement>(
237+
...args: Parameters<FindByBoundAttribute<T>>
238+
): ReturnType<FindByBoundAttribute<T>>
239+
export function findAllByTitle<T extends HTMLElement = HTMLElement>(
240+
...args: Parameters<FindAllByBoundAttribute<T>>
241+
): ReturnType<FindAllByBoundAttribute<T>>
242+
export function getByDisplayValue<T extends HTMLElement = HTMLElement>(
243+
...args: Parameters<GetByBoundAttribute<T>>
244+
): ReturnType<GetByBoundAttribute<T>>
245+
export function getAllByDisplayValue<T extends HTMLElement = HTMLElement>(
246+
...args: Parameters<AllByBoundAttribute<T>>
247+
): ReturnType<AllByBoundAttribute<T>>
248+
export function queryByDisplayValue<T extends HTMLElement = HTMLElement>(
249+
...args: Parameters<QueryByBoundAttribute<T>>
250+
): ReturnType<QueryByBoundAttribute<T>>
251+
export function queryAllByDisplayValue<T extends HTMLElement = HTMLElement>(
252+
...args: Parameters<AllByBoundAttribute<T>>
253+
): ReturnType<AllByBoundAttribute<T>>
254+
export function findByDisplayValue<T extends HTMLElement = HTMLElement>(
255+
...args: Parameters<FindByBoundAttribute<T>>
256+
): ReturnType<FindByBoundAttribute<T>>
257+
export function findAllByDisplayValue<T extends HTMLElement = HTMLElement>(
258+
...args: Parameters<FindAllByBoundAttribute<T>>
259+
): ReturnType<FindAllByBoundAttribute<T>>
260+
export function getByRole<T extends HTMLElement = HTMLElement>(
261+
...args: Parameters<GetByRole<T>>
262+
): ReturnType<GetByRole<T>>
263+
export function getAllByRole<T extends HTMLElement = HTMLElement>(
264+
...args: Parameters<AllByRole<T>>
265+
): ReturnType<AllByRole<T>>
266+
export function queryByRole<T extends HTMLElement = HTMLElement>(
267+
...args: Parameters<QueryByRole<T>>
268+
): ReturnType<QueryByRole<T>>
269+
export function queryAllByRole<T extends HTMLElement = HTMLElement>(
270+
...args: Parameters<AllByRole<T>>
271+
): ReturnType<AllByRole<T>>
272+
export function findByRole<T extends HTMLElement = HTMLElement>(
273+
...args: Parameters<FindByRole<T>>
274+
): ReturnType<FindByRole<T>>
275+
export function findAllByRole<T extends HTMLElement = HTMLElement>(
276+
...args: Parameters<FindAllByRole<T>>
277+
): ReturnType<FindAllByRole<T>>
278+
export function getByTestId<T extends HTMLElement = HTMLElement>(
279+
...args: Parameters<GetByBoundAttribute<T>>
280+
): ReturnType<GetByBoundAttribute<T>>
281+
export function getAllByTestId<T extends HTMLElement = HTMLElement>(
282+
...args: Parameters<AllByBoundAttribute<T>>
283+
): ReturnType<AllByBoundAttribute<T>>
284+
export function queryByTestId<T extends HTMLElement = HTMLElement>(
285+
...args: Parameters<QueryByBoundAttribute<T>>
286+
): ReturnType<QueryByBoundAttribute<T>>
287+
export function queryAllByTestId<T extends HTMLElement = HTMLElement>(
288+
...args: Parameters<AllByBoundAttribute<T>>
289+
): ReturnType<AllByBoundAttribute<T>>
290+
export function findByTestId<T extends HTMLElement = HTMLElement>(
291+
...args: Parameters<FindByBoundAttribute<T>>
292+
): ReturnType<FindByBoundAttribute<T>>
293+
export function findAllByTestId<T extends HTMLElement = HTMLElement>(
294+
...args: Parameters<FindAllByBoundAttribute<T>>
295+
): ReturnType<FindAllByBoundAttribute<T>>

0 commit comments

Comments
 (0)