Accessibility
Accessible name
The accessible name is the text that assistive technologies (like screen readers) announce for an element. It answers the question: "what would a screen reader call this element?". For instance, a button showing a trash icon might have the accessible name "Delete", so a screen reader user knows what it does.
Testing Library uses the accessible name in the *ByRole queries name option and in the toHaveAccessibleName() matcher. Combining a role with an accessible name is a semantic query: it finds an element by what it is (button) and what it is called ("Delete"), rather than by its implementation details. This matches how users experience your UI.
How the accessible name is computed
The accessible name is the first of these sources to produce a non-empty value. The sources are ordered from highest to lowest importance. An explicit accessibility label that the developer assigned always takes precedence, and the element's own text content is only used when no such label is present.
aria-labelledby/accessibilityLabelledBy: thenativeID(or array of IDs) of other elements, whose text content is joined with spaces in the referenced order.aria-label/accessibilityLabel: an explicit label string on the element.alt(Imageonly): the image's alternative text.placeholder(TextInputonly).- Text content: the element's own text, collected recursively from its children (see Text content below). This is the fallback when none of the label sources above are set.
Text content
When falling back to text content, child text is joined as follows:
- Adjacent inline text (directly inside
Textelements) is concatenated without a separator, matching how it renders on screen. - Text from separate, non-inline elements, such as sibling
Views, is joined with a single space.
For example:
Examples
isHiddenFromAccessibility
Also available as isInaccessible() alias for React Testing Library compatibility.
Checks if given element is hidden from assistive technology, e.g. screen readers.
Like isInaccessible function from DOM Testing Library this function considers both accessibility elements and presentational elements (regular Views) to be accessible, unless they are hidden in terms of host platform.
This covers only part of ARIA notion of Accessibility Tree, as ARIA excludes both hidden and presentational elements from the Accessibility Tree.
For the scope of this function, element is inaccessible when it, or any of its ancestors, meets any of the following conditions:
- it has
display: nonestyle - it has
aria-hiddenprop set totrue - it has
accessibilityElementsHiddenprop set totrue - it has
importantForAccessibilityprop set tono-hide-descendants - it has sibling host element with either
aria-modaloraccessibilityViewIsModalprop set totrue
Specifying accessible={false}, role="none", accessibilityRole="none", or importantForAccessibility="no" props does not cause the element to become inaccessible.
