MCPcopy Index your code
hub / github.com/callstack/react-native-testing-library / computeAriaLabel

Function computeAriaLabel

src/helpers/accessibility.ts:152–184  ·  view source on GitHub ↗
(instance: TestInstance)

Source from the content-addressed store, hash-verified

150}
151
152export function computeAriaLabel(instance: TestInstance): string | undefined {
153 const labelElementIds = getAriaLabelledByIds(instance);
154 if (labelElementIds.length > 0) {
155 const container = getContainerInstance(instance);
156 const labelTexts = labelElementIds
157 .map((labelElementId) => {
158 const labelInstance = findAll(
159 container,
160 (node) => isTestInstance(node) && node.props.nativeID === labelElementId,
161 { includeHiddenElements: true },
162 );
163
164 return labelInstance.length > 0 ? getTextContent(labelInstance[0]) : undefined;
165 })
166 .filter((labelText): labelText is string => labelText !== undefined);
167
168 if (labelTexts.length > 0) {
169 return labelTexts.join(' ').trim().replace(/\s+/g, ' ');
170 }
171 }
172
173 const explicitLabel = instance.props['aria-label'] ?? instance.props.accessibilityLabel;
174 if (explicitLabel) {
175 return explicitLabel;
176 }
177
178 //https://github.com/facebook/react-native/blob/8dabed60f456e76a9e53273b601446f34de41fb5/packages/react-native/Libraries/Image/Image.ios.js#L173
179 if (isHostImage(instance) && instance.props.alt) {
180 return instance.props.alt;
181 }
182
183 return undefined;
184}
185
186function getAriaLabelledByIds(instance: TestInstance): string[] {
187 const ariaLabelledBy = instance.props['aria-labelledby'];

Callers 3

matchAccessibilityLabelFunction · 0.90
computeAccessibleNameFunction · 0.85

Calls 6

getContainerInstanceFunction · 0.90
findAllFunction · 0.90
isTestInstanceFunction · 0.90
getTextContentFunction · 0.90
isHostImageFunction · 0.90
getAriaLabelledByIdsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…