Skip to content

Commit 05be4af

Browse files
author
EBAM006
committed
feat: add isAccessibilityElement helper
1 parent 2577013 commit 05be4af

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/helpers/__tests__/accessiblity.test.tsx

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

56
describe('isHiddenFromAccessibility', () => {
67
test('returns false for accessible elements', () => {
@@ -179,3 +180,53 @@ describe('isHiddenFromAccessibility', () => {
179180
expect(isInaccessible).toBe(isHiddenFromAccessibility);
180181
});
181182
});
183+
184+
describe('isAccessibilityElement', () => {
185+
describe('when accessible prop is not defined', () => {
186+
test('returns true for Text component', () => {
187+
const element = render(<Text testID="text">Hey</Text>).getByTestId(
188+
'text'
189+
);
190+
expect(isAccessibilityElement(element)).toEqual(true);
191+
});
192+
193+
test('returns true for TextInput component', () => {
194+
const element = render(<TextInput testID="input" />).getByTestId('input');
195+
expect(isAccessibilityElement(element)).toEqual(true);
196+
});
197+
198+
test('returns true for Pressable component', () => {
199+
const element = render(<Pressable testID="pressable" />).getByTestId(
200+
'pressable'
201+
);
202+
expect(isAccessibilityElement(element)).toEqual(true);
203+
});
204+
205+
test('returns false for View component', () => {
206+
const element = render(<View testID="element" />).getByTestId('element');
207+
expect(isAccessibilityElement(element)).toEqual(false);
208+
});
209+
});
210+
211+
describe('when accessible prop is defined', () => {
212+
test('returns false when accessible prop is set to false', () => {
213+
const element = render(
214+
<TextInput accessible={false} testID="input">
215+
Hey
216+
</TextInput>
217+
).getByTestId('input');
218+
expect(isAccessibilityElement(element)).toEqual(false);
219+
});
220+
221+
test('returns true when accessible prop is set to true', () => {
222+
const element = render(
223+
<View accessible={true} testID="view" />
224+
).getByTestId('view');
225+
expect(isAccessibilityElement(element)).toEqual(true);
226+
});
227+
});
228+
229+
test('returns false when given null', () => {
230+
expect(isAccessibilityElement(null)).toEqual(false);
231+
});
232+
});

src/helpers/accessiblity.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import {
22
AccessibilityState,
33
AccessibilityValue,
44
StyleSheet,
5+
Text,
6+
TextInput,
57
} from 'react-native';
68
import { ReactTestInstance } from 'react-test-renderer';
7-
import { getHostSiblings } from './component-tree';
9+
import { getHostSiblings, isHostElementForType } from './component-tree';
810

911
type IsInaccessibleOptions = {
1012
cache?: WeakMap<ReactTestInstance, boolean>;
@@ -81,3 +83,20 @@ function isSubtreeInaccessible(element: ReactTestInstance): boolean {
8183

8284
return false;
8385
}
86+
87+
export function isAccessibilityElement(
88+
element: ReactTestInstance | null
89+
): boolean {
90+
if (element == null) {
91+
return false;
92+
}
93+
94+
if (element.props.accessible !== undefined) {
95+
return element.props.accessible;
96+
}
97+
98+
return (
99+
isHostElementForType(element, Text) ||
100+
isHostElementForType(element, TextInput)
101+
);
102+
}

0 commit comments

Comments
 (0)