Skip to content

Commit 3938e92

Browse files
committed
fix: by role types
1 parent 7ca03f8 commit 3938e92

File tree

4 files changed

+216
-65
lines changed

4 files changed

+216
-65
lines changed

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

Lines changed: 175 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,181 @@
1-
import * as queries from './queries';
2-
3-
export type BoundFunction<T> = T extends (
4-
attribute: string,
5-
element: HTMLElement,
6-
text: infer P,
7-
options: infer Q,
8-
) => infer R
9-
? (text: P, options?: Q) => R
10-
: T extends (a1: any, text: infer P, options: infer Q, waitForElementOptions: infer W) => infer R
11-
? (text: P, options?: Q, waitForElementOptions?: W) => R
12-
: T extends (a1: any, text: infer P, options: infer Q) => infer R
13-
? (text: P, options?: Q) => R
14-
: never;
15-
export type BoundFunctions<T> = { [P in keyof T]: BoundFunction<T[P]> };
1+
import * as queries from './queries'
2+
import {Matcher, ByRoleOptions, MatcherOptions} from './matches'
3+
import {waitForOptions} from './wait-for'
4+
import {ARIARole} from 'aria-query'
5+
import {SelectorMatcherOptions} from './query-helpers'
6+
7+
export type QueryByBoundAttributeForElement = (
8+
id: Matcher,
9+
options?: MatcherOptions,
10+
) => HTMLElement | null
11+
12+
export type AllByBoundAttributeForElement = (
13+
id: Matcher,
14+
options?: MatcherOptions,
15+
) => HTMLElement[]
16+
17+
export type FindAllByBoundAttributeForElement = (
18+
id: Matcher,
19+
options?: MatcherOptions,
20+
waitForElementOptions?: waitForOptions,
21+
) => Promise<HTMLElement[]>
22+
23+
export type GetByBoundAttributeForElement = (
24+
id: Matcher,
25+
options?: MatcherOptions,
26+
) => HTMLElement
27+
28+
export type FindByBoundAttributeForElement = (
29+
id: Matcher,
30+
options?: MatcherOptions,
31+
waitForElementOptions?: waitForOptions,
32+
) => Promise<HTMLElement>
33+
34+
export type QueryByTextForElement = (
35+
id: Matcher,
36+
options?: SelectorMatcherOptions,
37+
) => HTMLElement | null
38+
39+
export type AllByTextForElement = (
40+
id: Matcher,
41+
options?: SelectorMatcherOptions,
42+
) => HTMLElement[]
43+
44+
export type FindAllByTextForElement = (
45+
id: Matcher,
46+
options?: SelectorMatcherOptions,
47+
waitForElementOptions?: waitForOptions,
48+
) => Promise<HTMLElement[]>
49+
50+
export type GetByTextForElement = (
51+
id: Matcher,
52+
options?: SelectorMatcherOptions,
53+
) => HTMLElement
54+
55+
export type FindByTextForElement = (
56+
id: Matcher,
57+
options?: SelectorMatcherOptions,
58+
waitForElementOptions?: waitForOptions,
59+
) => Promise<HTMLElement>
60+
61+
// disable unified-signatures to have intellisense for aria roles
62+
/* tslint:disable:unified-signatures */
63+
export function AllByRoleForElement(
64+
role: Matcher,
65+
options?: ByRoleOptions,
66+
): HTMLElement[]
67+
export function AllByRoleForElement(
68+
role: ARIARole,
69+
options?: ByRoleOptions,
70+
): HTMLElement[]
71+
72+
export function GetByRoleForElement(
73+
role: Matcher,
74+
options?: ByRoleOptions,
75+
): HTMLElement
76+
export function GetByRoleForElement(
77+
role: ARIARole,
78+
options?: ByRoleOptions,
79+
): HTMLElement
80+
81+
export function QueryByRoleForElement(
82+
role: Matcher | ByRoleOptions,
83+
options?: ByRoleOptions,
84+
): HTMLElement | null
85+
export function QueryByRoleForElement(
86+
role: ARIARole,
87+
options?: ByRoleOptions,
88+
): HTMLElement | null
89+
90+
export function FindByRoleForElement(
91+
role: Matcher,
92+
options?: ByRoleOptions,
93+
waitForElementOptions?: waitForOptions,
94+
): Promise<HTMLElement>
95+
export function FindByRoleForElement(
96+
role: ARIARole,
97+
options?: ByRoleOptions,
98+
waitForElementOptions?: waitForOptions,
99+
): Promise<HTMLElement>
100+
101+
export function FindAllByRoleForElement(
102+
role: Matcher,
103+
options?: ByRoleOptions,
104+
waitForElementOptions?: waitForOptions,
105+
): Promise<HTMLElement[]>
106+
export function FindAllByRoleForElement(
107+
role: Matcher,
108+
options?: ByRoleOptions,
109+
waitForElementOptions?: waitForOptions,
110+
): Promise<HTMLElement[]>
111+
112+
export interface BoundFunctions {
113+
getByLabelText: GetByTextForElement
114+
getAllByLabelText: AllByTextForElement
115+
queryByLabelText: QueryByTextForElement
116+
queryAllByLabelText: AllByTextForElement
117+
findByLabelText: FindByTextForElement
118+
findAllByLabelText: FindAllByTextForElement
119+
getByPlaceholderText: GetByBoundAttributeForElement
120+
getAllByPlaceholderText: AllByBoundAttributeForElement
121+
queryByPlaceholderText: QueryByBoundAttributeForElement
122+
queryAllByPlaceholderText: AllByBoundAttributeForElement
123+
findByPlaceholderText: FindByBoundAttributeForElement
124+
findAllByPlaceholderText: FindAllByBoundAttributeForElement
125+
getByText: GetByTextForElement
126+
getAllByText: AllByTextForElement
127+
queryByText: QueryByTextForElement
128+
queryAllByText: AllByTextForElement
129+
findByText: FindByTextForElement
130+
findAllByText: FindAllByTextForElement
131+
getByAltText: GetByBoundAttributeForElement
132+
getAllByAltText: AllByBoundAttributeForElement
133+
queryByAltText: QueryByBoundAttributeForElement
134+
queryAllByAltText: AllByBoundAttributeForElement
135+
findByAltText: FindByBoundAttributeForElement
136+
findAllByAltText: FindAllByBoundAttributeForElement
137+
getByTitle: GetByBoundAttributeForElement
138+
getAllByTitle: AllByBoundAttributeForElement
139+
queryByTitle: QueryByBoundAttributeForElement
140+
queryAllByTitle: AllByBoundAttributeForElement
141+
findByTitle: FindByBoundAttributeForElement
142+
findAllByTitle: FindAllByBoundAttributeForElement
143+
getByDisplayValue: GetByBoundAttributeForElement
144+
getAllByDisplayValue: AllByBoundAttributeForElement
145+
queryByDisplayValue: QueryByBoundAttributeForElement
146+
queryAllByDisplayValue: AllByBoundAttributeForElement
147+
findByDisplayValue: FindByBoundAttributeForElement
148+
findAllByDisplayValue: FindAllByBoundAttributeForElement
149+
getByRole: typeof GetByRoleForElement
150+
getAllByRole: typeof AllByRoleForElement
151+
queryByRole: typeof QueryByRoleForElement
152+
queryAllByRole: typeof AllByRoleForElement
153+
findByRole: typeof FindByRoleForElement
154+
findAllByRole: typeof FindAllByRoleForElement
155+
getByTestId: GetByBoundAttributeForElement
156+
getAllByTestId: AllByBoundAttributeForElement
157+
queryByTestId: QueryByBoundAttributeForElement
158+
queryAllByTestId: AllByBoundAttributeForElement
159+
findByTestId: FindByBoundAttributeForElement
160+
findAllByTestId: FindAllByBoundAttributeForElement
161+
}
16162

17163
export type Query = (
18-
container: HTMLElement,
19-
...args: any[]
20-
) => Error | Promise<HTMLElement[]> | Promise<HTMLElement> | HTMLElement[] | HTMLElement | null;
164+
container: HTMLElement,
165+
...args: any[]
166+
) =>
167+
| Error
168+
| Promise<HTMLElement[]>
169+
| Promise<HTMLElement>
170+
| HTMLElement[]
171+
| HTMLElement
172+
| null
21173

22174
export interface Queries {
23-
[T: string]: Query;
175+
[T: string]: Query
24176
}
25177

26-
export function getQueriesForElement<T extends Queries = typeof queries>(
27-
element: HTMLElement,
28-
queriesToBind?: T,
29-
): BoundFunctions<T>;
178+
export function getQueriesForElement(
179+
element: HTMLElement,
180+
queriesToBind?: typeof queries,
181+
): BoundFunctions

types/matches.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ export interface MatcherOptions {
1414
suggest?: boolean
1515
}
1616

17+
export interface ByRoleOptions extends MatcherOptions {
18+
/**
19+
* If true includes elements in the query set that are usually excluded from
20+
* the accessibility tree. `role="none"` or `role="presentation"` are included
21+
* in either case.
22+
*/
23+
hidden?: boolean
24+
/**
25+
* If true only includes elements in the query set that are marked as
26+
* selected in the accessibility tree, i.e., `aria-selected="true"`
27+
*/
28+
selected?: boolean
29+
/**
30+
* Includes every role used in the `role` attribute
31+
* For example *ByRole('progressbar', {queryFallbacks: true})` will find <div role="meter progressbar">`.
32+
*/
33+
queryFallbacks?: boolean
34+
/**
35+
* Only considers elements with the specified accessible name.
36+
*/
37+
name?:
38+
| string
39+
| RegExp
40+
| ((accessibleName: string, element: Element) => boolean)
41+
}
42+
1743
export type Match = (
1844
textToMatch: string,
1945
node: HTMLElement | null,

types/queries.d.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Matcher, MatcherOptions} from './matches'
1+
import {Matcher, MatcherOptions, ByRoleOptions} from './matches'
22
import {SelectorMatcherOptions} from './query-helpers'
33
import {waitForOptions} from './wait-for'
44
import {ARIARole} from 'aria-query'
@@ -67,32 +67,6 @@ export type FindByText = (
6767
waitForElementOptions?: waitForOptions,
6868
) => Promise<HTMLElement>
6969

70-
export interface ByRoleOptions extends MatcherOptions {
71-
/**
72-
* If true includes elements in the query set that are usually excluded from
73-
* the accessibility tree. `role="none"` or `role="presentation"` are included
74-
* in either case.
75-
*/
76-
hidden?: boolean
77-
/**
78-
* If true only includes elements in the query set that are marked as
79-
* selected in the accessibility tree, i.e., `aria-selected="true"`
80-
*/
81-
selected?: boolean
82-
/**
83-
* Includes every role used in the `role` attribute
84-
* For example *ByRole('progressbar', {queryFallbacks: true})` will find <div role="meter progressbar">`.
85-
*/
86-
queryFallbacks?: boolean
87-
/**
88-
* Only considers elements with the specified accessible name.
89-
*/
90-
name?:
91-
| string
92-
| RegExp
93-
| ((accessibleName: string, element: Element) => boolean)
94-
}
95-
9670
// disable unified-signatures to have intellisense for aria roles
9771
/* tslint:disable:unified-signatures */
9872
export function AllByRole(

types/screen.d.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { BoundFunctions, Queries } from './get-queries-for-element';
2-
import * as queries from './queries';
3-
import { OptionsReceived } from 'pretty-format';
1+
import {BoundFunctions, Queries} from './get-queries-for-element'
2+
import {OptionsReceived} from 'pretty-format'
43

5-
export type Screen<Q extends Queries = typeof queries> = BoundFunctions<Q> & {
6-
/**
7-
* Convenience function for `pretty-dom` which also allows an array
8-
* of elements
9-
*/
10-
debug: (
11-
element?: Element | HTMLDocument | Array<Element | HTMLDocument>,
12-
maxLength?: number,
13-
options?: OptionsReceived,
14-
) => void;
15-
};
4+
export type Screen = BoundFunctions & {
5+
/**
6+
* Convenience function for `pretty-dom` which also allows an array
7+
* of elements
8+
*/
9+
debug: (
10+
element?: Element | HTMLDocument | Array<Element | HTMLDocument>,
11+
maxLength?: number,
12+
options?: OptionsReceived,
13+
) => void
14+
}
1615

17-
export const screen: Screen;
16+
export const screen: Screen

0 commit comments

Comments
 (0)