1
1
import { AccessibilityState , AccessibilityValue , StyleSheet } from 'react-native' ;
2
2
import { ReactTestInstance } from 'react-test-renderer' ;
3
3
import { getHostSiblings , getUnsafeRootElement } from './component-tree' ;
4
- import { getHostComponentNames , isHostText } from './host-component-names' ;
4
+ import { getHostComponentNames , isHostSwitch , isHostText } from './host-component-names' ;
5
5
import { getTextContent } from './text-content' ;
6
6
7
7
type IsInaccessibleOptions = {
@@ -45,7 +45,7 @@ export function isHiddenFromAccessibility(
45
45
return false ;
46
46
}
47
47
48
- /** RTL-compatitibility alias for `isHiddenFromAccessibility` */
48
+ /** RTL-compatibility alias for `isHiddenFromAccessibility` */
49
49
export const isInaccessible = isHiddenFromAccessibility ;
50
50
51
51
function isSubtreeInaccessible ( element : ReactTestInstance ) : boolean {
@@ -140,45 +140,57 @@ export function getAccessibilityLabelledBy(element: ReactTestInstance): string |
140
140
return element . props [ 'aria-labelledby' ] ?? element . props . accessibilityLabelledBy ;
141
141
}
142
142
143
- export function getAccessibilityState ( element : ReactTestInstance ) : AccessibilityState | undefined {
144
- const {
145
- accessibilityState,
146
- 'aria-busy' : ariaBusy ,
147
- 'aria-checked' : ariaChecked ,
148
- 'aria-disabled' : ariaDisabled ,
149
- 'aria-expanded' : ariaExpanded ,
150
- 'aria-selected' : ariaSelected ,
151
- } = element . props ;
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 ;
162
+ }
152
163
153
- const hasAnyAccessibilityStateProps =
154
- accessibilityState != null ||
155
- ariaBusy != null ||
156
- ariaChecked != null ||
157
- ariaDisabled != null ||
158
- ariaExpanded != null ||
159
- ariaSelected != null ;
164
+ export function computeA11yChecked ( element : ReactTestInstance ) : AccessibilityState [ 'checked' ] {
165
+ if ( isHostSwitch ( element ) ) {
166
+ return element . props . value ;
167
+ }
160
168
161
- if ( ! hasAnyAccessibilityStateProps ) {
169
+ const role = getAccessibilityRole ( element ) ;
170
+ if ( role !== 'checkbox' && role !== 'radio' ) {
162
171
return undefined ;
163
172
}
164
173
165
- return {
166
- busy : ariaBusy ?? accessibilityState ?. busy ,
167
- checked : ariaChecked ?? accessibilityState ?. checked ,
168
- disabled : ariaDisabled ?? accessibilityState ?. disabled ,
169
- expanded : ariaExpanded ?? accessibilityState ?. expanded ,
170
- selected : ariaSelected ?? accessibilityState ?. selected ,
171
- } ;
174
+ const props = element . props ;
175
+ return props [ 'aria-checked' ] ?? props . accessibilityState ?. checked ;
172
176
}
173
177
174
- export function getAccessibilityCheckedState (
175
- element : ReactTestInstance ,
176
- ) : AccessibilityState [ 'checked' ] {
177
- const { accessibilityState, 'aria-checked' : ariaChecked } = element . props ;
178
- return ariaChecked ?? accessibilityState ?. checked ;
178
+ export function computeA11Disabled ( element : ReactTestInstance ) : AccessibilityState [ 'disabled' ] {
179
+ const props = element . props ;
180
+ return props [ 'aria-disabled' ] ?? props . accessibilityState ?. disabled ;
179
181
}
180
182
181
- export function getAccessibilityValue ( element : ReactTestInstance ) : AccessibilityValue | undefined {
183
+ export function computeA11yExpanded ( element : ReactTestInstance ) : AccessibilityState [ 'expanded' ] {
184
+ const props = element . props ;
185
+ return props [ 'aria-expanded' ] ?? props . accessibilityState ?. expanded ;
186
+ }
187
+
188
+ export function computeA11ySelected ( element : ReactTestInstance ) : AccessibilityState [ 'selected' ] {
189
+ const props = element . props ;
190
+ return props [ 'aria-selected' ] ?? props . accessibilityState ?. selected ;
191
+ }
192
+
193
+ export function computeAccessibilityValue ( element : ReactTestInstance ) : AccessibilityValue {
182
194
const {
183
195
accessibilityValue,
184
196
'aria-valuemax' : ariaValueMax ,
@@ -187,17 +199,6 @@ export function getAccessibilityValue(element: ReactTestInstance): Accessibility
187
199
'aria-valuetext' : ariaValueText ,
188
200
} = element . props ;
189
201
190
- const hasAnyAccessibilityValueProps =
191
- accessibilityValue != null ||
192
- ariaValueMax != null ||
193
- ariaValueMin != null ||
194
- ariaValueNow != null ||
195
- ariaValueText != null ;
196
-
197
- if ( ! hasAnyAccessibilityValueProps ) {
198
- return undefined ;
199
- }
200
-
201
202
return {
202
203
max : ariaValueMax ?? accessibilityValue ?. max ,
203
204
min : ariaValueMin ?? accessibilityValue ?. min ,
@@ -206,33 +207,7 @@ export function getAccessibilityValue(element: ReactTestInstance): Accessibility
206
207
} ;
207
208
}
208
209
209
- export function isElementBusy ( element : ReactTestInstance ) : NonNullable < AccessibilityState [ 'busy' ] > {
210
- const { accessibilityState, 'aria-busy' : ariaBusy } = element . props ;
211
- return ariaBusy ?? accessibilityState ?. busy ?? false ;
212
- }
213
-
214
- export function isElementCollapsed (
215
- element : ReactTestInstance ,
216
- ) : NonNullable < AccessibilityState [ 'expanded' ] > {
217
- const { accessibilityState, 'aria-expanded' : ariaExpanded } = element . props ;
218
- return ( ariaExpanded ?? accessibilityState ?. expanded ) === false ;
219
- }
220
-
221
- export function isElementExpanded (
222
- element : ReactTestInstance ,
223
- ) : NonNullable < AccessibilityState [ 'expanded' ] > {
224
- const { accessibilityState, 'aria-expanded' : ariaExpanded } = element . props ;
225
- return ariaExpanded ?? accessibilityState ?. expanded ?? false ;
226
- }
227
-
228
- export function isElementSelected (
229
- element : ReactTestInstance ,
230
- ) : NonNullable < AccessibilityState [ 'selected' ] > {
231
- const { accessibilityState, 'aria-selected' : ariaSelected } = element . props ;
232
- return ariaSelected ?? accessibilityState ?. selected ?? false ;
233
- }
234
-
235
- export function getAccessibleName ( element : ReactTestInstance ) : string | undefined {
210
+ export function computeAccessibleName ( element : ReactTestInstance ) : string | undefined {
236
211
const label = getAccessibilityLabel ( element ) ;
237
212
if ( label ) {
238
213
return label ;
0 commit comments