Skip to content

Commit ea68aa1

Browse files
committed
fix: remove unwanted files
1 parent 990ca8b commit ea68aa1

File tree

3 files changed

+112
-26
lines changed

3 files changed

+112
-26
lines changed

src/__tests__/a11yAPI.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const BUTTON_LABEL = 'cool button';
77
const BUTTON_HINT = 'click this button';
88
const TEXT_LABEL = 'cool text';
99
const TEXT_HINT = 'static text';
10+
const SINGLE_OCCURANCE = 'more words';
11+
const TWO_OCCURANCE = 'cooler text';
1012
// Little hack to make all the methods happy with type
1113
const NO_MATCHES_TEXT: any = 'not-existent-element';
1214
const FOUND_TWO_INSTANCES = 'Expected 1 but found 2 instances';
@@ -70,6 +72,22 @@ function Section() {
7072
);
7173
}
7274

75+
function ButtonsWithText() {
76+
return (
77+
<>
78+
<TouchableOpacity accessibilityRole="button">
79+
<Text>{TWO_OCCURANCE}</Text>
80+
</TouchableOpacity>
81+
<TouchableOpacity accessibilityRole="button">
82+
<Text>{TWO_OCCURANCE}</Text>
83+
</TouchableOpacity>
84+
<TouchableOpacity accessibilityRole="button">
85+
<Text>{SINGLE_OCCURANCE}</Text>
86+
</TouchableOpacity>
87+
</>
88+
);
89+
}
90+
7391
test('getByA11yLabel, queryByA11yLabel, findByA11yLabel', async () => {
7492
const { getByA11yLabel, queryByA11yLabel, findByA11yLabel } = render(
7593
<Section />
@@ -192,6 +210,34 @@ test('getByA11yRole, queryByA11yRole, findByA11yRole', async () => {
192210
await expect(findByA11yRole('link')).rejects.toThrow(FOUND_TWO_INSTANCES);
193211
});
194212

213+
test('getByA11yRole, queryByA11yRole, findByA11yRole with name', async () => {
214+
const { getByA11yRole, queryByA11yRole, findByA11yRole } = render(
215+
<ButtonsWithText />
216+
);
217+
218+
expect(() => getByA11yRole(/button/g, { name: TWO_OCCURANCE })).toThrow(
219+
FOUND_TWO_INSTANCES
220+
);
221+
getByA11yRole(/button/g, { name: SINGLE_OCCURANCE });
222+
223+
expect(queryByA11yRole(/button/g, { name: NO_MATCHES_TEXT })).toBeNull();
224+
expect(() => queryByA11yRole(/button/g, { name: TWO_OCCURANCE })).toThrow(
225+
FOUND_TWO_INSTANCES
226+
);
227+
228+
await findByA11yRole('button', {
229+
name: SINGLE_OCCURANCE,
230+
});
231+
await expect(
232+
findByA11yRole('button', { ...waitForOptions, name: NO_MATCHES_TEXT })
233+
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityRole', 'button'));
234+
await expect(
235+
findByA11yRole('button', {
236+
name: TWO_OCCURANCE,
237+
})
238+
).rejects.toThrow(FOUND_TWO_INSTANCES);
239+
});
240+
195241
test('getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole', async () => {
196242
const { getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole } = render(
197243
<Section />
@@ -211,6 +257,8 @@ test('getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole', async () => {
211257
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityRole'));
212258
});
213259

260+
test.todo('getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole with name');
261+
214262
// TODO: accessibilityStates was removed from RN 0.62
215263
test.skip('getByA11yStates, queryByA11yStates', () => {
216264
const { getByA11yStates, queryByA11yStates } = render(<Section />);

src/helpers/a11yAPI.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ type QueryAllReturn = Array<ReactTestInstance>;
1010
type FindReturn = Promise<GetReturn>;
1111
type FindAllReturn = Promise<GetAllReturn>;
1212

13+
type QueryOptions = {
14+
name: string | RegExp,
15+
};
16+
17+
export type WaitForOptionsWithName = {
18+
...WaitForOptions,
19+
...$Exact<QueryOptions>,
20+
};
21+
1322
export type A11yAPI = {|
1423
// Label
1524
getByA11yLabel: (string | RegExp) => GetReturn,
@@ -40,18 +49,18 @@ export type A11yAPI = {|
4049
findAllByHintText: (string | RegExp, ?WaitForOptions) => FindAllReturn,
4150

4251
// Role
43-
getByA11yRole: (A11yRole | RegExp) => GetReturn,
44-
getByRole: (A11yRole | RegExp) => GetReturn,
45-
getAllByA11yRole: (A11yRole | RegExp) => GetAllReturn,
46-
getAllByRole: (A11yRole | RegExp) => GetAllReturn,
47-
queryByA11yRole: (A11yRole | RegExp) => QueryReturn,
48-
queryByRole: (A11yRole | RegExp) => QueryReturn,
49-
queryAllByA11yRole: (A11yRole | RegExp) => QueryAllReturn,
50-
queryAllByRole: (A11yRole | RegExp) => QueryAllReturn,
51-
findByA11yRole: (A11yRole, ?WaitForOptions) => FindReturn,
52-
findByRole: (A11yRole, ?WaitForOptions) => FindReturn,
53-
findAllByA11yRole: (A11yRole, ?WaitForOptions) => FindAllReturn,
54-
findAllByRole: (A11yRole, ?WaitForOptions) => FindAllReturn,
52+
getByA11yRole: (A11yRole | RegExp, ?QueryOptions, ?QueryOptions) => GetReturn,
53+
getByRole: (A11yRole | RegExp, ?QueryOptions) => GetReturn,
54+
getAllByA11yRole: (A11yRole | RegExp, ?QueryOptions) => GetAllReturn,
55+
getAllByRole: (A11yRole | RegExp, ?QueryOptions) => GetAllReturn,
56+
queryByA11yRole: (A11yRole | RegExp, ?QueryOptions) => QueryReturn,
57+
queryByRole: (A11yRole | RegExp, ?QueryOptions) => QueryReturn,
58+
queryAllByA11yRole: (A11yRole | RegExp, ?QueryOptions) => QueryAllReturn,
59+
queryAllByRole: (A11yRole | RegExp, ?QueryOptions) => QueryAllReturn,
60+
findByA11yRole: (A11yRole, ?WaitForOptionsWithName) => FindReturn,
61+
findByRole: (A11yRole, ?WaitForOptionsWithName) => FindReturn,
62+
findAllByA11yRole: (A11yRole, ?WaitForOptionsWithName) => FindAllReturn,
63+
findAllByRole: (A11yRole, ?WaitForOptionsWithName) => FindAllReturn,
5564

5665
// States
5766
getByA11yStates: (A11yStates | Array<A11yStates>) => GetReturn,

src/helpers/makeA11yQuery.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22
import waitFor from '../waitFor';
3-
import type { WaitForOptions } from '../waitFor';
3+
import { getQueriesForElement } from '../within';
4+
import type { WaitForOptionsWithName } from './a11yAPI';
45
import {
56
ErrorWithStack,
67
prepareErrorMessage,
@@ -26,15 +27,30 @@ type QueryNames = {
2627
findAllBy: Array<string>,
2728
};
2829

30+
type QueryOptions = {
31+
name: string | RegExp,
32+
};
33+
2934
const makeA11yQuery = <P: mixed, M: mixed>(
3035
name: string,
3136
queryNames: QueryNames,
3237
matcherFn: (prop: P, value: M) => boolean
3338
): ((instance: ReactTestInstance) => { ... }) => (
3439
instance: ReactTestInstance
3540
) => {
36-
const getBy = (matcher: M) => {
41+
const getBy = (matcher: M, options?: QueryOptions) => {
3742
try {
43+
if (options?.name) {
44+
return instance.find((node) => {
45+
const matchesRole =
46+
isNodeValid(node) && matcherFn(node.props[name], matcher);
47+
48+
if (!matchesRole) return false;
49+
50+
return !!getQueriesForElement(node).queryByText(options.name);
51+
});
52+
}
53+
3854
return instance.find(
3955
(node) => isNodeValid(node) && matcherFn(node.props[name], matcher)
4056
);
@@ -46,10 +62,23 @@ const makeA11yQuery = <P: mixed, M: mixed>(
4662
}
4763
};
4864

49-
const getAllBy = (matcher: M) => {
50-
const results = instance.findAll(
51-
(node) => isNodeValid(node) && matcherFn(node.props[name], matcher)
52-
);
65+
const getAllBy = (matcher: M, options?: QueryOptions) => {
66+
let results = [];
67+
68+
if (options?.name) {
69+
results = instance.find((node) => {
70+
const matchesRole =
71+
isNodeValid(node) && matcherFn(node.props[name], matcher);
72+
73+
if (!matchesRole) return false;
74+
75+
return !!getQueriesForElement(node).queryByText(options.name);
76+
});
77+
} else {
78+
results = instance.findAll(
79+
(node) => isNodeValid(node) && matcherFn(node.props[name], matcher)
80+
);
81+
}
5382

5483
if (results.length === 0) {
5584
throw new ErrorWithStack(
@@ -61,28 +90,28 @@ const makeA11yQuery = <P: mixed, M: mixed>(
6190
return results;
6291
};
6392

64-
const queryBy = (matcher: M) => {
93+
const queryBy = (matcher: M, options?: QueryOptions) => {
6594
try {
66-
return getBy(matcher);
95+
return getBy(matcher, options);
6796
} catch (error) {
6897
return createQueryByError(error, queryBy);
6998
}
7099
};
71100

72-
const queryAllBy = (matcher: M) => {
101+
const queryAllBy = (matcher: M, options?: QueryOptions) => {
73102
try {
74-
return getAllBy(matcher);
103+
return getAllBy(matcher, options);
75104
} catch (error) {
76105
return [];
77106
}
78107
};
79108

80-
const findBy = (matcher: M, waitForOptions?: WaitForOptions) => {
81-
return waitFor(() => getBy(matcher), waitForOptions);
109+
const findBy = (matcher: M, waitForOptions?: WaitForOptionsWithName) => {
110+
return waitFor(() => getBy(matcher, waitForOptions));
82111
};
83112

84-
const findAllBy = (matcher: M, waitForOptions?: WaitForOptions) => {
85-
return waitFor(() => getAllBy(matcher), waitForOptions);
113+
const findAllBy = (matcher: M, waitForOptions?: WaitForOptionsWithName) => {
114+
return waitFor(() => getAllBy(matcher, waitForOptions));
86115
};
87116

88117
return {

0 commit comments

Comments
 (0)