Skip to content

Commit 99bc2c0

Browse files
feat: add generics to screen queries (#1034)
Closes #1033
1 parent 669602c commit 99bc2c0

File tree

2 files changed

+173
-15
lines changed

2 files changed

+173
-15
lines changed

types/__tests__/type-tests.ts

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
waitFor,
1010
waitForElementToBeRemoved,
1111
MatcherOptions,
12+
BoundFunctions,
1213
} from '@testing-library/dom'
1314

1415
const {
@@ -38,12 +39,14 @@ export async function testQueries() {
3839

3940
// screen queries
4041
screen.getByText('foo')
42+
screen.getByText<HTMLDivElement>('foo')
4143
screen.queryByText('foo')
4244
await screen.findByText('foo')
4345
await screen.findByText('foo', undefined, {timeout: 10})
4446
screen.debug(screen.getAllByText('bar'))
4547
screen.queryAllByText('bar')
4648
await screen.findAllByText('bar')
49+
await screen.findAllByRole<HTMLButtonElement>('button', {name: 'submit'})
4750
await screen.findAllByText('bar', undefined, {timeout: 10})
4851
}
4952

@@ -88,6 +91,22 @@ export async function testQueryHelpers() {
8891
await findByAutomationId(element, 'id')
8992
}
9093

94+
export function testBoundFunctions() {
95+
const boundfunctions = {} as BoundFunctions<{
96+
customQueryOne: (container: HTMLElement, text: string) => HTMLElement
97+
customQueryTwo: (
98+
container: HTMLElement,
99+
text: string,
100+
text2: string,
101+
) => HTMLElement
102+
customQueryThree: (container: HTMLElement, number: number) => HTMLElement
103+
}>
104+
105+
boundfunctions.customQueryOne('one')
106+
boundfunctions.customQueryTwo('one', 'two')
107+
boundfunctions.customQueryThree(3)
108+
}
109+
91110
export async function testByRole() {
92111
const element = document.createElement('button')
93112
element.setAttribute('aria-hidden', 'true')

types/get-queries-for-element.d.ts

+154-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,162 @@
11
import * as queries from './queries'
22

33
export type BoundFunction<T> = T extends (
4-
attribute: string,
5-
element: HTMLElement,
6-
text: infer P,
7-
options: infer Q,
4+
container: HTMLElement,
5+
...args: infer P
86
) => infer R
9-
? (text: P, options?: Q) => R
10-
: T extends (
11-
a1: any,
12-
text: infer P,
13-
options: infer Q,
14-
waitForElementOptions: infer W,
15-
) => infer R
16-
? (text: P, options?: Q, waitForElementOptions?: W) => R
17-
: T extends (a1: any, text: infer P, options: infer Q) => infer R
18-
? (text: P, options?: Q) => R
7+
? (...args: P) => R
198
: never
20-
export type BoundFunctions<T> = {[P in keyof T]: BoundFunction<T[P]>}
9+
10+
export type BoundFunctions<Q> = Q extends typeof queries
11+
? {
12+
getByLabelText<T extends HTMLElement = HTMLElement>(
13+
...args: Parameters<BoundFunction<queries.GetByText<T>>>
14+
): ReturnType<queries.GetByText<T>>
15+
getAllByLabelText<T extends HTMLElement = HTMLElement>(
16+
...args: Parameters<BoundFunction<queries.AllByText<T>>>
17+
): ReturnType<queries.AllByText<T>>
18+
queryByLabelText<T extends HTMLElement = HTMLElement>(
19+
...args: Parameters<BoundFunction<queries.QueryByText<T>>>
20+
): ReturnType<queries.QueryByText<T>>
21+
queryAllByLabelText<T extends HTMLElement = HTMLElement>(
22+
...args: Parameters<BoundFunction<queries.AllByText<T>>>
23+
): ReturnType<queries.AllByText<T>>
24+
findByLabelText<T extends HTMLElement = HTMLElement>(
25+
...args: Parameters<BoundFunction<queries.FindByText<T>>>
26+
): ReturnType<queries.FindByText<T>>
27+
findAllByLabelText<T extends HTMLElement = HTMLElement>(
28+
...args: Parameters<BoundFunction<queries.FindAllByText<T>>>
29+
): ReturnType<queries.FindAllByText<T>>
30+
getByPlaceholderText<T extends HTMLElement = HTMLElement>(
31+
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
32+
): ReturnType<queries.GetByBoundAttribute<T>>
33+
getAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
34+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
35+
): ReturnType<queries.AllByBoundAttribute<T>>
36+
queryByPlaceholderText<T extends HTMLElement = HTMLElement>(
37+
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
38+
): ReturnType<queries.QueryByBoundAttribute<T>>
39+
queryAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
40+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
41+
): ReturnType<queries.AllByBoundAttribute<T>>
42+
findByPlaceholderText<T extends HTMLElement = HTMLElement>(
43+
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
44+
): ReturnType<queries.FindByBoundAttribute<T>>
45+
findAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
46+
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
47+
): ReturnType<queries.FindAllByBoundAttribute<T>>
48+
getByText<T extends HTMLElement = HTMLElement>(
49+
...args: Parameters<BoundFunction<queries.GetByText<T>>>
50+
): ReturnType<queries.GetByText<T>>
51+
getAllByText<T extends HTMLElement = HTMLElement>(
52+
...args: Parameters<BoundFunction<queries.AllByText<T>>>
53+
): ReturnType<queries.AllByText<T>>
54+
queryByText<T extends HTMLElement = HTMLElement>(
55+
...args: Parameters<BoundFunction<queries.QueryByText<T>>>
56+
): ReturnType<queries.QueryByText<T>>
57+
queryAllByText<T extends HTMLElement = HTMLElement>(
58+
...args: Parameters<BoundFunction<queries.AllByText<T>>>
59+
): ReturnType<queries.AllByText<T>>
60+
findByText<T extends HTMLElement = HTMLElement>(
61+
...args: Parameters<BoundFunction<queries.FindByText<T>>>
62+
): ReturnType<queries.FindByText<T>>
63+
findAllByText<T extends HTMLElement = HTMLElement>(
64+
...args: Parameters<BoundFunction<queries.FindAllByText<T>>>
65+
): ReturnType<queries.FindAllByText<T>>
66+
getByAltText<T extends HTMLElement = HTMLElement>(
67+
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
68+
): ReturnType<queries.GetByBoundAttribute<T>>
69+
getAllByAltText<T extends HTMLElement = HTMLElement>(
70+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
71+
): ReturnType<queries.AllByBoundAttribute<T>>
72+
queryByAltText<T extends HTMLElement = HTMLElement>(
73+
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
74+
): ReturnType<queries.QueryByBoundAttribute<T>>
75+
queryAllByAltText<T extends HTMLElement = HTMLElement>(
76+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
77+
): ReturnType<queries.AllByBoundAttribute<T>>
78+
findByAltText<T extends HTMLElement = HTMLElement>(
79+
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
80+
): ReturnType<queries.FindByBoundAttribute<T>>
81+
findAllByAltText<T extends HTMLElement = HTMLElement>(
82+
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
83+
): ReturnType<queries.FindAllByBoundAttribute<T>>
84+
getByTitle<T extends HTMLElement = HTMLElement>(
85+
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
86+
): ReturnType<queries.GetByBoundAttribute<T>>
87+
getAllByTitle<T extends HTMLElement = HTMLElement>(
88+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
89+
): ReturnType<queries.AllByBoundAttribute<T>>
90+
queryByTitle<T extends HTMLElement = HTMLElement>(
91+
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
92+
): ReturnType<queries.QueryByBoundAttribute<T>>
93+
queryAllByTitle<T extends HTMLElement = HTMLElement>(
94+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
95+
): ReturnType<queries.AllByBoundAttribute<T>>
96+
findByTitle<T extends HTMLElement = HTMLElement>(
97+
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
98+
): ReturnType<queries.FindByBoundAttribute<T>>
99+
findAllByTitle<T extends HTMLElement = HTMLElement>(
100+
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
101+
): ReturnType<queries.FindAllByBoundAttribute<T>>
102+
getByDisplayValue<T extends HTMLElement = HTMLElement>(
103+
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
104+
): ReturnType<queries.GetByBoundAttribute<T>>
105+
getAllByDisplayValue<T extends HTMLElement = HTMLElement>(
106+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
107+
): ReturnType<queries.AllByBoundAttribute<T>>
108+
queryByDisplayValue<T extends HTMLElement = HTMLElement>(
109+
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
110+
): ReturnType<queries.QueryByBoundAttribute<T>>
111+
queryAllByDisplayValue<T extends HTMLElement = HTMLElement>(
112+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
113+
): ReturnType<queries.AllByBoundAttribute<T>>
114+
findByDisplayValue<T extends HTMLElement = HTMLElement>(
115+
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
116+
): ReturnType<queries.FindByBoundAttribute<T>>
117+
findAllByDisplayValue<T extends HTMLElement = HTMLElement>(
118+
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
119+
): ReturnType<queries.FindAllByBoundAttribute<T>>
120+
getByRole<T extends HTMLElement = HTMLElement>(
121+
...args: Parameters<BoundFunction<queries.GetByRole<T>>>
122+
): ReturnType<queries.GetByRole<T>>
123+
getAllByRole<T extends HTMLElement = HTMLElement>(
124+
...args: Parameters<BoundFunction<queries.AllByRole<T>>>
125+
): ReturnType<queries.AllByRole<T>>
126+
queryByRole<T extends HTMLElement = HTMLElement>(
127+
...args: Parameters<BoundFunction<queries.QueryByRole<T>>>
128+
): ReturnType<queries.QueryByRole<T>>
129+
queryAllByRole<T extends HTMLElement = HTMLElement>(
130+
...args: Parameters<BoundFunction<queries.AllByRole<T>>>
131+
): ReturnType<queries.AllByRole<T>>
132+
findByRole<T extends HTMLElement = HTMLElement>(
133+
...args: Parameters<BoundFunction<queries.FindByRole<T>>>
134+
): ReturnType<queries.FindByRole<T>>
135+
findAllByRole<T extends HTMLElement = HTMLElement>(
136+
...args: Parameters<BoundFunction<queries.FindAllByRole<T>>>
137+
): ReturnType<queries.FindAllByRole<T>>
138+
getByTestId<T extends HTMLElement = HTMLElement>(
139+
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
140+
): ReturnType<queries.GetByBoundAttribute<T>>
141+
getAllByTestId<T extends HTMLElement = HTMLElement>(
142+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
143+
): ReturnType<queries.AllByBoundAttribute<T>>
144+
queryByTestId<T extends HTMLElement = HTMLElement>(
145+
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
146+
): ReturnType<queries.QueryByBoundAttribute<T>>
147+
queryAllByTestId<T extends HTMLElement = HTMLElement>(
148+
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
149+
): ReturnType<queries.AllByBoundAttribute<T>>
150+
findByTestId<T extends HTMLElement = HTMLElement>(
151+
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
152+
): ReturnType<queries.FindByBoundAttribute<T>>
153+
findAllByTestId<T extends HTMLElement = HTMLElement>(
154+
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
155+
): ReturnType<queries.FindAllByBoundAttribute<T>>
156+
}
157+
: {
158+
[P in keyof Q]: BoundFunction<Q[P]>
159+
}
21160

22161
export type Query = (
23162
container: HTMLElement,

0 commit comments

Comments
 (0)