Skip to content

Commit 188a285

Browse files
committed
Fix test naming for consistency, improve types and ensure multiple children matching is supported
1 parent 300541b commit 188a285

File tree

4 files changed

+39
-41
lines changed

4 files changed

+39
-41
lines changed

src/queries/__tests__/role.test.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ test('getAllByRole, queryAllByRole, findAllByRole', async () => {
7979
);
8080
});
8181

82-
describe('*ByRole with a name', () => {
83-
test('Find an element that has the corresponding role and a children with the name', () => {
82+
describe('supports name option', () => {
83+
test('returns an element that has the corresponding role and a children with the name', () => {
8484
const { getByRole } = render(
8585
<TouchableOpacity accessibilityRole="button" testID="target-button">
8686
<Text>Save</Text>
@@ -93,10 +93,11 @@ describe('*ByRole with a name', () => {
9393
);
9494
});
9595

96-
test('Find an element that has the corresponding role and a children with a matching accessibilityLabel', () => {
96+
test('returns an element that has the corresponding role when several children include the name', () => {
9797
const { getByRole } = render(
9898
<TouchableOpacity accessibilityRole="button" testID="target-button">
99-
<Text accessibilityLabel="Save" />
99+
<Text>Save</Text>
100+
<Text>Save</Text>
100101
</TouchableOpacity>
101102
);
102103

@@ -106,13 +107,11 @@ describe('*ByRole with a name', () => {
106107
);
107108
});
108109

109-
test('Find an element that has the corresponding role and a matching accessibilityLabel', () => {
110+
test('returns an element that has the corresponding role and a children with a matching accessibilityLabel', () => {
110111
const { getByRole } = render(
111-
<TouchableOpacity
112-
accessibilityRole="button"
113-
testID="target-button"
114-
accessibilityLabel="Save"
115-
></TouchableOpacity>
112+
<TouchableOpacity accessibilityRole="button" testID="target-button">
113+
<Text accessibilityLabel="Save" />
114+
</TouchableOpacity>
116115
);
117116

118117
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
@@ -121,7 +120,7 @@ describe('*ByRole with a name', () => {
121120
);
122121
});
123122

124-
test('Can find by name using a regex', () => {
123+
test('returns an element that has the corresponding role and a matching accessibilityLabel', () => {
125124
const { getByRole } = render(
126125
<TouchableOpacity
127126
accessibilityRole="button"
@@ -131,7 +130,7 @@ describe('*ByRole with a name', () => {
131130
);
132131

133132
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
134-
expect(getByRole('button', { name: /Save/ }).props.testID).toBe(
133+
expect(getByRole('button', { name: 'Save' }).props.testID).toBe(
135134
'target-button'
136135
);
137136
});

src/queries/role.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
2-
import type { AccessibilityRole } from 'react-native';
32
import { matchStringProp } from '../helpers/matchers/matchStringProp';
43
import { TextMatch } from '../matches';
54
import { getQueriesForElement } from '../within';
@@ -13,37 +12,34 @@ import type {
1312
QueryByQuery,
1413
} from './makeQueries';
1514

16-
type Role = AccessibilityRole | TextMatch;
17-
1815
type ByRoleOptions = {
1916
name?: TextMatch;
2017
};
2118

22-
const filterByAccessibleName = (
23-
node: ReactTestInstance,
24-
name: TextMatch | undefined
25-
) => {
26-
if (!name) return true;
19+
const matchAccessibleName = (node: ReactTestInstance, name?: TextMatch) => {
20+
if (name == null) return true;
2721

28-
const { queryByText, queryByLabelText } = getQueriesForElement(node);
29-
return Boolean(queryByText(name) || queryByLabelText(name));
22+
const { queryAllByText, queryAllByLabelText } = getQueriesForElement(node);
23+
return (
24+
queryAllByText(name).length > 0 || queryAllByLabelText(name).length > 0
25+
);
3026
};
3127

3228
const queryAllByRole = (
3329
instance: ReactTestInstance
34-
): ((role: Role, options?: ByRoleOptions) => Array<ReactTestInstance>) =>
30+
): ((role: TextMatch, options?: ByRoleOptions) => Array<ReactTestInstance>) =>
3531
function queryAllByRoleFn(role, options) {
3632
return instance.findAll(
3733
(node) =>
3834
typeof node.type === 'string' &&
3935
matchStringProp(node.props.accessibilityRole, role) &&
40-
filterByAccessibleName(node, options?.name)
36+
matchAccessibleName(node, options?.name)
4137
);
4238
};
4339

44-
const getMultipleError = (role: Role) =>
40+
const getMultipleError = (role: TextMatch) =>
4541
`Found multiple elements with accessibilityRole: ${String(role)} `;
46-
const getMissingError = (role: Role) =>
42+
const getMissingError = (role: TextMatch) =>
4743
`Unable to find an element with accessibilityRole: ${String(role)}`;
4844

4945
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
@@ -53,12 +49,12 @@ const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
5349
);
5450

5551
export type ByRoleQueries = {
56-
getByRole: GetByQuery<Role, ByRoleOptions>;
57-
getAllByRole: GetAllByQuery<Role, ByRoleOptions>;
58-
queryByRole: QueryByQuery<Role, ByRoleOptions>;
59-
queryAllByRole: QueryAllByQuery<Role, ByRoleOptions>;
60-
findByRole: FindByQuery<Role, ByRoleOptions>;
61-
findAllByRole: FindAllByQuery<Role, ByRoleOptions>;
52+
getByRole: GetByQuery<TextMatch, ByRoleOptions>;
53+
getAllByRole: GetAllByQuery<TextMatch, ByRoleOptions>;
54+
queryByRole: QueryByQuery<TextMatch, ByRoleOptions>;
55+
queryAllByRole: QueryAllByQuery<TextMatch, ByRoleOptions>;
56+
findByRole: FindByQuery<TextMatch, ByRoleOptions>;
57+
findAllByRole: FindAllByQuery<TextMatch, ByRoleOptions>;
6258
};
6359

6460
export const bindByRoleQueries = (

typings/index.flow.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ interface UnsafeByPropsQueries {
203203
| [];
204204
}
205205

206-
interface ByRoleOption {
207-
name: string;
206+
interface ByRoleOptions {
207+
name?: string;
208208
}
209209

210210
interface A11yAPI {
@@ -249,24 +249,27 @@ interface A11yAPI {
249249
) => FindAllReturn;
250250

251251
// Role
252-
getByRole: (matcher: A11yRole | RegExp, role?: ByRoleOption) => GetReturn;
252+
getByRole: (matcher: A11yRole | RegExp, role?: ByRoleOptions) => GetReturn;
253253
getAllByRole: (
254254
matcher: A11yRole | RegExp,
255-
role?: ByRoleOption
255+
role?: ByRoleOptions
256256
) => GetAllReturn;
257-
queryByRole: (matcher: A11yRole | RegExp, role?: ByRoleOption) => QueryReturn;
257+
queryByRole: (
258+
matcher: A11yRole | RegExp,
259+
role?: ByRoleOptions
260+
) => QueryReturn;
258261
queryAllByRole: (
259262
matcher: A11yRole | RegExp,
260-
role?: ByRoleOption
263+
role?: ByRoleOptions
261264
) => QueryAllReturn;
262265
findByRole: (
263266
matcher: A11yRole | RegExp,
264-
role?: ByRoleOption,
267+
role?: ByRoleOptions,
265268
waitForOptions?: WaitForOptions
266269
) => FindReturn;
267270
findAllByRole: (
268271
matcher: A11yRole | RegExp,
269-
role?: ByRoleOption,
272+
role?: ByRoleOptions,
270273
waitForOptions?: WaitForOptions
271274
) => FindAllReturn;
272275

website/docs/Queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ const element = screen.getByRole('button');
191191

192192
#### Options
193193

194-
`name`: Finds an element with given `accessibilityRole` and an accessible name (equivalent to `ByText` or `queryByLabelText`).
194+
`name`: Finds an element with given `accessibilityRole` and an accessible name (equivalent to `byText` or `byLabelText` query).
195195

196196
### `ByA11yState`, `ByAccessibilityState`
197197

0 commit comments

Comments
 (0)