Skip to content

Commit 7181579

Browse files
committed
fix(types): allow bound function to accept string and regex matcher
1 parent bae446d commit 7181579

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

types/matches.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
import {ARIARole} from 'aria-query'
2+
13
export type MatcherFunction = (content: string, element: HTMLElement) => boolean
24
export type Matcher = string | RegExp | MatcherFunction
35

6+
// Allow `String` to have intellisense for roles while writing ByRole queries
7+
// If `string` is used it overrules the ARIARole string union types
8+
// resulting in no intellisense
9+
// We still want to support roles not included in the union type
10+
/* tslint:disable:ban-types */
11+
export type ByRoleMatcher = ARIARole | String | RegExp | MatcherFunction
12+
413
export type NormalizerFn = (text: string) => string
514

615
export interface MatcherOptions {

types/queries.d.ts

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {Matcher, MatcherOptions} from './matches'
1+
import {Matcher, MatcherOptions, ByRoleMatcher} from './matches'
22
import {SelectorMatcherOptions} from './query-helpers'
33
import {waitForOptions} from './wait-for'
4-
import {ARIARole} from 'aria-query'
54

65
export type QueryByBoundAttribute = (
76
container: HTMLElement,
@@ -93,67 +92,37 @@ export interface ByRoleOptions extends MatcherOptions {
9392
| ((accessibleName: string, element: Element) => boolean)
9493
}
9594

96-
// disable unified-signatures to have intellisense for aria roles
97-
/* tslint:disable:unified-signatures */
98-
export function AllByRole(
95+
export type AllByRole = (
9996
container: HTMLElement,
100-
role: Matcher,
97+
role: ByRoleMatcher,
10198
options?: ByRoleOptions,
102-
): HTMLElement[]
103-
export function AllByRole(
104-
container: HTMLElement,
105-
role: ARIARole,
106-
options?: ByRoleOptions,
107-
): HTMLElement[]
99+
) => HTMLElement[]
108100

109-
export function GetByRole(
110-
container: HTMLElement,
111-
role: Matcher,
112-
options?: ByRoleOptions,
113-
): HTMLElement
114-
export function GetByRole(
101+
export type GetByRole = (
115102
container: HTMLElement,
116-
role: ARIARole,
103+
role: ByRoleMatcher,
117104
options?: ByRoleOptions,
118-
): HTMLElement
105+
) => HTMLElement
119106

120-
export function QueryByRole(
121-
container: HTMLElement,
122-
role: Matcher | ByRoleOptions,
123-
options?: ByRoleOptions,
124-
): HTMLElement | null
125-
export function QueryByRole(
107+
export type QueryByRole = (
126108
container: HTMLElement,
127-
role: ARIARole,
109+
role: ByRoleMatcher,
128110
options?: ByRoleOptions,
129-
): HTMLElement | null
111+
) => HTMLElement | null
130112

131-
export function FindByRole(
113+
export type FindByRole = (
132114
container: HTMLElement,
133-
role: Matcher,
115+
role: ByRoleMatcher,
134116
options?: ByRoleOptions,
135117
waitForElementOptions?: waitForOptions,
136-
): Promise<HTMLElement>
137-
export function FindByRole(
138-
container: HTMLElement,
139-
role: ARIARole,
140-
options?: ByRoleOptions,
141-
waitForElementOptions?: waitForOptions,
142-
): Promise<HTMLElement>
118+
) => Promise<HTMLElement>
143119

144-
export function FindAllByRole(
120+
export type FindAllByRole = (
145121
container: HTMLElement,
146-
role: Matcher,
122+
role: ByRoleMatcher,
147123
options?: ByRoleOptions,
148124
waitForElementOptions?: waitForOptions,
149-
): Promise<HTMLElement[]>
150-
export function FindAllByRole(
151-
container: HTMLElement,
152-
role: Matcher,
153-
options?: ByRoleOptions,
154-
waitForElementOptions?: waitForOptions,
155-
): Promise<HTMLElement[]>
156-
/* tslint:enable */
125+
) => Promise<HTMLElement[]>
157126

158127
export const getByLabelText: GetByText
159128
export const getAllByLabelText: AllByText
@@ -191,12 +160,12 @@ export const queryByDisplayValue: QueryByBoundAttribute
191160
export const queryAllByDisplayValue: AllByBoundAttribute
192161
export const findByDisplayValue: FindByBoundAttribute
193162
export const findAllByDisplayValue: FindAllByBoundAttribute
194-
export const getByRole: typeof GetByRole
195-
export const getAllByRole: typeof AllByRole
196-
export const queryByRole: typeof QueryByRole
197-
export const queryAllByRole: typeof AllByRole
198-
export const findByRole: typeof FindByRole
199-
export const findAllByRole: typeof FindAllByRole
163+
export const getByRole: GetByRole
164+
export const getAllByRole: AllByRole
165+
export const queryByRole: QueryByRole
166+
export const queryAllByRole: AllByRole
167+
export const findByRole: FindByRole
168+
export const findAllByRole: FindAllByRole
200169
export const getByTestId: GetByBoundAttribute
201170
export const getAllByTestId: AllByBoundAttribute
202171
export const queryByTestId: QueryByBoundAttribute

0 commit comments

Comments
 (0)