1
1
import type { ReactTestInstance } from 'react-test-renderer' ;
2
+ import type { AccessibilityRole , Role } from 'react-native' ;
2
3
import {
3
4
accessibilityStateKeys ,
4
- accessiblityValueKeys ,
5
+ accessibilityValueKeys ,
5
6
getAccessibilityRole ,
6
7
isAccessibilityElement ,
7
- } from '../helpers/accessiblity ' ;
8
+ } from '../helpers/accessibility ' ;
8
9
import { findAll } from '../helpers/find-all' ;
9
10
import {
10
11
AccessibilityStateMatcher ,
@@ -16,6 +17,7 @@ import {
16
17
} from '../helpers/matchers/match-accessibility-value' ;
17
18
import { matchStringProp } from '../helpers/matchers/match-string-prop' ;
18
19
import type { TextMatch } from '../matches' ;
20
+ import { StringWithAutocomplete } from '../types' ;
19
21
import { getQueriesForElement } from '../within' ;
20
22
import { makeQueries } from './make-queries' ;
21
23
import type {
@@ -28,7 +30,9 @@ import type {
28
30
} from './make-queries' ;
29
31
import { CommonQueryOptions } from './options' ;
30
32
31
- type ByRoleOptions = CommonQueryOptions &
33
+ export type ByRoleMatcher = StringWithAutocomplete < AccessibilityRole | Role > | RegExp ;
34
+
35
+ export type ByRoleOptions = CommonQueryOptions &
32
36
AccessibilityStateMatcher & {
33
37
name ?: TextMatch ;
34
38
value ?: AccessibilityValueMatcher ;
@@ -52,7 +56,9 @@ const matchAccessibilityValueIfNeeded = (
52
56
return value != null ? matchAccessibilityValue ( node , value ) : true ;
53
57
} ;
54
58
55
- const queryAllByRole = ( instance : ReactTestInstance ) : QueryAllByQuery < TextMatch , ByRoleOptions > =>
59
+ const queryAllByRole = (
60
+ instance : ReactTestInstance ,
61
+ ) : QueryAllByQuery < ByRoleMatcher , ByRoleOptions > =>
56
62
function queryAllByRoleFn ( role , options ) {
57
63
return findAll (
58
64
instance ,
@@ -80,7 +86,7 @@ const formatQueryParams = (role: TextMatch, options: ByRoleOptions = {}) => {
80
86
}
81
87
} ) ;
82
88
83
- accessiblityValueKeys . forEach ( ( valueKey ) => {
89
+ accessibilityValueKeys . forEach ( ( valueKey ) => {
84
90
if ( options ?. value ?. [ valueKey ] !== undefined ) {
85
91
params . push ( `${ valueKey } value: ${ options ?. value ?. [ valueKey ] } ` ) ;
86
92
}
@@ -102,12 +108,12 @@ const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
102
108
) ;
103
109
104
110
export type ByRoleQueries = {
105
- getByRole : GetByQuery < TextMatch , ByRoleOptions > ;
106
- getAllByRole : GetAllByQuery < TextMatch , ByRoleOptions > ;
107
- queryByRole : QueryByQuery < TextMatch , ByRoleOptions > ;
108
- queryAllByRole : QueryAllByQuery < TextMatch , ByRoleOptions > ;
109
- findByRole : FindByQuery < TextMatch , ByRoleOptions > ;
110
- findAllByRole : FindAllByQuery < TextMatch , ByRoleOptions > ;
111
+ getByRole : GetByQuery < ByRoleMatcher , ByRoleOptions > ;
112
+ getAllByRole : GetAllByQuery < ByRoleMatcher , ByRoleOptions > ;
113
+ queryByRole : QueryByQuery < ByRoleMatcher , ByRoleOptions > ;
114
+ queryAllByRole : QueryAllByQuery < ByRoleMatcher , ByRoleOptions > ;
115
+ findByRole : FindByQuery < ByRoleMatcher , ByRoleOptions > ;
116
+ findAllByRole : FindAllByQuery < ByRoleMatcher , ByRoleOptions > ;
111
117
} ;
112
118
113
119
export const bindByRoleQueries = ( instance : ReactTestInstance ) : ByRoleQueries => ( {
0 commit comments