Skip to content

Commit c1516d0

Browse files
committed
docs: clarify accessible name docs
1 parent 928b941 commit c1516d0

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

website/docs/14.x/docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ React Native Testing Library consists of following APIs:
1818
- [`renderHook` function](/docs/api/misc/render-hook) - render hooks for testing
1919
- [Async utils](/docs/api/misc/async): `findBy*` queries, `waitFor`, `waitForElementToBeRemoved`
2020
- [Configuration](/docs/api/misc/config): `configure`, `resetToDefaults`
21-
- [Accessibility](/docs/api/misc/accessibility): `isHiddenFromAccessibility`
21+
- [Accessibility](/docs/api/misc/accessibility): accessible name, `isHiddenFromAccessibility`
2222
- [Other](/docs/api/misc/other): `within`, `act`, `cleanup`

website/docs/14.x/docs/api/jest-matchers.mdx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,9 @@ expect(element).toHaveAccessibleName(
179179
)
180180
```
181181

182-
Checks if an element has the specified accessible name. Accepts `string` or `RegExp`, with optional [text match options](/docs/api/queries#text-match-options) like `exact` and `normalizer`.
182+
Checks if an element has the specified [accessible name](/docs/api/misc/accessibility#accessible-name). Accepts `string` or `RegExp`, with optional [text match options](/docs/api/queries#text-match-options) like `exact` and `normalizer`.
183183

184-
The accessible name comes from `aria-labelledby`, `accessibilityLabelledBy`, `aria-label`, and `accessibilityLabel` props. For `Image` elements, the `alt` prop is also used. If none are present, the element's text content is used.
185-
186-
When `accessibilityLabelledBy` references multiple elements with an array, their text content is joined with spaces in the referenced order and matched as a single accessible name. `aria-labelledby` follows React Native's single `nativeID` value behavior.
184+
See [Accessible name](/docs/api/misc/accessibility#accessible-name) for how the accessible name is derived from an element's label props and text content.
187185

188186
Without a `name` parameter (or with `undefined`), it only checks whether the element has any accessible name.
189187

website/docs/14.x/docs/api/misc/accessibility.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
11
# Accessibility
22

3+
## Accessible name {#accessible-name}
4+
5+
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.
6+
7+
Testing Library uses the accessible name in the [`*ByRole`](/docs/api/queries#by-role) queries `name` option and in the [`toHaveAccessibleName()`](/docs/api/jest-matchers#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.
8+
9+
### How the accessible name is computed {#how-it-is-computed}
10+
11+
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.
12+
13+
1. **`aria-labelledby` / `accessibilityLabelledBy`**: the [`nativeID`](https://reactnative.dev/docs/view#nativeid) (or array of IDs) of other elements, whose text content is joined with spaces in the referenced order.
14+
2. **`aria-label` / `accessibilityLabel`**: an explicit label string on the element.
15+
3. **`alt`** (`Image` only): the image's alternative text.
16+
4. **`placeholder`** (`TextInput` only).
17+
5. **Text content**: the element's own text, collected recursively from its children (see [Text content](#text-content) below). This is the fallback when none of the label sources above are set.
18+
19+
### Text content {#text-content}
20+
21+
When falling back to text content, child text is joined as follows:
22+
23+
- Adjacent inline text (directly inside `Text` elements) is concatenated **without** a separator, matching how it renders on screen.
24+
- Text from separate, non-inline elements, such as sibling `View`s, is joined with a **single space**.
25+
26+
For example:
27+
28+
```jsx
29+
render(
30+
<Pressable accessibilityRole="button">
31+
<Text>Hello</Text>
32+
<Text> World</Text>
33+
</Pressable>,
34+
);
35+
// Accessible name: "Hello World"
36+
```
37+
38+
### Examples {#accessible-name-examples}
39+
40+
```jsx
41+
// Explicit label wins over text content
42+
<Pressable accessibilityRole="button" accessibilityLabel="Close dialog">
43+
<Text>X</Text>
44+
</Pressable>
45+
// Accessible name: "Close dialog"
46+
47+
// Text content is used when no explicit label is set
48+
<Pressable accessibilityRole="button">
49+
<Text>Submit</Text>
50+
</Pressable>
51+
// Accessible name: "Submit"
52+
```
53+
354
## `isHiddenFromAccessibility`
455

556
```ts

website/docs/14.x/docs/api/queries.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ const element3 = screen.getByRole('button', { name: 'Hello', disabled: true });
192192

193193
#### Options {#by-role-options}
194194

195-
- `name`: Finds an element with given `role`/`accessibilityRole` and an accessible name (= accessability label or text content).
195+
- `name`: Finds an element with given `role`/`accessibilityRole` and a matching [accessible name](/docs/api/misc/accessibility#accessible-name). The accessible name is the text a screen reader would announce for the element, derived from its label props or text content. See [Accessible name](/docs/api/misc/accessibility#accessible-name) for the details.
196196

197197
- `disabled`: You can filter elements by their disabled state (coming either from `aria-disabled` prop or `accessbilityState.disabled` prop). The possible values are `true` or `false`. Querying `disabled: false` will also match elements with `disabled: undefined` (see the [wiki](https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State) for more details).
198198
- See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `disabled` state.

0 commit comments

Comments
 (0)