1
1
// @flow
2
2
import makeQuery from './makeQuery' ;
3
+ import type { A11yRole , A11yStates , A11yState } from '../types.flow' ;
3
4
4
- type QueryFn = ( string | RegExp ) => ReactTestInstance | null ;
5
- type QueryAllFn = ( string | RegExp ) => Array < ReactTestInstance > | [ ] ;
6
- type GetFn = ( string | RegExp ) => ReactTestInstance ;
7
- type GetAllFn = ( string | RegExp ) => Array < ReactTestInstance > ;
8
- type ArrayQueryFn = ( string | Array < string > ) => ReactTestInstance | null ;
9
- type ArrayQueryAllFn = (
10
- string | Array < string >
11
- ) => Array < ReactTestInstance > | [ ] ;
12
- type ArrayGetFn = ( string | Array < string > ) => ReactTestInstance ;
13
- type ArrayGetAllFn = ( string | Array < string > ) => Array < ReactTestInstance > ;
5
+ type GetReturn = ReactTestInstance ;
6
+ type GetAllReturn = Array < ReactTestInstance > ;
7
+ type QueryReturn = ReactTestInstance | null ;
8
+ type QueryAllReturn = Array < ReactTestInstance > | [ ] ;
14
9
15
10
type A11yAPI = { |
16
- getByA11yLabel : GetFn ,
17
- getAllByA11yLabel : GetAllFn ,
18
- queryByA11yLabel : QueryFn ,
19
- queryAllByA11yLabel : QueryAllFn ,
20
- getByA11yHint : GetFn ,
21
- getAllByA11yHint : GetAllFn ,
22
- queryByA11yHint : QueryFn ,
23
- queryAllByA11yHint : QueryAllFn ,
24
- getByA11yRole : GetFn ,
25
- getAllByA11yRole : GetAllFn ,
26
- queryByA11yRole : QueryFn ,
27
- queryAllByA11yRole : QueryAllFn ,
28
- getByA11yStates : ArrayGetFn ,
29
- getAllByA11yStates : ArrayGetAllFn ,
30
- queryByA11yStates : ArrayQueryFn ,
31
- queryAllByA11yStates : ArrayQueryAllFn ,
11
+ // Label
12
+ getByA11yLabel : ( string | RegExp ) = > GetReturn ,
13
+ getAllByA11yLabel : ( string | RegExp ) = > GetAllReturn ,
14
+ queryByA11yLabel : ( string | RegExp ) = > QueryReturn ,
15
+ queryAllByA11yLabel : ( string | RegExp ) = > QueryAllReturn ,
16
+
17
+ // Hint
18
+ getByA11yHint : ( string | RegExp ) = > GetReturn ,
19
+ getAllByA11yHint : ( string | RegExp ) = > GetAllReturn ,
20
+ queryByA11yHint : ( string | RegExp ) = > QueryReturn ,
21
+ queryAllByA11yHint : ( string | RegExp ) = > QueryAllReturn ,
22
+
23
+ // Role
24
+ getByA11yRole : ( A11yRole | RegExp ) = > GetReturn ,
25
+ getAllByA11yRole : ( A11yRole | RegExp ) = > GetAllReturn ,
26
+ queryByA11yRole : ( A11yRole | RegExp ) = > QueryReturn ,
27
+ queryAllByA11yRole : ( A11yRole | RegExp ) = > QueryAllReturn ,
28
+
29
+ // States
30
+ getByA11yStates : ( A11yStates | Array < A11yStates > ) => GetReturn ,
31
+ getAllByA11yStates : ( A11yStates | Array < A11yStates > ) => GetAllReturn ,
32
+ queryByA11yStates : ( A11yStates | Array < A11yStates > ) => QueryReturn ,
33
+ queryAllByA11yStates : ( A11yStates | Array < A11yStates > ) => QueryAllReturn ,
34
+
35
+ // State
36
+ getByA11yState : A11yState => GetReturn ,
37
+ getAllByA11yState : A11yState => GetAllReturn ,
38
+ queryByA11yState : A11yState => QueryReturn ,
39
+ queryAllByA11yState : A11yState => QueryAllReturn ,
32
40
| } ;
33
41
34
- export function matchStringValue ( prop ?: string , matcher : string | RegExp ) {
42
+ export function matchStringValue (
43
+ prop ?: string ,
44
+ matcher : string | RegExp
45
+ ) : boolean {
35
46
if ( ! prop ) {
36
47
return false ;
37
48
}
@@ -46,7 +57,7 @@ export function matchStringValue(prop?: string, matcher: string | RegExp) {
46
57
export function matchArrayValue (
47
58
prop ?: Array < string > ,
48
59
matcher : string | Array < string >
49
- ) {
60
+ ) : boolean {
50
61
if ( ! prop || matcher . length === 0 ) {
51
62
return false ;
52
63
}
@@ -58,6 +69,14 @@ export function matchArrayValue(
58
69
return ! matcher . some ( e => ! prop . includes ( e ) ) ;
59
70
}
60
71
72
+ export function matchObject < T : { } > ( prop ? : T , matcher : T ) : boolean {
73
+ return prop
74
+ ? Object . keys ( matcher ) . length !== 0 &&
75
+ Object . keys ( prop ) . length !== 0 &&
76
+ ! Object . keys ( matcher ) . some ( key => prop [ key ] !== matcher [ key ] )
77
+ : false ;
78
+ }
79
+
61
80
const a11yAPI = ( instance : ReactTestInstance ) : A11yAPI =>
62
81
( {
63
82
...makeQuery (
@@ -100,6 +119,16 @@ const a11yAPI = (instance: ReactTestInstance): A11yAPI =>
100
119
} ,
101
120
matchArrayValue
102
121
) ( instance ) ,
122
+ ...makeQuery (
123
+ 'accessibilityState' ,
124
+ {
125
+ getBy : [ 'getByA11yState' , 'getByAccessibilityState' ] ,
126
+ getAllBy : [ 'getAllByA11yState' , 'getAllByAccessibilityState' ] ,
127
+ queryBy : [ 'queryByA11yState' , 'queryByAccessibilityState' ] ,
128
+ queryAllBy : [ 'queryAllByA11yState' , 'queryAllByAccessibilityState' ] ,
129
+ } ,
130
+ matchObject
131
+ ) ( instance ) ,
103
132
} : any ) ;
104
133
105
134
export default a11yAPI ;
0 commit comments