11// @flow
22import makeQuery from './makeQuery' ;
3+ import type { A11yRole , A11yStates , A11yState } from '../types.flow' ;
34
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 > | [ ] ;
149
1510type 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 ,
3240| } ;
3341
34- export function matchStringValue ( prop ?: string , matcher : string | RegExp ) {
42+ export function matchStringValue (
43+ prop ?: string ,
44+ matcher : string | RegExp
45+ ) : boolean {
3546 if ( ! prop ) {
3647 return false ;
3748 }
@@ -46,7 +57,7 @@ export function matchStringValue(prop?: string, matcher: string | RegExp) {
4657export function matchArrayValue (
4758 prop ?: Array < string > ,
4859 matcher : string | Array < string >
49- ) {
60+ ) : boolean {
5061 if ( ! prop || matcher . length === 0 ) {
5162 return false ;
5263 }
@@ -58,6 +69,14 @@ export function matchArrayValue(
5869 return ! matcher . some ( e => ! prop . includes ( e ) ) ;
5970}
6071
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+
6180const a11yAPI = ( instance : ReactTestInstance ) : A11yAPI =>
6281 ( {
6382 ...makeQuery (
@@ -100,6 +119,16 @@ const a11yAPI = (instance: ReactTestInstance): A11yAPI =>
100119 } ,
101120 matchArrayValue
102121 ) ( 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 ) ,
103132 } : any ) ;
104133
105134export default a11yAPI ;
0 commit comments