Skip to content

Commit 23e9b56

Browse files
committed
refactor: a11y label helpers
1 parent 5a10b31 commit 23e9b56

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
@@ -158,6 +158,15 @@ export function computeAriaModal(element: ReactTestInstance): boolean | undefine
158158
}
159159

160160
export function computeAriaLabel(element: ReactTestInstance): string | undefined {
161+
const labelElementId = element.props['aria-labelledby'] ?? element.props.accessibilityLabelledBy;
162+
if (labelElementId) {
163+
const rootElement = getUnsafeRootElement(element);
164+
const labelElement = rootElement?.findByProps({ nativeID: labelElementId });
165+
if (labelElement) {
166+
return getTextContent(labelElement);
167+
}
168+
}
169+
161170
const explicitLabel = element.props['aria-label'] ?? element.props.accessibilityLabel;
162171
if (explicitLabel) {
163172
return explicitLabel;
@@ -171,10 +180,6 @@ export function computeAriaLabel(element: ReactTestInstance): string | undefined
171180
return undefined;
172181
}
173182

174-
export function computeAriaLabelledBy(element: ReactTestInstance): string | undefined {
175-
return element.props['aria-labelledby'] ?? element.props.accessibilityLabelledBy;
176-
}
177-
178183
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#busy-state
179184
export function computeAriaBusy({ props }: ReactTestInstance): boolean {
180185
return props['aria-busy'] ?? props.accessibilityState?.busy ?? false;
@@ -234,21 +239,7 @@ export function computeAriaValue(element: ReactTestInstance): AccessibilityValue
234239
}
235240

236241
export function computeAccessibleName(element: ReactTestInstance): string | undefined {
237-
const label = computeAriaLabel(element);
238-
if (label) {
239-
return label;
240-
}
241-
242-
const labelElementId = computeAriaLabelledBy(element);
243-
if (labelElementId) {
244-
const rootElement = getUnsafeRootElement(element);
245-
const labelElement = rootElement?.findByProps({ nativeID: labelElementId });
246-
if (labelElement) {
247-
return getTextContent(labelElement);
248-
}
249-
}
250-
251-
return getTextContent(element);
242+
return computeAriaLabel(element) ?? getTextContent(element);
252243
}
253244

254245
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)