Skip to content

Commit 62e5eb3

Browse files
authored
Merge pull request #683 from testing-library/pr/680
fix(types): allow bound function to accept string and regex matcher
2 parents fd0d670 + 5d628fa commit 62e5eb3

File tree

3 files changed

+31
-53
lines changed

3 files changed

+31
-53
lines changed

types/__tests__/type-tests.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ async function testByRole() {
116116

117117
// allow to query for a role that isn't included in the types
118118
console.assert(queryByRole(element, 'foo') === null)
119+
console.assert(queryByRole(element, /foo/) === null)
120+
console.assert(screen.queryByRole('foo') === null)
121+
console.assert(screen.queryByRole(/foo/) === null)
119122
}
120123

121124
function testA11yHelper() {

types/matches.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
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+
// Get autocomplete for ARIARole union types, while still supporting another string
7+
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972
8+
export type ByRoleMatcher = ARIARole | (string & {}) | RegExp | MatcherFunction
9+
410
export type NormalizerFn = (text: string) => string
511

612
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,
@@ -98,67 +97,37 @@ export interface ByRoleOptions extends MatcherOptions {
9897
| ((accessibleName: string, element: Element) => boolean)
9998
}
10099

101-
// disable unified-signatures to have intellisense for aria roles
102-
/* tslint:disable:unified-signatures */
103-
export function AllByRole(
100+
export type AllByRole = (
104101
container: HTMLElement,
105-
role: Matcher,
102+
role: ByRoleMatcher,
106103
options?: ByRoleOptions,
107-
): HTMLElement[]
108-
export function AllByRole(
109-
container: HTMLElement,
110-
role: ARIARole,
111-
options?: ByRoleOptions,
112-
): HTMLElement[]
104+
) => HTMLElement[]
113105

114-
export function GetByRole(
115-
container: HTMLElement,
116-
role: Matcher,
117-
options?: ByRoleOptions,
118-
): HTMLElement
119-
export function GetByRole(
106+
export type GetByRole = (
120107
container: HTMLElement,
121-
role: ARIARole,
108+
role: ByRoleMatcher,
122109
options?: ByRoleOptions,
123-
): HTMLElement
110+
) => HTMLElement
124111

125-
export function QueryByRole(
126-
container: HTMLElement,
127-
role: Matcher | ByRoleOptions,
128-
options?: ByRoleOptions,
129-
): HTMLElement | null
130-
export function QueryByRole(
112+
export type QueryByRole = (
131113
container: HTMLElement,
132-
role: ARIARole,
114+
role: ByRoleMatcher,
133115
options?: ByRoleOptions,
134-
): HTMLElement | null
116+
) => HTMLElement | null
135117

136-
export function FindByRole(
118+
export type FindByRole = (
137119
container: HTMLElement,
138-
role: Matcher,
120+
role: ByRoleMatcher,
139121
options?: ByRoleOptions,
140122
waitForElementOptions?: waitForOptions,
141-
): Promise<HTMLElement>
142-
export function FindByRole(
143-
container: HTMLElement,
144-
role: ARIARole,
145-
options?: ByRoleOptions,
146-
waitForElementOptions?: waitForOptions,
147-
): Promise<HTMLElement>
123+
) => Promise<HTMLElement>
148124

149-
export function FindAllByRole(
125+
export type FindAllByRole = (
150126
container: HTMLElement,
151-
role: Matcher,
127+
role: ByRoleMatcher,
152128
options?: ByRoleOptions,
153129
waitForElementOptions?: waitForOptions,
154-
): Promise<HTMLElement[]>
155-
export function FindAllByRole(
156-
container: HTMLElement,
157-
role: Matcher,
158-
options?: ByRoleOptions,
159-
waitForElementOptions?: waitForOptions,
160-
): Promise<HTMLElement[]>
161-
/* tslint:enable */
130+
) => Promise<HTMLElement[]>
162131

163132
export const getByLabelText: GetByText
164133
export const getAllByLabelText: AllByText
@@ -196,12 +165,12 @@ export const queryByDisplayValue: QueryByBoundAttribute
196165
export const queryAllByDisplayValue: AllByBoundAttribute
197166
export const findByDisplayValue: FindByBoundAttribute
198167
export const findAllByDisplayValue: FindAllByBoundAttribute
199-
export const getByRole: typeof GetByRole
200-
export const getAllByRole: typeof AllByRole
201-
export const queryByRole: typeof QueryByRole
202-
export const queryAllByRole: typeof AllByRole
203-
export const findByRole: typeof FindByRole
204-
export const findAllByRole: typeof FindAllByRole
168+
export const getByRole: GetByRole
169+
export const getAllByRole: AllByRole
170+
export const queryByRole: QueryByRole
171+
export const queryAllByRole: AllByRole
172+
export const findByRole: FindByRole
173+
export const findAllByRole: FindAllByRole
205174
export const getByTestId: GetByBoundAttribute
206175
export const getAllByTestId: AllByBoundAttribute
207176
export const queryByTestId: QueryByBoundAttribute

0 commit comments

Comments
 (0)