5
5
Role ,
6
6
StyleSheet ,
7
7
} 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' ;
10
11
import { isHostImage , isHostSwitch , isHostText , isHostTextInput } from './host-component-names' ;
11
12
import { getTextContent } from './text-content' ;
12
13
import { isTextInputEditable } from './text-input' ;
@@ -158,6 +159,19 @@ export function computeAriaModal(element: ReactTestInstance): boolean | undefine
158
159
}
159
160
160
161
export 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
+
161
175
const explicitLabel = element . props [ 'aria-label' ] ?? element . props . accessibilityLabel ;
162
176
if ( explicitLabel ) {
163
177
return explicitLabel ;
@@ -171,10 +185,6 @@ export function computeAriaLabel(element: ReactTestInstance): string | undefined
171
185
return undefined ;
172
186
}
173
187
174
- export function computeAriaLabelledBy ( element : ReactTestInstance ) : string | undefined {
175
- return element . props [ 'aria-labelledby' ] ?? element . props . accessibilityLabelledBy ;
176
- }
177
-
178
188
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#busy-state
179
189
export function computeAriaBusy ( { props } : ReactTestInstance ) : boolean {
180
190
return props [ 'aria-busy' ] ?? props . accessibilityState ?. busy ?? false ;
@@ -234,21 +244,7 @@ export function computeAriaValue(element: ReactTestInstance): AccessibilityValue
234
244
}
235
245
236
246
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 ) ;
247
+ return computeAriaLabel ( element ) ?? getTextContent ( element ) ;
252
248
}
253
249
254
250
type RoleSupportMap = Partial < Record < Role | AccessibilityRole , true > > ;
0 commit comments