From c1dd9cf3e1f0bcb09d2c8c716d5ef8e8d106f199 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 25 Sep 2019 22:46:58 +0200 Subject: [PATCH] feat(testing-library__dom): Add byRole#hidden (#38560) * feat(testing-library__dom): Add byRole#hidden * Update version --- types/testing-library__dom/index.d.ts | 2 +- types/testing-library__dom/queries.d.ts | 42 ++++++++++++++++--- .../testing-library__dom-tests.ts | 10 ++++- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/types/testing-library__dom/index.d.ts b/types/testing-library__dom/index.d.ts index fee7911e8e..bae0c7a7b2 100644 --- a/types/testing-library__dom/index.d.ts +++ b/types/testing-library__dom/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for @testing-library/dom 6.0 +// Type definitions for @testing-library/dom 6.4 // Project: https://github.com/testing-library/dom-testing-library // Definitions by: Alex Krolick // Kent C Dodds diff --git a/types/testing-library__dom/queries.d.ts b/types/testing-library__dom/queries.d.ts index 98d8741a9a..e27cdf5a3d 100644 --- a/types/testing-library__dom/queries.d.ts +++ b/types/testing-library__dom/queries.d.ts @@ -46,6 +46,36 @@ export type FindByText = ( waitForElementOptions?: WaitForElementOptions, ) => 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. + * @default false + */ + hidden?: boolean; +} + +export type AllByRole = (container: HTMLElement, role: Matcher, options?: ByRoleOptions) => HTMLElement[]; + +export type GetByRole = (container: HTMLElement, role: Matcher, options?: ByRoleOptions) => HTMLElement; + +export type QueryByRole = (container: HTMLElement, role: Matcher, options?: ByRoleOptions) => HTMLElement | null; + +export type FindByRole = ( + container: HTMLElement, + role: Matcher, + options?: ByRoleOptions, + waitForElementOptions?: WaitForElementOptions, +) => Promise; + +export type FindAllByRole = ( + container: HTMLElement, + role: Matcher, + options?: ByRoleOptions, + waitForElementOptions?: WaitForElementOptions, +) => Promise; + export const getByLabelText: GetByText; export const getAllByLabelText: AllByText; export const queryByLabelText: QueryByText; @@ -82,12 +112,12 @@ export const queryByDisplayValue: QueryByBoundAttribute; export const queryAllByDisplayValue: AllByBoundAttribute; export const findByDisplayValue: FindByBoundAttribute; export const findAllByDisplayValue: FindAllByBoundAttribute; -export const getByRole: GetByBoundAttribute; -export const getAllByRole: AllByBoundAttribute; -export const queryByRole: QueryByBoundAttribute; -export const queryAllByRole: AllByBoundAttribute; -export const findByRole: FindByBoundAttribute; -export const findAllByRole: FindAllByBoundAttribute; +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; diff --git a/types/testing-library__dom/testing-library__dom-tests.ts b/types/testing-library__dom/testing-library__dom-tests.ts index 635f5babba..072ea21366 100644 --- a/types/testing-library__dom/testing-library__dom-tests.ts +++ b/types/testing-library__dom/testing-library__dom-tests.ts @@ -1,6 +1,6 @@ import { queries } from '@testing-library/dom'; -const { getByText, queryByText, findByText, getAllByText, queryAllByText, findAllByText } = queries; +const { getByText, queryByText, findByText, getAllByText, queryAllByText, findAllByText, queryByRole } = queries; async function testQueries() { const element = document.createElement('div'); @@ -11,3 +11,11 @@ async function testQueries() { queryAllByText(element, 'bar'); await findAllByText(element, 'bar'); } + +function testByRole() { + const element = document.createElement('button'); + element.setAttribute('aria-hidden', 'true'); + + console.assert(queryByRole(element, 'button') === null); + console.assert(queryByRole(element, 'button', { hidden: true }) !== null); +}