From f4db6d23c5d40e312f750f8cc24266b440a87980 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Fri, 3 Jul 2020 07:43:50 +0200 Subject: [PATCH 1/5] test: reproduce issue --- types/__tests__/type-tests.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/types/__tests__/type-tests.ts b/types/__tests__/type-tests.ts index 32663fd4..fcadbb29 100644 --- a/types/__tests__/type-tests.ts +++ b/types/__tests__/type-tests.ts @@ -116,6 +116,9 @@ async function testByRole() { // allow to query for a role that isn't included in the types console.assert(queryByRole(element, 'foo') === null) + console.assert(queryByRole(element, /foo/) === null) + console.assert(screen.queryByRole('foo') === null) + console.assert(screen.queryByRole(/foo/) === null) } function testA11yHelper() { From ea7a71db45aa8e6ea7f5bdcad17777c9c994e2fb Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Fri, 3 Jul 2020 19:15:43 +0200 Subject: [PATCH 2/5] fix: by role types --- types/get-queries-for-element.d.ts | 198 +++++++++++++++++++++++++---- types/matches.d.ts | 26 ++++ types/queries.d.ts | 28 +--- types/screen.d.ts | 29 ++--- 4 files changed, 216 insertions(+), 65 deletions(-) diff --git a/types/get-queries-for-element.d.ts b/types/get-queries-for-element.d.ts index b93adfe1..24051a72 100644 --- a/types/get-queries-for-element.d.ts +++ b/types/get-queries-for-element.d.ts @@ -1,29 +1,181 @@ -import * as queries from './queries'; - -export type BoundFunction = T extends ( - attribute: string, - element: HTMLElement, - text: infer P, - options: infer Q, -) => infer R - ? (text: P, options?: Q) => R - : T extends (a1: any, text: infer P, options: infer Q, waitForElementOptions: infer W) => infer R - ? (text: P, options?: Q, waitForElementOptions?: W) => R - : T extends (a1: any, text: infer P, options: infer Q) => infer R - ? (text: P, options?: Q) => R - : never; -export type BoundFunctions = { [P in keyof T]: BoundFunction }; +import * as queries from './queries' +import {Matcher, ByRoleOptions, MatcherOptions} from './matches' +import {waitForOptions} from './wait-for' +import {ARIARole} from 'aria-query' +import {SelectorMatcherOptions} from './query-helpers' + +export type QueryByBoundAttributeForElement = ( + id: Matcher, + options?: MatcherOptions, +) => HTMLElement | null + +export type AllByBoundAttributeForElement = ( + id: Matcher, + options?: MatcherOptions, +) => HTMLElement[] + +export type FindAllByBoundAttributeForElement = ( + id: Matcher, + options?: MatcherOptions, + waitForElementOptions?: waitForOptions, +) => Promise + +export type GetByBoundAttributeForElement = ( + id: Matcher, + options?: MatcherOptions, +) => HTMLElement + +export type FindByBoundAttributeForElement = ( + id: Matcher, + options?: MatcherOptions, + waitForElementOptions?: waitForOptions, +) => Promise + +export type QueryByTextForElement = ( + id: Matcher, + options?: SelectorMatcherOptions, +) => HTMLElement | null + +export type AllByTextForElement = ( + id: Matcher, + options?: SelectorMatcherOptions, +) => HTMLElement[] + +export type FindAllByTextForElement = ( + id: Matcher, + options?: SelectorMatcherOptions, + waitForElementOptions?: waitForOptions, +) => Promise + +export type GetByTextForElement = ( + id: Matcher, + options?: SelectorMatcherOptions, +) => HTMLElement + +export type FindByTextForElement = ( + id: Matcher, + options?: SelectorMatcherOptions, + waitForElementOptions?: waitForOptions, +) => Promise + +// disable unified-signatures to have intellisense for aria roles +/* tslint:disable:unified-signatures */ +export function AllByRoleForElement( + role: Matcher, + options?: ByRoleOptions, +): HTMLElement[] +export function AllByRoleForElement( + role: ARIARole, + options?: ByRoleOptions, +): HTMLElement[] + +export function GetByRoleForElement( + role: Matcher, + options?: ByRoleOptions, +): HTMLElement +export function GetByRoleForElement( + role: ARIARole, + options?: ByRoleOptions, +): HTMLElement + +export function QueryByRoleForElement( + role: Matcher | ByRoleOptions, + options?: ByRoleOptions, +): HTMLElement | null +export function QueryByRoleForElement( + role: ARIARole, + options?: ByRoleOptions, +): HTMLElement | null + +export function FindByRoleForElement( + role: Matcher, + options?: ByRoleOptions, + waitForElementOptions?: waitForOptions, +): Promise +export function FindByRoleForElement( + role: ARIARole, + options?: ByRoleOptions, + waitForElementOptions?: waitForOptions, +): Promise + +export function FindAllByRoleForElement( + role: Matcher, + options?: ByRoleOptions, + waitForElementOptions?: waitForOptions, +): Promise +export function FindAllByRoleForElement( + role: Matcher, + options?: ByRoleOptions, + waitForElementOptions?: waitForOptions, +): Promise + +export interface BoundFunctions { + getByLabelText: GetByTextForElement + getAllByLabelText: AllByTextForElement + queryByLabelText: QueryByTextForElement + queryAllByLabelText: AllByTextForElement + findByLabelText: FindByTextForElement + findAllByLabelText: FindAllByTextForElement + getByPlaceholderText: GetByBoundAttributeForElement + getAllByPlaceholderText: AllByBoundAttributeForElement + queryByPlaceholderText: QueryByBoundAttributeForElement + queryAllByPlaceholderText: AllByBoundAttributeForElement + findByPlaceholderText: FindByBoundAttributeForElement + findAllByPlaceholderText: FindAllByBoundAttributeForElement + getByText: GetByTextForElement + getAllByText: AllByTextForElement + queryByText: QueryByTextForElement + queryAllByText: AllByTextForElement + findByText: FindByTextForElement + findAllByText: FindAllByTextForElement + getByAltText: GetByBoundAttributeForElement + getAllByAltText: AllByBoundAttributeForElement + queryByAltText: QueryByBoundAttributeForElement + queryAllByAltText: AllByBoundAttributeForElement + findByAltText: FindByBoundAttributeForElement + findAllByAltText: FindAllByBoundAttributeForElement + getByTitle: GetByBoundAttributeForElement + getAllByTitle: AllByBoundAttributeForElement + queryByTitle: QueryByBoundAttributeForElement + queryAllByTitle: AllByBoundAttributeForElement + findByTitle: FindByBoundAttributeForElement + findAllByTitle: FindAllByBoundAttributeForElement + getByDisplayValue: GetByBoundAttributeForElement + getAllByDisplayValue: AllByBoundAttributeForElement + queryByDisplayValue: QueryByBoundAttributeForElement + queryAllByDisplayValue: AllByBoundAttributeForElement + findByDisplayValue: FindByBoundAttributeForElement + findAllByDisplayValue: FindAllByBoundAttributeForElement + getByRole: typeof GetByRoleForElement + getAllByRole: typeof AllByRoleForElement + queryByRole: typeof QueryByRoleForElement + queryAllByRole: typeof AllByRoleForElement + findByRole: typeof FindByRoleForElement + findAllByRole: typeof FindAllByRoleForElement + getByTestId: GetByBoundAttributeForElement + getAllByTestId: AllByBoundAttributeForElement + queryByTestId: QueryByBoundAttributeForElement + queryAllByTestId: AllByBoundAttributeForElement + findByTestId: FindByBoundAttributeForElement + findAllByTestId: FindAllByBoundAttributeForElement +} export type Query = ( - container: HTMLElement, - ...args: any[] -) => Error | Promise | Promise | HTMLElement[] | HTMLElement | null; + container: HTMLElement, + ...args: any[] +) => + | Error + | Promise + | Promise + | HTMLElement[] + | HTMLElement + | null export interface Queries { - [T: string]: Query; + [T: string]: Query } -export function getQueriesForElement( - element: HTMLElement, - queriesToBind?: T, -): BoundFunctions; +export function getQueriesForElement( + element: HTMLElement, + queriesToBind?: typeof queries, +): BoundFunctions diff --git a/types/matches.d.ts b/types/matches.d.ts index 6454c86a..5b89a104 100644 --- a/types/matches.d.ts +++ b/types/matches.d.ts @@ -14,6 +14,32 @@ export interface MatcherOptions { suggest?: boolean } +export interface ByRoleOptions extends MatcherOptions { + /** + * If true includes elements in the query set that are usually excluded from + * the accessibility tree. `role="none"` or `role="presentation"` are included + * in either case. + */ + hidden?: boolean + /** + * If true only includes elements in the query set that are marked as + * selected in the accessibility tree, i.e., `aria-selected="true"` + */ + selected?: boolean + /** + * Includes every role used in the `role` attribute + * For example *ByRole('progressbar', {queryFallbacks: true})` will find
`. + */ + queryFallbacks?: boolean + /** + * Only considers elements with the specified accessible name. + */ + name?: + | string + | RegExp + | ((accessibleName: string, element: Element) => boolean) +} + export type Match = ( textToMatch: string, node: HTMLElement | null, diff --git a/types/queries.d.ts b/types/queries.d.ts index cdee32da..0fd62338 100644 --- a/types/queries.d.ts +++ b/types/queries.d.ts @@ -1,4 +1,4 @@ -import {Matcher, MatcherOptions} from './matches' +import {Matcher, MatcherOptions, ByRoleOptions} from './matches' import {SelectorMatcherOptions} from './query-helpers' import {waitForOptions} from './wait-for' import {ARIARole} from 'aria-query' @@ -67,32 +67,6 @@ export type FindByText = ( waitForElementOptions?: waitForOptions, ) => Promise -export interface ByRoleOptions extends MatcherOptions { - /** - * If true includes elements in the query set that are usually excluded from - * the accessibility tree. `role="none"` or `role="presentation"` are included - * in either case. - */ - hidden?: boolean - /** - * If true only includes elements in the query set that are marked as - * selected in the accessibility tree, i.e., `aria-selected="true"` - */ - selected?: boolean - /** - * Includes every role used in the `role` attribute - * For example *ByRole('progressbar', {queryFallbacks: true})` will find
`. - */ - queryFallbacks?: boolean - /** - * Only considers elements with the specified accessible name. - */ - name?: - | string - | RegExp - | ((accessibleName: string, element: Element) => boolean) -} - // disable unified-signatures to have intellisense for aria roles /* tslint:disable:unified-signatures */ export function AllByRole( diff --git a/types/screen.d.ts b/types/screen.d.ts index 2594f5be..45cade06 100644 --- a/types/screen.d.ts +++ b/types/screen.d.ts @@ -1,17 +1,16 @@ -import { BoundFunctions, Queries } from './get-queries-for-element'; -import * as queries from './queries'; -import { OptionsReceived } from 'pretty-format'; +import {BoundFunctions, Queries} from './get-queries-for-element' +import {OptionsReceived} from 'pretty-format' -export type Screen = BoundFunctions & { - /** - * Convenience function for `pretty-dom` which also allows an array - * of elements - */ - debug: ( - element?: Element | HTMLDocument | Array, - maxLength?: number, - options?: OptionsReceived, - ) => void; -}; +export type Screen = BoundFunctions & { + /** + * Convenience function for `pretty-dom` which also allows an array + * of elements + */ + debug: ( + element?: Element | HTMLDocument | Array, + maxLength?: number, + options?: OptionsReceived, + ) => void +} -export const screen: Screen; +export const screen: Screen From 8391d49ae09e35b9c5093b0941465afaf121e798 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Tue, 7 Jul 2020 18:50:35 +0200 Subject: [PATCH 3/5] revert "fix: by role types" This reverts commit 3938e9224eb7f3504d2cf9488712da49e9a558d5. --- types/get-queries-for-element.d.ts | 198 ++++------------------------- types/matches.d.ts | 26 ---- types/queries.d.ts | 28 +++- types/screen.d.ts | 29 +++-- 4 files changed, 65 insertions(+), 216 deletions(-) diff --git a/types/get-queries-for-element.d.ts b/types/get-queries-for-element.d.ts index 24051a72..b93adfe1 100644 --- a/types/get-queries-for-element.d.ts +++ b/types/get-queries-for-element.d.ts @@ -1,181 +1,29 @@ -import * as queries from './queries' -import {Matcher, ByRoleOptions, MatcherOptions} from './matches' -import {waitForOptions} from './wait-for' -import {ARIARole} from 'aria-query' -import {SelectorMatcherOptions} from './query-helpers' - -export type QueryByBoundAttributeForElement = ( - id: Matcher, - options?: MatcherOptions, -) => HTMLElement | null - -export type AllByBoundAttributeForElement = ( - id: Matcher, - options?: MatcherOptions, -) => HTMLElement[] - -export type FindAllByBoundAttributeForElement = ( - id: Matcher, - options?: MatcherOptions, - waitForElementOptions?: waitForOptions, -) => Promise - -export type GetByBoundAttributeForElement = ( - id: Matcher, - options?: MatcherOptions, -) => HTMLElement - -export type FindByBoundAttributeForElement = ( - id: Matcher, - options?: MatcherOptions, - waitForElementOptions?: waitForOptions, -) => Promise - -export type QueryByTextForElement = ( - id: Matcher, - options?: SelectorMatcherOptions, -) => HTMLElement | null - -export type AllByTextForElement = ( - id: Matcher, - options?: SelectorMatcherOptions, -) => HTMLElement[] - -export type FindAllByTextForElement = ( - id: Matcher, - options?: SelectorMatcherOptions, - waitForElementOptions?: waitForOptions, -) => Promise - -export type GetByTextForElement = ( - id: Matcher, - options?: SelectorMatcherOptions, -) => HTMLElement - -export type FindByTextForElement = ( - id: Matcher, - options?: SelectorMatcherOptions, - waitForElementOptions?: waitForOptions, -) => Promise - -// disable unified-signatures to have intellisense for aria roles -/* tslint:disable:unified-signatures */ -export function AllByRoleForElement( - role: Matcher, - options?: ByRoleOptions, -): HTMLElement[] -export function AllByRoleForElement( - role: ARIARole, - options?: ByRoleOptions, -): HTMLElement[] - -export function GetByRoleForElement( - role: Matcher, - options?: ByRoleOptions, -): HTMLElement -export function GetByRoleForElement( - role: ARIARole, - options?: ByRoleOptions, -): HTMLElement - -export function QueryByRoleForElement( - role: Matcher | ByRoleOptions, - options?: ByRoleOptions, -): HTMLElement | null -export function QueryByRoleForElement( - role: ARIARole, - options?: ByRoleOptions, -): HTMLElement | null - -export function FindByRoleForElement( - role: Matcher, - options?: ByRoleOptions, - waitForElementOptions?: waitForOptions, -): Promise -export function FindByRoleForElement( - role: ARIARole, - options?: ByRoleOptions, - waitForElementOptions?: waitForOptions, -): Promise - -export function FindAllByRoleForElement( - role: Matcher, - options?: ByRoleOptions, - waitForElementOptions?: waitForOptions, -): Promise -export function FindAllByRoleForElement( - role: Matcher, - options?: ByRoleOptions, - waitForElementOptions?: waitForOptions, -): Promise - -export interface BoundFunctions { - getByLabelText: GetByTextForElement - getAllByLabelText: AllByTextForElement - queryByLabelText: QueryByTextForElement - queryAllByLabelText: AllByTextForElement - findByLabelText: FindByTextForElement - findAllByLabelText: FindAllByTextForElement - getByPlaceholderText: GetByBoundAttributeForElement - getAllByPlaceholderText: AllByBoundAttributeForElement - queryByPlaceholderText: QueryByBoundAttributeForElement - queryAllByPlaceholderText: AllByBoundAttributeForElement - findByPlaceholderText: FindByBoundAttributeForElement - findAllByPlaceholderText: FindAllByBoundAttributeForElement - getByText: GetByTextForElement - getAllByText: AllByTextForElement - queryByText: QueryByTextForElement - queryAllByText: AllByTextForElement - findByText: FindByTextForElement - findAllByText: FindAllByTextForElement - getByAltText: GetByBoundAttributeForElement - getAllByAltText: AllByBoundAttributeForElement - queryByAltText: QueryByBoundAttributeForElement - queryAllByAltText: AllByBoundAttributeForElement - findByAltText: FindByBoundAttributeForElement - findAllByAltText: FindAllByBoundAttributeForElement - getByTitle: GetByBoundAttributeForElement - getAllByTitle: AllByBoundAttributeForElement - queryByTitle: QueryByBoundAttributeForElement - queryAllByTitle: AllByBoundAttributeForElement - findByTitle: FindByBoundAttributeForElement - findAllByTitle: FindAllByBoundAttributeForElement - getByDisplayValue: GetByBoundAttributeForElement - getAllByDisplayValue: AllByBoundAttributeForElement - queryByDisplayValue: QueryByBoundAttributeForElement - queryAllByDisplayValue: AllByBoundAttributeForElement - findByDisplayValue: FindByBoundAttributeForElement - findAllByDisplayValue: FindAllByBoundAttributeForElement - getByRole: typeof GetByRoleForElement - getAllByRole: typeof AllByRoleForElement - queryByRole: typeof QueryByRoleForElement - queryAllByRole: typeof AllByRoleForElement - findByRole: typeof FindByRoleForElement - findAllByRole: typeof FindAllByRoleForElement - getByTestId: GetByBoundAttributeForElement - getAllByTestId: AllByBoundAttributeForElement - queryByTestId: QueryByBoundAttributeForElement - queryAllByTestId: AllByBoundAttributeForElement - findByTestId: FindByBoundAttributeForElement - findAllByTestId: FindAllByBoundAttributeForElement -} +import * as queries from './queries'; + +export type BoundFunction = T extends ( + attribute: string, + element: HTMLElement, + text: infer P, + options: infer Q, +) => infer R + ? (text: P, options?: Q) => R + : T extends (a1: any, text: infer P, options: infer Q, waitForElementOptions: infer W) => infer R + ? (text: P, options?: Q, waitForElementOptions?: W) => R + : T extends (a1: any, text: infer P, options: infer Q) => infer R + ? (text: P, options?: Q) => R + : never; +export type BoundFunctions = { [P in keyof T]: BoundFunction }; export type Query = ( - container: HTMLElement, - ...args: any[] -) => - | Error - | Promise - | Promise - | HTMLElement[] - | HTMLElement - | null + container: HTMLElement, + ...args: any[] +) => Error | Promise | Promise | HTMLElement[] | HTMLElement | null; export interface Queries { - [T: string]: Query + [T: string]: Query; } -export function getQueriesForElement( - element: HTMLElement, - queriesToBind?: typeof queries, -): BoundFunctions +export function getQueriesForElement( + element: HTMLElement, + queriesToBind?: T, +): BoundFunctions; diff --git a/types/matches.d.ts b/types/matches.d.ts index 5b89a104..6454c86a 100644 --- a/types/matches.d.ts +++ b/types/matches.d.ts @@ -14,32 +14,6 @@ export interface MatcherOptions { suggest?: boolean } -export interface ByRoleOptions extends MatcherOptions { - /** - * If true includes elements in the query set that are usually excluded from - * the accessibility tree. `role="none"` or `role="presentation"` are included - * in either case. - */ - hidden?: boolean - /** - * If true only includes elements in the query set that are marked as - * selected in the accessibility tree, i.e., `aria-selected="true"` - */ - selected?: boolean - /** - * Includes every role used in the `role` attribute - * For example *ByRole('progressbar', {queryFallbacks: true})` will find
`. - */ - queryFallbacks?: boolean - /** - * Only considers elements with the specified accessible name. - */ - name?: - | string - | RegExp - | ((accessibleName: string, element: Element) => boolean) -} - export type Match = ( textToMatch: string, node: HTMLElement | null, diff --git a/types/queries.d.ts b/types/queries.d.ts index 0fd62338..cdee32da 100644 --- a/types/queries.d.ts +++ b/types/queries.d.ts @@ -1,4 +1,4 @@ -import {Matcher, MatcherOptions, ByRoleOptions} from './matches' +import {Matcher, MatcherOptions} from './matches' import {SelectorMatcherOptions} from './query-helpers' import {waitForOptions} from './wait-for' import {ARIARole} from 'aria-query' @@ -67,6 +67,32 @@ export type FindByText = ( waitForElementOptions?: waitForOptions, ) => Promise +export interface ByRoleOptions extends MatcherOptions { + /** + * If true includes elements in the query set that are usually excluded from + * the accessibility tree. `role="none"` or `role="presentation"` are included + * in either case. + */ + hidden?: boolean + /** + * If true only includes elements in the query set that are marked as + * selected in the accessibility tree, i.e., `aria-selected="true"` + */ + selected?: boolean + /** + * Includes every role used in the `role` attribute + * For example *ByRole('progressbar', {queryFallbacks: true})` will find
`. + */ + queryFallbacks?: boolean + /** + * Only considers elements with the specified accessible name. + */ + name?: + | string + | RegExp + | ((accessibleName: string, element: Element) => boolean) +} + // disable unified-signatures to have intellisense for aria roles /* tslint:disable:unified-signatures */ export function AllByRole( diff --git a/types/screen.d.ts b/types/screen.d.ts index 45cade06..2594f5be 100644 --- a/types/screen.d.ts +++ b/types/screen.d.ts @@ -1,16 +1,17 @@ -import {BoundFunctions, Queries} from './get-queries-for-element' -import {OptionsReceived} from 'pretty-format' +import { BoundFunctions, Queries } from './get-queries-for-element'; +import * as queries from './queries'; +import { OptionsReceived } from 'pretty-format'; -export type Screen = BoundFunctions & { - /** - * Convenience function for `pretty-dom` which also allows an array - * of elements - */ - debug: ( - element?: Element | HTMLDocument | Array, - maxLength?: number, - options?: OptionsReceived, - ) => void -} +export type Screen = BoundFunctions & { + /** + * Convenience function for `pretty-dom` which also allows an array + * of elements + */ + debug: ( + element?: Element | HTMLDocument | Array, + maxLength?: number, + options?: OptionsReceived, + ) => void; +}; -export const screen: Screen +export const screen: Screen; From 1582c7327961e2db4c2c72d30bcce49a2c9d1f2f Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Tue, 7 Jul 2020 18:57:16 +0200 Subject: [PATCH 4/5] fix(types): allow bound function to accept string and regex matcher --- types/matches.d.ts | 9 ++++++ types/queries.d.ts | 75 ++++++++++++++-------------------------------- 2 files changed, 31 insertions(+), 53 deletions(-) diff --git a/types/matches.d.ts b/types/matches.d.ts index 6454c86a..419ec51a 100644 --- a/types/matches.d.ts +++ b/types/matches.d.ts @@ -1,6 +1,15 @@ +import {ARIARole} from 'aria-query' + export type MatcherFunction = (content: string, element: HTMLElement) => boolean export type Matcher = string | RegExp | MatcherFunction +// Allow `String` to have intellisense for roles while writing ByRole queries +// If `string` is used it overrules the ARIARole string union types +// resulting in no intellisense +// We still want to support roles not included in the union type +/* tslint:disable:ban-types */ +export type ByRoleMatcher = ARIARole | String | RegExp | MatcherFunction + export type NormalizerFn = (text: string) => string export interface MatcherOptions { diff --git a/types/queries.d.ts b/types/queries.d.ts index cdee32da..a60edec2 100644 --- a/types/queries.d.ts +++ b/types/queries.d.ts @@ -1,7 +1,6 @@ -import {Matcher, MatcherOptions} from './matches' +import {Matcher, MatcherOptions, ByRoleMatcher} from './matches' import {SelectorMatcherOptions} from './query-helpers' import {waitForOptions} from './wait-for' -import {ARIARole} from 'aria-query' export type QueryByBoundAttribute = ( container: HTMLElement, @@ -93,67 +92,37 @@ export interface ByRoleOptions extends MatcherOptions { | ((accessibleName: string, element: Element) => boolean) } -// disable unified-signatures to have intellisense for aria roles -/* tslint:disable:unified-signatures */ -export function AllByRole( +export type AllByRole = ( container: HTMLElement, - role: Matcher, + role: ByRoleMatcher, options?: ByRoleOptions, -): HTMLElement[] -export function AllByRole( - container: HTMLElement, - role: ARIARole, - options?: ByRoleOptions, -): HTMLElement[] +) => HTMLElement[] -export function GetByRole( - container: HTMLElement, - role: Matcher, - options?: ByRoleOptions, -): HTMLElement -export function GetByRole( +export type GetByRole = ( container: HTMLElement, - role: ARIARole, + role: ByRoleMatcher, options?: ByRoleOptions, -): HTMLElement +) => HTMLElement -export function QueryByRole( - container: HTMLElement, - role: Matcher | ByRoleOptions, - options?: ByRoleOptions, -): HTMLElement | null -export function QueryByRole( +export type QueryByRole = ( container: HTMLElement, - role: ARIARole, + role: ByRoleMatcher, options?: ByRoleOptions, -): HTMLElement | null +) => HTMLElement | null -export function FindByRole( +export type FindByRole = ( container: HTMLElement, - role: Matcher, + role: ByRoleMatcher, options?: ByRoleOptions, waitForElementOptions?: waitForOptions, -): Promise -export function FindByRole( - container: HTMLElement, - role: ARIARole, - options?: ByRoleOptions, - waitForElementOptions?: waitForOptions, -): Promise +) => Promise -export function FindAllByRole( +export type FindAllByRole = ( container: HTMLElement, - role: Matcher, + role: ByRoleMatcher, options?: ByRoleOptions, waitForElementOptions?: waitForOptions, -): Promise -export function FindAllByRole( - container: HTMLElement, - role: Matcher, - options?: ByRoleOptions, - waitForElementOptions?: waitForOptions, -): Promise -/* tslint:enable */ +) => Promise export const getByLabelText: GetByText export const getAllByLabelText: AllByText @@ -191,12 +160,12 @@ export const queryByDisplayValue: QueryByBoundAttribute export const queryAllByDisplayValue: AllByBoundAttribute export const findByDisplayValue: FindByBoundAttribute export const findAllByDisplayValue: FindAllByBoundAttribute -export const getByRole: typeof GetByRole -export const getAllByRole: typeof AllByRole -export const queryByRole: typeof QueryByRole -export const queryAllByRole: typeof AllByRole -export const findByRole: typeof FindByRole -export const findAllByRole: typeof FindAllByRole +export const getByRole: GetByRole +export const getAllByRole: AllByRole +export const queryByRole: QueryByRole +export const queryAllByRole: AllByRole +export const findByRole: FindByRole +export const findAllByRole: FindAllByRole export const getByTestId: GetByBoundAttribute export const getAllByTestId: AllByBoundAttribute export const queryByTestId: QueryByBoundAttribute From 6637bebe1ed94710bbc33d15ee4d2c5675014c39 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Tue, 7 Jul 2020 22:31:35 +0200 Subject: [PATCH 5/5] refactor: replace String with workaround to allow intellisense --- types/matches.d.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/types/matches.d.ts b/types/matches.d.ts index 419ec51a..86fc007c 100644 --- a/types/matches.d.ts +++ b/types/matches.d.ts @@ -3,12 +3,9 @@ import {ARIARole} from 'aria-query' export type MatcherFunction = (content: string, element: HTMLElement) => boolean export type Matcher = string | RegExp | MatcherFunction -// Allow `String` to have intellisense for roles while writing ByRole queries -// If `string` is used it overrules the ARIARole string union types -// resulting in no intellisense -// We still want to support roles not included in the union type -/* tslint:disable:ban-types */ -export type ByRoleMatcher = ARIARole | String | RegExp | MatcherFunction +// Get autocomplete for ARIARole union types, while still supporting another string +// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972 +export type ByRoleMatcher = ARIARole | (string & {}) | RegExp | MatcherFunction export type NormalizerFn = (text: string) => string