Skip to content

Commit 92cb031

Browse files
feat: alias isInaccessible as isHiddenFromAccessibility (#1211)
1 parent 860356b commit 92cb031

File tree

7 files changed

+71
-59
lines changed

7 files changed

+71
-59
lines changed

src/helpers/__tests__/accessiblity.test.tsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
import React from 'react';
22
import { View, Text, TextInput } from 'react-native';
3-
import { render, isInaccessible } from '../..';
3+
import { render, isHiddenFromAccessibility, isInaccessible } from '../..';
44

55
test('returns false for accessible elements', () => {
66
expect(
7-
isInaccessible(render(<View testID="subject" />).getByTestId('subject'))
7+
isHiddenFromAccessibility(
8+
render(<View testID="subject" />).getByTestId('subject')
9+
)
810
).toBe(false);
911

1012
expect(
11-
isInaccessible(
13+
isHiddenFromAccessibility(
1214
render(<Text testID="subject">Hello</Text>).getByTestId('subject')
1315
)
1416
).toBe(false);
1517

1618
expect(
17-
isInaccessible(
19+
isHiddenFromAccessibility(
1820
render(<TextInput testID="subject" />).getByTestId('subject')
1921
)
2022
).toBe(false);
2123
});
2224

25+
test('returns true for hidden elements', () => {
26+
expect(isHiddenFromAccessibility(null)).toBe(true);
27+
});
28+
2329
test('detects elements with accessibilityElementsHidden prop', () => {
2430
const view = render(<View testID="subject" accessibilityElementsHidden />);
25-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
31+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
2632
});
2733

2834
test('detects nested elements with accessibilityElementsHidden prop', () => {
@@ -31,7 +37,7 @@ test('detects nested elements with accessibilityElementsHidden prop', () => {
3137
<View testID="subject" />
3238
</View>
3339
);
34-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
40+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
3541
});
3642

3743
test('detects deeply nested elements with accessibilityElementsHidden prop', () => {
@@ -44,14 +50,14 @@ test('detects deeply nested elements with accessibilityElementsHidden prop', ()
4450
</View>
4551
</View>
4652
);
47-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
53+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
4854
});
4955

5056
test('detects elements with importantForAccessibility="no-hide-descendants" prop', () => {
5157
const view = render(
5258
<View testID="subject" importantForAccessibility="no-hide-descendants" />
5359
);
54-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
60+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
5561
});
5662

5763
test('detects nested elements with importantForAccessibility="no-hide-descendants" prop', () => {
@@ -60,12 +66,12 @@ test('detects nested elements with importantForAccessibility="no-hide-descendant
6066
<View testID="subject" />
6167
</View>
6268
);
63-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
69+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
6470
});
6571

6672
test('detects elements with display=none', () => {
6773
const view = render(<View testID="subject" style={{ display: 'none' }} />);
68-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
74+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
6975
});
7076

7177
test('detects nested elements with display=none', () => {
@@ -74,7 +80,7 @@ test('detects nested elements with display=none', () => {
7480
<View testID="subject" />
7581
</View>
7682
);
77-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
83+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
7884
});
7985

8086
test('detects deeply nested elements with display=none', () => {
@@ -87,7 +93,7 @@ test('detects deeply nested elements with display=none', () => {
8793
</View>
8894
</View>
8995
);
90-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
96+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
9197
});
9298

9399
test('detects elements with display=none with complex style', () => {
@@ -97,12 +103,12 @@ test('detects elements with display=none with complex style', () => {
97103
style={[{ display: 'flex' }, [{ display: 'flex' }], { display: 'none' }]}
98104
/>
99105
);
100-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
106+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
101107
});
102108

103109
test('is not trigged by opacity = 0', () => {
104110
const view = render(<View testID="subject" style={{ opacity: 0 }} />);
105-
expect(isInaccessible(view.getByTestId('subject'))).toBe(false);
111+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(false);
106112
});
107113

108114
test('detects siblings of element with accessibilityViewIsModal prop', () => {
@@ -112,7 +118,7 @@ test('detects siblings of element with accessibilityViewIsModal prop', () => {
112118
<View testID="subject" />
113119
</View>
114120
);
115-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
121+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
116122
});
117123

118124
test('detects deeply nested siblings of element with accessibilityViewIsModal prop', () => {
@@ -126,7 +132,7 @@ test('detects deeply nested siblings of element with accessibilityViewIsModal pr
126132
</View>
127133
</View>
128134
);
129-
expect(isInaccessible(view.getByTestId('subject'))).toBe(true);
135+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(true);
130136
});
131137

132138
test('is not triggered for element with accessibilityViewIsModal prop', () => {
@@ -135,7 +141,7 @@ test('is not triggered for element with accessibilityViewIsModal prop', () => {
135141
<View accessibilityViewIsModal testID="subject" />
136142
</View>
137143
);
138-
expect(isInaccessible(view.getByTestId('subject'))).toBe(false);
144+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(false);
139145
});
140146

141147
test('is not triggered for child of element with accessibilityViewIsModal prop', () => {
@@ -146,7 +152,7 @@ test('is not triggered for child of element with accessibilityViewIsModal prop',
146152
</View>
147153
</View>
148154
);
149-
expect(isInaccessible(view.getByTestId('subject'))).toBe(false);
155+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(false);
150156
});
151157

152158
test('is not triggered for descendent of element with accessibilityViewIsModal prop', () => {
@@ -161,5 +167,9 @@ test('is not triggered for descendent of element with accessibilityViewIsModal p
161167
</View>
162168
</View>
163169
);
164-
expect(isInaccessible(view.getByTestId('subject'))).toBe(false);
170+
expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(false);
171+
});
172+
173+
test('has isInaccessible alias', () => {
174+
expect(isInaccessible).toBe(isHiddenFromAccessibility);
165175
});

src/helpers/accessiblity.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const accessibilityStateKeys: AccessibilityStateKey[] = [
1616
'expanded',
1717
];
1818

19-
export function isInaccessible(
19+
export function isHiddenFromAccessibility(
2020
element: ReactTestInstance | null,
2121
{ cache }: IsInaccessibleOptions = {}
2222
): boolean {
@@ -43,13 +43,10 @@ export function isInaccessible(
4343
return false;
4444
}
4545

46-
export function isSubtreeInaccessible(
47-
element: ReactTestInstance | null
48-
): boolean {
49-
if (element == null) {
50-
return true;
51-
}
46+
/** RTL-compatitibility alias for `isHiddenFromAccessibility` */
47+
export const isInaccessible = isHiddenFromAccessibility;
5248

49+
function isSubtreeInaccessible(element: ReactTestInstance): boolean {
5350
// iOS: accessibilityElementsHidden
5451
// See: https://reactnative.dev/docs/accessibility#accessibilityelementshidden-ios
5552
if (element.props.accessibilityElementsHidden) {

src/helpers/findAll.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import { getConfig } from '../config';
3-
import { isInaccessible } from './accessiblity';
3+
import { isHiddenFromAccessibility } from './accessiblity';
44

55
interface FindAllOptions {
66
hidden?: boolean;
@@ -19,5 +19,7 @@ export function findAll(
1919
}
2020

2121
const cache = new WeakMap<ReactTestInstance>();
22-
return results.filter((element) => !isInaccessible(element, { cache }));
22+
return results.filter(
23+
(element) => !isHiddenFromAccessibility(element, { cache })
24+
);
2325
}

src/pure.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import act from './act';
2-
import cleanup from './cleanup';
3-
import fireEvent from './fireEvent';
4-
import render from './render';
5-
import waitFor from './waitFor';
6-
import waitForElementToBeRemoved from './waitForElementToBeRemoved';
7-
import { within, getQueriesForElement } from './within';
8-
import { getDefaultNormalizer } from './matches';
9-
import { renderHook } from './renderHook';
10-
import { screen } from './screen';
11-
import { isInaccessible } from './helpers/accessiblity';
1+
export { default as act } from './act';
2+
export { default as cleanup } from './cleanup';
3+
export { default as fireEvent } from './fireEvent';
4+
export { default as render } from './render';
5+
export { default as waitFor } from './waitFor';
6+
export { default as waitForElementToBeRemoved } from './waitForElementToBeRemoved';
7+
export { within, getQueriesForElement } from './within';
8+
9+
export { configure, resetToDefaults } from './config';
10+
export {
11+
isHiddenFromAccessibility,
12+
isInaccessible,
13+
} from './helpers/accessiblity';
14+
export { getDefaultNormalizer } from './matches';
15+
export { renderHook } from './renderHook';
16+
export { screen } from './screen';
1217

1318
export type {
1419
RenderOptions,
@@ -17,16 +22,3 @@ export type {
1722
} from './render';
1823
export type { RenderHookOptions, RenderHookResult } from './renderHook';
1924
export type { Config } from './config';
20-
21-
export { act };
22-
export { cleanup };
23-
export { configure, resetToDefaults } from './config';
24-
export { fireEvent };
25-
export { render };
26-
export { waitFor };
27-
export { waitForElementToBeRemoved };
28-
export { within, getQueriesForElement };
29-
export { getDefaultNormalizer };
30-
export { renderHook };
31-
export { screen };
32-
export { isInaccessible };

typings/index.flow.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,20 @@ declare module '@testing-library/react-native' {
472472
declare export var act: (callback: () => void) => Thenable;
473473
declare export var within: (instance: ReactTestInstance) => Queries;
474474
declare export var getQueriesForElement: (
475-
instance: ReactTestInstance
475+
element: ReactTestInstance
476476
) => Queries;
477477

478478
declare export var getDefaultNormalizer: (
479479
normalizerConfig?: NormalizerConfig
480480
) => NormalizerFn;
481481

482+
declare export var isHiddenFromAccessibility: (
483+
element: ReactTestInstance | null
484+
) => boolean;
485+
declare export var isInaccessible: (
486+
element: ReactTestInstance | null
487+
) => boolean;
488+
482489
declare type RenderHookResult<Result, Props> = {
483490
rerender: (props: Props) => void,
484491
result: { current: Result },

website/docs/API.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ title: API
5757
- [`RNTL_SKIP_AUTO_CLEANUP`](#rntl_skip_auto_cleanup)
5858
- [`RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS`](#rntl_skip_auto_detect_fake_timers)
5959
- [Accessibility](#accessibility)
60-
- [`isInaccessible`](#isinaccessible)
60+
- [`isHiddenFromAccessibility`](#ishiddenfromaccessibility)
6161

6262
This page gathers public API of React Native Testing Library along with usage examples.
6363

@@ -825,16 +825,20 @@ $ RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS=true jest
825825

826826
## Accessibility
827827

828-
### `isInaccessible`
828+
### `isHiddenFromAccessibility`
829829

830830
```ts
831-
function isInaccessible(element: ReactTestInstance | null): boolean {}
831+
function isHiddenFromAccessibility(
832+
element: ReactTestInstance | null
833+
): boolean {}
832834
```
833835

834-
Checks if given element is hidden from assistive technology, e.g. screen readers.
836+
Also available as `isInaccessible()` alias for React Testing Library compatibility.
837+
838+
Checks if given element is hidden from assistive technology, e.g. screen readers.
835839

836840
:::note
837-
Like [`isInaccessible`](https://testing-library.com/docs/dom-testing-library/api-accessibility/#isinaccessible) function from [DOM Testing Library](https://testing-library.com/docs/dom-testing-library/intro) this function considers both accessibility elements and presentational elements (regular `View`s) to be accessible, unless they are hidden in terms of host platform.
841+
Like [`isInaccessible`](https://testing-library.com/docs/dom-testing-library/api-accessibility/#isinaccessible) function from DOM Testing Library this function considers both accessibility elements and presentational elements (regular `View`s) to be accessible, unless they are hidden in terms of host platform.
838842

839843
This covers only part of [ARIA notion of Accessiblity Tree](https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion), as ARIA excludes both hidden and presentational elements from the Accessibility Tree.
840844
:::

website/docs/Queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ All queries have the `hidden` option which enables them to respect accessibility
382382

383383
You can configure the default value with the [`configure` function](API.md#configure).
384384

385-
An element is considered to be hidden from accessibility based on [`isInaccessible()`](./API.md#isinaccessible) function result.
385+
An element is considered to be hidden from accessibility based on [`isHiddenFromAccessibility()`](./API.md#ishiddenfromaccessibility) function.
386386

387387
**Examples**
388388

0 commit comments

Comments
 (0)