1
- import { AccessibilityState , AccessibilityValue , StyleSheet } from 'react-native' ;
1
+ import {
2
+ AccessibilityRole ,
3
+ AccessibilityState ,
4
+ AccessibilityValue ,
5
+ Role ,
6
+ StyleSheet ,
7
+ } from 'react-native' ;
2
8
import { ReactTestInstance } from 'react-test-renderer' ;
3
9
import { getHostSiblings , getUnsafeRootElement } from './component-tree' ;
4
10
import { getHostComponentNames , isHostSwitch , isHostText } from './host-component-names' ;
5
11
import { getTextContent } from './text-content' ;
12
+ import { isTextInputEditable } from './text-input' ;
6
13
7
14
type IsInaccessibleOptions = {
8
15
cache ?: WeakMap < ReactTestInstance , boolean > ;
@@ -78,7 +85,7 @@ function isSubtreeInaccessible(element: ReactTestInstance): boolean {
78
85
// iOS: accessibilityViewIsModal or aria-modal
79
86
// See: https://reactnative.dev/docs/accessibility#accessibilityviewismodal-ios
80
87
const hostSiblings = getHostSiblings ( element ) ;
81
- if ( hostSiblings . some ( ( sibling ) => getAccessibilityViewIsModal ( sibling ) ) ) {
88
+ if ( hostSiblings . some ( ( sibling ) => computeAriaModal ( sibling ) ) ) {
82
89
return true ;
83
90
}
84
91
@@ -115,7 +122,7 @@ export function isAccessibilityElement(element: ReactTestInstance | null): boole
115
122
* @param element
116
123
* @returns
117
124
*/
118
- export function getAccessibilityRole ( element : ReactTestInstance ) {
125
+ export function getRole ( element : ReactTestInstance ) : Role | AccessibilityRole {
119
126
const explicitRole = element . props . role ?? element . props . accessibilityRole ;
120
127
if ( explicitRole ) {
121
128
return explicitRole ;
@@ -128,45 +135,30 @@ export function getAccessibilityRole(element: ReactTestInstance) {
128
135
return 'none' ;
129
136
}
130
137
131
- export function getAccessibilityViewIsModal ( element : ReactTestInstance ) {
138
+ export function computeAriaModal ( element : ReactTestInstance ) : boolean | undefined {
132
139
return element . props [ 'aria-modal' ] ?? element . props . accessibilityViewIsModal ;
133
140
}
134
141
135
- export function getAccessibilityLabel ( element : ReactTestInstance ) : string | undefined {
142
+ export function computeAriaLabel ( element : ReactTestInstance ) : string | undefined {
136
143
return element . props [ 'aria-label' ] ?? element . props . accessibilityLabel ;
137
144
}
138
145
139
- export function getAccessibilityLabelledBy ( element : ReactTestInstance ) : string | undefined {
146
+ export function computeAriaLabelledBy ( element : ReactTestInstance ) : string | undefined {
140
147
return element . props [ 'aria-labelledby' ] ?? element . props . accessibilityLabelledBy ;
141
148
}
142
149
143
- export function computeAccessibilityState ( element : ReactTestInstance ) : AccessibilityState {
144
- const busy = computeA11yBusy ( element ) ;
145
- const checked = computeA11yChecked ( element ) ;
146
- const disabled = computeA11Disabled ( element ) ;
147
- const expanded = computeA11yExpanded ( element ) ;
148
- const selected = computeA11ySelected ( element ) ;
149
-
150
- return {
151
- busy,
152
- checked,
153
- disabled,
154
- expanded,
155
- selected,
156
- } ;
157
- }
158
-
159
- export function computeA11yBusy ( element : ReactTestInstance ) : AccessibilityState [ 'busy' ] {
160
- const props = element . props ;
161
- return props [ 'aria-busy' ] ?? props . accessibilityState ?. busy ;
150
+ // See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#busy-state
151
+ export function computeAriaBusy ( { props } : ReactTestInstance ) : boolean {
152
+ return props [ 'aria-busy' ] ?? props . accessibilityState ?. busy ?? false ;
162
153
}
163
154
164
- export function computeA11yChecked ( element : ReactTestInstance ) : AccessibilityState [ 'checked' ] {
155
+ // See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#checked-state
156
+ export function computeAriaChecked ( element : ReactTestInstance ) : AccessibilityState [ 'checked' ] {
165
157
if ( isHostSwitch ( element ) ) {
166
158
return element . props . value ;
167
159
}
168
160
169
- const role = getAccessibilityRole ( element ) ;
161
+ const role = getRole ( element ) ;
170
162
if ( role !== 'checkbox' && role !== 'radio' ) {
171
163
return undefined ;
172
164
}
@@ -175,22 +167,27 @@ export function computeA11yChecked(element: ReactTestInstance): AccessibilitySta
175
167
return props [ 'aria-checked' ] ?? props . accessibilityState ?. checked ;
176
168
}
177
169
178
- export function computeA11Disabled ( element : ReactTestInstance ) : AccessibilityState [ 'disabled' ] {
179
- const props = element . props ;
180
- return props [ 'aria-disabled' ] ?? props . accessibilityState ?. disabled ;
170
+ // See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#disabled-state
171
+ export function computeAriaDisabled ( element : ReactTestInstance ) : boolean {
172
+ if ( isHostSwitch ( element ) ) {
173
+ return ! isTextInputEditable ( element ) ;
174
+ }
175
+
176
+ const { props } = element ;
177
+ return props [ 'aria-disabled' ] ?? props . accessibilityState ?. disabled ?? false ;
181
178
}
182
179
183
- export function computeA11yExpanded ( element : ReactTestInstance ) : AccessibilityState [ ' expanded' ] {
184
- const props = element . props ;
180
+ // See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State# expanded-state
181
+ export function computeAriaExpanded ( { props } : ReactTestInstance ) : boolean | undefined {
185
182
return props [ 'aria-expanded' ] ?? props . accessibilityState ?. expanded ;
186
183
}
187
184
188
- export function computeA11ySelected ( element : ReactTestInstance ) : AccessibilityState [ ' selected' ] {
189
- const props = element . props ;
190
- return props [ 'aria-selected' ] ?? props . accessibilityState ?. selected ;
185
+ // See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State# selected-state
186
+ export function computeAriaSelected ( { props } : ReactTestInstance ) : boolean {
187
+ return props [ 'aria-selected' ] ?? props . accessibilityState ?. selected ?? false ;
191
188
}
192
189
193
- export function computeAccessibilityValue ( element : ReactTestInstance ) : AccessibilityValue {
190
+ export function computeAriaValue ( element : ReactTestInstance ) : AccessibilityValue {
194
191
const {
195
192
accessibilityValue,
196
193
'aria-valuemax' : ariaValueMax ,
@@ -208,12 +205,12 @@ export function computeAccessibilityValue(element: ReactTestInstance): Accessibi
208
205
}
209
206
210
207
export function computeAccessibleName ( element : ReactTestInstance ) : string | undefined {
211
- const label = getAccessibilityLabel ( element ) ;
208
+ const label = computeAriaLabel ( element ) ;
212
209
if ( label ) {
213
210
return label ;
214
211
}
215
212
216
- const labelElementId = getAccessibilityLabelledBy ( element ) ;
213
+ const labelElementId = computeAriaLabelledBy ( element ) ;
217
214
if ( labelElementId ) {
218
215
const rootElement = getUnsafeRootElement ( element ) ;
219
216
const labelElement = rootElement ?. findByProps ( { nativeID : labelElementId } ) ;
0 commit comments