File tree 2 files changed +42
-6
lines changed
2 files changed +42
-6
lines changed Original file line number Diff line number Diff line change 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 { getHostComponentNames , isHostText } from './host-component-names' ;
9
+ import { getTextContent } from './text-content' ;
10
10
11
11
type IsInaccessibleOptions = {
12
12
cache ?: WeakMap < ReactTestInstance , boolean > ;
@@ -113,10 +113,30 @@ export function isAccessibilityElement(
113
113
) ;
114
114
}
115
115
116
- export function getAccessibilityRole (
117
- element : ReactTestInstance
118
- ) : string | undefined {
119
- return element . props . role ?? element . props . accessibilityRole ;
116
+ /**
117
+ * Returns the accessibility role for given element. It will return explicit
118
+ * role from either `role` or `accessibilityRole` props if set.
119
+ *
120
+ * If explicit role is not available, it would try to return default element
121
+ * role:
122
+ * - `text` for `Text` elements
123
+ *
124
+ * In all other cases this functions returns `none`.
125
+ *
126
+ * @param element
127
+ * @returns
128
+ */
129
+ export function getAccessibilityRole ( element : ReactTestInstance ) {
130
+ const explicitRole = element . props . role ?? element . props . accessibilityRole ;
131
+ if ( explicitRole ) {
132
+ return explicitRole ;
133
+ }
134
+
135
+ if ( isHostText ( element ) ) {
136
+ return 'text' ;
137
+ }
138
+
139
+ return 'none' ;
120
140
}
121
141
122
142
export function getAccessibilityViewIsModal ( element : ReactTestInstance ) {
Original file line number Diff line number Diff line change 3
3
TouchableOpacity ,
4
4
TouchableWithoutFeedback ,
5
5
Text ,
6
+ TextInput ,
6
7
View ,
7
8
Pressable ,
8
9
Button as RNButton ,
@@ -110,6 +111,21 @@ test('supports role prop', () => {
110
111
expect ( screen . getByRole ( 'button' ) ) . toBeTruthy ( ) ;
111
112
} ) ;
112
113
114
+ test ( 'supports default View component "none" role' , ( ) => {
115
+ const screen = render ( < View testID = "view" accessible /> ) ;
116
+ expect ( screen . getByRole ( 'none' ) . props . testID ) . toBe ( 'view' ) ;
117
+ } ) ;
118
+
119
+ test ( 'supports default Text component "text" role' , ( ) => {
120
+ const screen = render ( < Text testID = "text" /> ) ;
121
+ expect ( screen . getByRole ( 'text' ) . props . testID ) . toBe ( 'text' ) ;
122
+ } ) ;
123
+
124
+ test ( 'supports default TextInput component "none" role' , ( ) => {
125
+ const screen = render ( < TextInput testID = "text-input" /> ) ;
126
+ expect ( screen . getByRole ( 'none' ) . props . testID ) . toBe ( 'text-input' ) ;
127
+ } ) ;
128
+
113
129
describe ( 'supports name option' , ( ) => {
114
130
test ( 'returns an element that has the corresponding role and a children with the name' , ( ) => {
115
131
const { getByRole } = render (
You can’t perform that action at this time.
0 commit comments