Skip to content

Commit e916842

Browse files
ShaswatPrabhatmdjastrzebski
authored andcommitted
Add queryOptions to labelText and hintText
1 parent 8e5c9f9 commit e916842

File tree

2 files changed

+60
-38
lines changed

2 files changed

+60
-38
lines changed

src/queries/hintText.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
2-
import { TextMatch } from '../matches';
3-
import { matchStringProp } from '../helpers/matchers/matchStringProp';
2+
import { matches, TextMatch } from '../matches';
43
import { makeQueries } from './makeQueries';
54
import type {
65
FindAllByQuery,
@@ -10,16 +9,27 @@ import type {
109
QueryAllByQuery,
1110
QueryByQuery,
1211
} from './makeQueries';
12+
import { TextMatchOptions } from './text';
13+
14+
const getNodeByHintText = (
15+
node: ReactTestInstance,
16+
text: TextMatch,
17+
options: TextMatchOptions = {}
18+
) => {
19+
const { exact, normalizer } = options;
20+
return matches(text, node.props.accessibilityHint, normalizer, exact);
21+
};
1322

1423
const queryAllByHintText = (
1524
instance: ReactTestInstance
16-
): ((hint: TextMatch) => Array<ReactTestInstance>) =>
17-
function queryAllByA11yHintFn(hint) {
18-
return instance.findAll(
19-
(node) =>
20-
typeof node.type === 'string' &&
21-
matchStringProp(node.props.accessibilityHint, hint)
22-
);
25+
): ((
26+
hint: TextMatch,
27+
queryOptions?: TextMatchOptions
28+
) => Array<ReactTestInstance>) =>
29+
function queryAllByA11yHintFn(hint, queryOptions) {
30+
return instance
31+
.findAll((node) => getNodeByHintText(node, hint, queryOptions))
32+
.filter((element) => typeof element.type === 'string');
2333
};
2434

2535
const getMultipleError = (hint: TextMatch) =>
@@ -34,28 +44,28 @@ const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
3444
);
3545

3646
export type ByHintTextQueries = {
37-
getByHintText: GetByQuery<TextMatch>;
38-
getAllByHintText: GetAllByQuery<TextMatch>;
39-
queryByHintText: QueryByQuery<TextMatch>;
40-
queryAllByHintText: QueryAllByQuery<TextMatch>;
41-
findByHintText: FindByQuery<TextMatch>;
42-
findAllByHintText: FindAllByQuery<TextMatch>;
47+
getByHintText: GetByQuery<TextMatch, TextMatchOptions>;
48+
getAllByHintText: GetAllByQuery<TextMatch, TextMatchOptions>;
49+
queryByHintText: QueryByQuery<TextMatch, TextMatchOptions>;
50+
queryAllByHintText: QueryAllByQuery<TextMatch, TextMatchOptions>;
51+
findByHintText: FindByQuery<TextMatch, TextMatchOptions>;
52+
findAllByHintText: FindAllByQuery<TextMatch, TextMatchOptions>;
4353

4454
// a11yHint aliases
45-
getByA11yHint: GetByQuery<TextMatch>;
46-
getAllByA11yHint: GetAllByQuery<TextMatch>;
47-
queryByA11yHint: QueryByQuery<TextMatch>;
48-
queryAllByA11yHint: QueryAllByQuery<TextMatch>;
49-
findByA11yHint: FindByQuery<TextMatch>;
50-
findAllByA11yHint: FindAllByQuery<TextMatch>;
55+
getByA11yHint: GetByQuery<TextMatch, TextMatchOptions>;
56+
getAllByA11yHint: GetAllByQuery<TextMatch, TextMatchOptions>;
57+
queryByA11yHint: QueryByQuery<TextMatch, TextMatchOptions>;
58+
queryAllByA11yHint: QueryAllByQuery<TextMatch, TextMatchOptions>;
59+
findByA11yHint: FindByQuery<TextMatch, TextMatchOptions>;
60+
findAllByA11yHint: FindAllByQuery<TextMatch, TextMatchOptions>;
5161

5262
// accessibilityHint aliases
53-
getByAccessibilityHint: GetByQuery<TextMatch>;
54-
getAllByAccessibilityHint: GetAllByQuery<TextMatch>;
55-
queryByAccessibilityHint: QueryByQuery<TextMatch>;
56-
queryAllByAccessibilityHint: QueryAllByQuery<TextMatch>;
57-
findByAccessibilityHint: FindByQuery<TextMatch>;
58-
findAllByAccessibilityHint: FindAllByQuery<TextMatch>;
63+
getByAccessibilityHint: GetByQuery<TextMatch, TextMatchOptions>;
64+
getAllByAccessibilityHint: GetAllByQuery<TextMatch, TextMatchOptions>;
65+
queryByAccessibilityHint: QueryByQuery<TextMatch, TextMatchOptions>;
66+
queryAllByAccessibilityHint: QueryAllByQuery<TextMatch, TextMatchOptions>;
67+
findByAccessibilityHint: FindByQuery<TextMatch, TextMatchOptions>;
68+
findAllByAccessibilityHint: FindAllByQuery<TextMatch, TextMatchOptions>;
5969
};
6070

6171
export const bindByHintTextQueries = (

src/queries/labelText.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
2-
import { TextMatch } from '../matches';
3-
import { matchStringProp } from '../helpers/matchers/matchStringProp';
2+
import { matches, TextMatch } from '../matches';
43
import { makeQueries } from './makeQueries';
54
import type {
65
FindAllByQuery,
@@ -10,15 +9,28 @@ import type {
109
QueryAllByQuery,
1110
QueryByQuery,
1211
} from './makeQueries';
12+
import { TextMatchOptions } from './text';
13+
14+
const getNodeByLabelText = (
15+
node: ReactTestInstance,
16+
text: TextMatch,
17+
options: TextMatchOptions = {}
18+
) => {
19+
const { exact, normalizer } = options;
20+
return matches(text, node.props.accessibilityLabel, normalizer, exact);
21+
};
1322

1423
const queryAllByLabelText = (
1524
instance: ReactTestInstance
16-
): ((text: TextMatch) => Array<ReactTestInstance>) =>
17-
function queryAllByLabelTextFn(text) {
25+
): ((
26+
text: TextMatch,
27+
queryOptions?: TextMatchOptions
28+
) => Array<ReactTestInstance>) =>
29+
function queryAllByLabelTextFn(text, queryOptions?: TextMatchOptions) {
1830
return instance.findAll(
1931
(node) =>
2032
typeof node.type === 'string' &&
21-
matchStringProp(node.props.accessibilityLabel, text)
33+
getNodeByLabelText(node, text, queryOptions)
2234
);
2335
};
2436

@@ -34,12 +46,12 @@ const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
3446
);
3547

3648
export type ByLabelTextQueries = {
37-
getByLabelText: GetByQuery<TextMatch>;
38-
getAllByLabelText: GetAllByQuery<TextMatch>;
39-
queryByLabelText: QueryByQuery<TextMatch>;
40-
queryAllByLabelText: QueryAllByQuery<TextMatch>;
41-
findByLabelText: FindByQuery<TextMatch>;
42-
findAllByLabelText: FindAllByQuery<TextMatch>;
49+
getByLabelText: GetByQuery<TextMatch, TextMatchOptions>;
50+
getAllByLabelText: GetAllByQuery<TextMatch, TextMatchOptions>;
51+
queryByLabelText: QueryByQuery<TextMatch, TextMatchOptions>;
52+
queryAllByLabelText: QueryAllByQuery<TextMatch, TextMatchOptions>;
53+
findByLabelText: FindByQuery<TextMatch, TextMatchOptions>;
54+
findAllByLabelText: FindAllByQuery<TextMatch, TextMatchOptions>;
4355
};
4456

4557
export const bindByLabelTextQueries = (

0 commit comments

Comments
 (0)