Skip to content

Commit 567a87f

Browse files
committed
Increase test coverage and run expensive checks last
1 parent 6495c63 commit 567a87f

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

src/queries/__tests__/role.test.tsx

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import * as React from 'react';
2-
import { TouchableOpacity, Text, View } from 'react-native';
2+
import {
3+
TouchableOpacity,
4+
TouchableWithoutFeedback,
5+
Text,
6+
View,
7+
Pressable,
8+
Button as RNButton,
9+
} from 'react-native';
310
import { render } from '../..';
411

512
const TEXT_LABEL = 'cool text';
@@ -245,6 +252,39 @@ describe('supports accessibility states', () => {
245252

246253
expect(queryByRole('button', { disabled: false })).toBe(null);
247254
});
255+
256+
test('returns elements using the built-in disabled prop', () => {
257+
const { debug, getByRole } = render(
258+
<>
259+
<Pressable disabled accessibilityRole="button">
260+
<Text>Pressable</Text>
261+
</Pressable>
262+
263+
<TouchableWithoutFeedback disabled accessibilityRole="button">
264+
<View>
265+
<Text>TouchableWithoutFeedback</Text>
266+
</View>
267+
</TouchableWithoutFeedback>
268+
<RNButton disabled onPress={() => {}} title="RNButton" />
269+
</>
270+
);
271+
272+
expect(
273+
getByRole('button', { name: 'Pressable', disabled: true })
274+
).toBeTruthy();
275+
276+
expect(
277+
getByRole('button', {
278+
name: 'TouchableWithoutFeedback',
279+
disabled: true,
280+
})
281+
).toBeTruthy();
282+
debug();
283+
284+
expect(
285+
getByRole('button', { name: 'RNButton', disabled: true })
286+
).toBeTruthy();
287+
});
248288
});
249289

250290
describe('selected', () => {
@@ -525,6 +565,28 @@ describe('supports accessibility states', () => {
525565
});
526566
});
527567

568+
test('ignores non queried accessibilityState', () => {
569+
const { getByRole } = render(
570+
<TouchableOpacity
571+
accessibilityRole="button"
572+
accessibilityState={{
573+
disabled: true,
574+
// set `selected`, but don't query it
575+
selected: true,
576+
}}
577+
>
578+
<Text>Save</Text>
579+
</TouchableOpacity>
580+
);
581+
582+
expect(
583+
getByRole('button', {
584+
name: 'Save',
585+
disabled: true,
586+
})
587+
).toBeTruthy();
588+
});
589+
528590
test('matches an element combining all the options', () => {
529591
const { getByRole } = render(
530592
<TouchableOpacity

src/queries/role.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,34 @@ const queryAllByRole = (
4242
): ((role: TextMatch, options?: ByRoleOptions) => Array<ReactTestInstance>) =>
4343
function queryAllByRoleFn(role, options) {
4444
return instance.findAll((node) => {
45+
// run the cheapest checks first, and early exit too avoid unneeded computations
4546
const matchRole =
4647
typeof node.type === 'string' &&
4748
matchStringProp(node.props.accessibilityRole, role);
4849

4950
if (!matchRole) return false;
5051

51-
if (options?.name) {
52-
if (!matchAccessibleNameIfNeeded(node, options.name)) {
53-
return false;
52+
const mismatchAccessibilityState = accessibilityStates.some(
53+
(accessibilityState) => {
54+
const queriedState = options?.[accessibilityState];
55+
56+
if (typeof queriedState !== 'undefined') {
57+
return (
58+
queriedState !==
59+
// default to false because disabled:undefined is equivalent to disabled:false
60+
(node.props.accessibilityState?.[accessibilityState] ?? false)
61+
);
62+
} else {
63+
return false;
64+
}
5465
}
66+
);
67+
68+
if (mismatchAccessibilityState) {
69+
return false;
5570
}
5671

57-
return accessibilityStates.every((accessibilityState) => {
58-
const queriedState = options?.[accessibilityState];
59-
60-
if (typeof queriedState !== 'undefined') {
61-
return (
62-
queriedState ===
63-
// default to false because disabled:undefined is equivalent to disabled:false
64-
(node.props.accessibilityState?.[accessibilityState] ?? false)
65-
);
66-
} else {
67-
return true;
68-
}
69-
});
72+
return matchAccessibleNameIfNeeded(node, options?.name);
7073
});
7174
};
7275

0 commit comments

Comments
 (0)