55 Role ,
66 StyleSheet ,
77} from 'react-native' ;
8- import { ReactTestInstance } from 'react-test-renderer' ;
9- import { getHostSiblings , getUnsafeRootElement } from './component-tree' ;
8+ import type { ReactTestInstance } from 'react-test-renderer' ;
9+ import { getHostSiblings , getUnsafeRootElement , isHostElement } from './component-tree' ;
10+ import { findAll } from './find-all' ;
1011import { isHostImage , isHostSwitch , isHostText , isHostTextInput } from './host-component-names' ;
1112import { getTextContent } from './text-content' ;
1213import { isTextInputEditable } from './text-input' ;
@@ -158,6 +159,19 @@ export function computeAriaModal(element: ReactTestInstance): boolean | undefine
158159}
159160
160161export function computeAriaLabel ( element : ReactTestInstance ) : string | undefined {
162+ const labelElementId = element . props [ 'aria-labelledby' ] ?? element . props . accessibilityLabelledBy ;
163+ if ( labelElementId ) {
164+ const rootElement = getUnsafeRootElement ( element ) ;
165+ const labelElement = findAll (
166+ rootElement ,
167+ ( node ) => isHostElement ( node ) && node . props . nativeID === labelElementId ,
168+ { includeHiddenElements : true } ,
169+ ) ;
170+ if ( labelElement . length > 0 ) {
171+ return getTextContent ( labelElement [ 0 ] ) ;
172+ }
173+ }
174+
161175 const explicitLabel = element . props [ 'aria-label' ] ?? element . props . accessibilityLabel ;
162176 if ( explicitLabel ) {
163177 return explicitLabel ;
@@ -171,10 +185,6 @@ export function computeAriaLabel(element: ReactTestInstance): string | undefined
171185 return undefined ;
172186}
173187
174- export function computeAriaLabelledBy ( element : ReactTestInstance ) : string | undefined {
175- return element . props [ 'aria-labelledby' ] ?? element . props . accessibilityLabelledBy ;
176- }
177-
178188// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#busy-state
179189export function computeAriaBusy ( { props } : ReactTestInstance ) : boolean {
180190 return props [ 'aria-busy' ] ?? props . accessibilityState ?. busy ?? false ;
@@ -234,21 +244,7 @@ export function computeAriaValue(element: ReactTestInstance): AccessibilityValue
234244}
235245
236246export 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 ) ;
247+ return computeAriaLabel ( element ) ?? getTextContent ( element ) ;
252248}
253249
254250type RoleSupportMap = Partial < Record < Role | AccessibilityRole , true > > ;
0 commit comments