@@ -4,9 +4,13 @@ import {
4
4
StyleSheet ,
5
5
} from 'react-native' ;
6
6
import { ReactTestInstance } from 'react-test-renderer' ;
7
- import { getTextContent } from './text-content' ;
8
7
import { getHostSiblings , getUnsafeRootElement } from './component-tree' ;
9
- import { getHostComponentNames } from './host-component-names' ;
8
+ import {
9
+ getHostComponentNames ,
10
+ isHostText ,
11
+ isHostTextInput ,
12
+ } from './host-component-names' ;
13
+ import { getTextContent } from './text-content' ;
10
14
11
15
type IsInaccessibleOptions = {
12
16
cache ?: WeakMap < ReactTestInstance , boolean > ;
@@ -113,10 +117,39 @@ export function isAccessibilityElement(
113
117
) ;
114
118
}
115
119
116
- export function getAccessibilityRole (
117
- element : ReactTestInstance
118
- ) : string | undefined {
119
- return element . props . role ?? element . props . accessibilityRole ;
120
+ /**
121
+ * Returns the accessibility role for given element. It will return explicit
122
+ * role from either `role` or `accessibilityRole` props if set.
123
+ *
124
+ * If explicit role is not available, it would try to return default element
125
+ * role:
126
+ * - `text` for `Text` elements
127
+ * - `textbox`* for `TextInput` elements.
128
+ *
129
+ * Note: `textbox` is not an official React Native role, you cannot set it
130
+ * explicitly on an element. However, it is an ARIA role that better characterizes
131
+ * TextInput elements than the default `none` role.
132
+ *
133
+ * In all other cases this functions returns `none`.
134
+ *
135
+ * @param element
136
+ * @returns
137
+ */
138
+ export function getAccessibilityRole ( element : ReactTestInstance ) {
139
+ const explicitRole = element . props . role ?? element . props . accessibilityRole ;
140
+ if ( explicitRole ) {
141
+ return explicitRole ;
142
+ }
143
+
144
+ if ( isHostText ( element ) ) {
145
+ return 'text' ;
146
+ }
147
+
148
+ if ( isHostTextInput ( element ) ) {
149
+ return 'textbox' ;
150
+ }
151
+
152
+ return 'none' ;
120
153
}
121
154
122
155
export function getAccessibilityViewIsModal ( element : ReactTestInstance ) {
0 commit comments