Skip to content

Commit d849657

Browse files
committed
refactor: a11y label helpers
1 parent f231ea3 commit d849657

File tree

3 files changed

+16
-57
lines changed

3 files changed

+16
-57
lines changed

src/helpers/accessibility.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ export function computeAriaModal(element: ReactTestInstance): boolean | undefine
169169
}
170170

171171
export function computeAriaLabel(element: ReactTestInstance): string | undefined {
172+
const labelElementId = element.props['aria-labelledby'] ?? element.props.accessibilityLabelledBy;
173+
if (labelElementId) {
174+
const rootElement = getUnsafeRootElement(element);
175+
const labelElement = rootElement?.findByProps({ nativeID: labelElementId });
176+
if (labelElement) {
177+
return getTextContent(labelElement);
178+
}
179+
}
180+
172181
const explicitLabel = element.props['aria-label'] ?? element.props.accessibilityLabel;
173182
if (explicitLabel) {
174183
return explicitLabel;
@@ -182,10 +191,6 @@ export function computeAriaLabel(element: ReactTestInstance): string | undefined
182191
return undefined;
183192
}
184193

185-
export function computeAriaLabelledBy(element: ReactTestInstance): string | undefined {
186-
return element.props['aria-labelledby'] ?? element.props.accessibilityLabelledBy;
187-
}
188-
189194
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#busy-state
190195
export function computeAriaBusy({ props }: ReactTestInstance): boolean {
191196
return props['aria-busy'] ?? props.accessibilityState?.busy ?? false;
@@ -245,21 +250,7 @@ export function computeAriaValue(element: ReactTestInstance): AccessibilityValue
245250
}
246251

247252
export function computeAccessibleName(element: ReactTestInstance): string | undefined {
248-
const label = computeAriaLabel(element);
249-
if (label) {
250-
return label;
251-
}
252-
253-
const labelElementId = computeAriaLabelledBy(element);
254-
if (labelElementId) {
255-
const rootElement = getUnsafeRootElement(element);
256-
const labelElement = rootElement?.findByProps({ nativeID: labelElementId });
257-
if (labelElement) {
258-
return getTextContent(labelElement);
259-
}
260-
}
261-
262-
return getTextContent(element);
253+
return computeAriaLabel(element) ?? getTextContent(element);
263254
}
264255

265256
type RoleSupportMap = Partial<Record<Role | AccessibilityRole, true>>;
Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,11 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import { matches, TextMatch, TextMatchOptions } from '../../matches';
3-
import { computeAriaLabel, computeAriaLabelledBy } from '../accessibility';
4-
import { findAll } from '../find-all';
5-
import { matchTextContent } from './match-text-content';
3+
import { computeAriaLabel } from '../accessibility';
64

7-
export function matchLabelText(
8-
root: ReactTestInstance,
9-
element: ReactTestInstance,
10-
expectedText: TextMatch,
11-
options: TextMatchOptions = {},
12-
) {
13-
return (
14-
matchAccessibilityLabel(element, expectedText, options) ||
15-
matchAccessibilityLabelledBy(root, computeAriaLabelledBy(element), expectedText, options)
16-
);
17-
}
18-
19-
function matchAccessibilityLabel(
5+
export function matchAccessibilityLabel(
206
element: ReactTestInstance,
217
expectedLabel: TextMatch,
22-
options: TextMatchOptions,
8+
options?: TextMatchOptions,
239
) {
24-
return matches(expectedLabel, computeAriaLabel(element), options.normalizer, options.exact);
25-
}
26-
27-
function matchAccessibilityLabelledBy(
28-
root: ReactTestInstance,
29-
nativeId: string | undefined,
30-
text: TextMatch,
31-
options: TextMatchOptions,
32-
) {
33-
if (!nativeId) {
34-
return false;
35-
}
36-
37-
return (
38-
findAll(
39-
root,
40-
(node) => node.props.nativeID === nativeId && matchTextContent(node, text, options),
41-
).length > 0
42-
);
10+
return matches(expectedLabel, computeAriaLabel(element), options?.normalizer, options?.exact);
4311
}

src/queries/label-text.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
22
import { findAll } from '../helpers/find-all';
33
import { TextMatch, TextMatchOptions } from '../matches';
4-
import { matchLabelText } from '../helpers/matchers/match-label-text';
4+
import { matchAccessibilityLabel } from '../helpers/matchers/match-label-text';
55
import { makeQueries } from './make-queries';
66
import type {
77
FindAllByQuery,
@@ -19,7 +19,7 @@ function queryAllByLabelText(instance: ReactTestInstance) {
1919
return (text: TextMatch, queryOptions?: ByLabelTextOptions) => {
2020
return findAll(
2121
instance,
22-
(node) => matchLabelText(instance, node, text, queryOptions),
22+
(node) => matchAccessibilityLabel(node, text, queryOptions),
2323
queryOptions,
2424
);
2525
};

0 commit comments

Comments
 (0)