Skip to content

Commit f0cda1c

Browse files
author
EBAM006
committed
feat: handle accessible in byrole
1 parent 7a4b7fc commit f0cda1c

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

src/helpers/__tests__/accessiblity.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { View, Text, TextInput, Pressable } from 'react-native';
2+
import { View, Text, TextInput, Pressable, Switch } from 'react-native';
33
import { render, isHiddenFromAccessibility, isInaccessible } from '../..';
44
import { isAccessibilityElement } from '../accessiblity';
55

@@ -202,6 +202,11 @@ describe('isAccessibilityElement', () => {
202202
expect(isAccessibilityElement(element)).toEqual(true);
203203
});
204204

205+
test('returns true for Switch component', () => {
206+
const element = render(<Switch testID="switch" />).getByTestId('switch');
207+
expect(isAccessibilityElement(element)).toEqual(true);
208+
});
209+
205210
test('returns false for View component', () => {
206211
const element = render(<View testID="element" />).getByTestId('element');
207212
expect(isAccessibilityElement(element)).toEqual(false);

src/helpers/accessiblity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AccessibilityState,
33
AccessibilityValue,
44
StyleSheet,
5+
Switch,
56
Text,
67
TextInput,
78
} from 'react-native';
@@ -97,6 +98,7 @@ export function isAccessibilityElement(
9798

9899
return (
99100
isHostElementForType(element, Text) ||
100-
isHostElementForType(element, TextInput)
101+
isHostElementForType(element, TextInput) ||
102+
isHostElementForType(element, Switch)
101103
);
102104
}

src/queries/__tests__/role-value.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe('accessibility value', () => {
66
test('matches using all value props', () => {
77
const { getByRole, queryByRole } = render(
88
<View
9+
accessible
910
accessibilityRole="adjustable"
1011
accessibilityValue={{ min: 0, max: 100, now: 50, text: '50%' }}
1112
/>
@@ -41,6 +42,7 @@ describe('accessibility value', () => {
4142
test('matches using single value', () => {
4243
const { getByRole, queryByRole } = render(
4344
<View
45+
accessible
4446
accessibilityRole="adjustable"
4547
accessibilityValue={{ min: 10, max: 20, now: 12, text: 'Hello' }}
4648
/>

src/queries/__tests__/role.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,3 +733,12 @@ test('byRole queries support hidden option', () => {
733733
`"Unable to find an element with role: "button""`
734734
);
735735
});
736+
737+
test('takes accessible prop into account', () => {
738+
const { queryByRole } = render(
739+
<Pressable accessibilityRole="button" accessible={false}>
740+
<Text>Action</Text>
741+
</Pressable>
742+
);
743+
expect(queryByRole('button', { name: 'Action' })).toBeFalsy();
744+
});

src/queries/role.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ReactTestInstance } from 'react-test-renderer';
33
import {
44
accessibilityStateKeys,
55
accessiblityValueKeys,
6+
isAccessibilityElement,
67
} from '../helpers/accessiblity';
78
import { findAll } from '../helpers/findAll';
89
import { matchAccessibilityState } from '../helpers/matchers/accessibilityState';
@@ -65,6 +66,7 @@ const queryAllByRole = (
6566
(node) =>
6667
// run the cheapest checks first, and early exit to avoid unneeded computations
6768
typeof node.type === 'string' &&
69+
isAccessibilityElement(node) &&
6870
matchStringProp(node.props.accessibilityRole, role) &&
6971
matchAccessibleStateIfNeeded(node, options) &&
7072
matchAccessibilityValueIfNeeded(node, options?.value) &&

0 commit comments

Comments
 (0)