@@ -3,84 +3,105 @@ import waitFor from '../waitFor';
3
3
import type { WaitForOptions } from '../waitFor' ;
4
4
import { ErrorWithStack } from './errors' ;
5
5
6
- type AllQuery = < T > (
6
+ type QueryArg = string | RegExp ;
7
+
8
+ type QueryFunction < ArgType , ReturnType > = (
7
9
instance : ReactTestInstance
8
- // $FlowFixMe - Property @@iterator is missing in T [1] but exists in $Iterable [2]
9
- ) => ( ...args : T ) => Array < ReactTestInstance > ;
10
-
11
- export function makeGetAllQuery < T > (
12
- allQuery : AllQuery ,
13
- instance : ReactTestInstance ,
14
- getMissingError : ( args : T ) => string
15
- ) : ( ...args : Array < T > ) => Array < ReactTestInstance > {
16
- return function getAllFn ( ...args : Array < T > ) {
17
- const results = allQuery ( instance ) ( ...args ) ;
18
-
19
- if ( results . length === 0 ) {
20
- throw new ErrorWithStack ( getMissingError ( ...args ) , getAllFn ) ;
21
- }
22
-
23
- return results ;
24
- } ;
25
- }
10
+ ) => ( args : ArgType ) => ReturnType ;
26
11
27
- export function makeSingleQuery < T > (
28
- allQuery : AllQuery ,
29
- instance : ReactTestInstance ,
30
- getMultipleError : ( args : T ) => string
31
- ) : ( ...args : Array < T > ) => null | ReactTestInstance {
32
- return function singleQueryFn ( ...args : Array < T > ) {
33
- const results = allQuery ( instance ) ( ...args ) ;
12
+ type FindQueryFunction < ArgType , ReturnType > = (
13
+ instance : ReactTestInstance
14
+ ) => ( args : ArgType , waitForOptions ? : WaitForOptions ) => Promise < ReturnType > ;
34
15
35
- if ( results . length > 1 ) {
36
- throw new ErrorWithStack ( getMultipleError ( ...args ) , singleQueryFn ) ;
37
- }
16
+ type QueryAllByQuery = QueryFunction < QueryArg , Array < ReactTestInstance >> ;
17
+ type QueryByQuery = QueryFunction < QueryArg , null | ReactTestInstance > ;
38
18
39
- if ( results . length === 0 ) {
40
- return null ;
41
- }
19
+ type GetAllByQuery = QueryFunction < QueryArg , Array < ReactTestInstance >> ;
20
+ type GetByQuery = QueryFunction < QueryArg , ReactTestInstance > ;
42
21
43
- return results [ 0 ] ;
44
- } ;
45
- }
22
+ type FindAllByQuery = FindQueryFunction < QueryArg , Array < ReactTestInstance >> ;
23
+ type FindByQuery = FindQueryFunction < QueryArg , ReactTestInstance > ;
46
24
47
- export function makeGetQuery < T > (
48
- allQuery : AllQuery ,
49
- instance : ReactTestInstance ,
50
- getMultipleError : ( args : T ) => string ,
51
- getMissingError : ( args : T ) => string
52
- ) : ( ...args : Array < T > ) => ReactTestInstance {
53
- return function getFn ( ...args : Array < T > ) {
54
- const results = allQuery ( instance ) ( ...args ) ;
25
+ type Queries = {
26
+ getBy : GetByQuery ,
27
+ getAllBy : GetAllByQuery ,
28
+ queryBy : QueryByQuery ,
29
+ findBy : FindByQuery ,
30
+ findAllBy : FindAllByQuery ,
31
+ } ;
55
32
56
- if ( results . length > 1 ) {
57
- throw new ErrorWithStack ( getMultipleError ( ...args ) , getFn ) ;
58
- }
33
+ export function makeQueries (
34
+ queryAllByQuery : QueryAllByQuery ,
35
+ getMissingError : ( args : QueryArg ) = > string ,
36
+ getMultipleError : ( args : QueryArg ) = > string
37
+ ) : Queries {
38
+ function getAllByQuery ( instance : ReactTestInstance ) {
39
+ return function getAllFn ( args : QueryArg ) {
40
+ const results = queryAllByQuery ( instance ) ( args ) ;
59
41
60
- if ( results . length === 0 ) {
61
- throw new ErrorWithStack ( getMissingError ( ... args ) , getFn ) ;
62
- }
42
+ if ( results . length === 0 ) {
43
+ throw new ErrorWithStack ( getMissingError ( args ) , getAllFn ) ;
44
+ }
63
45
64
- return results [ 0 ] ;
65
- } ;
66
- }
46
+ return results ;
47
+ } ;
48
+ }
67
49
68
- export function makeFindAllQuery < T > (
69
- getAllQuery: AllQuery,
70
- instance: ReactTestInstance
71
- ): (
72
- args: T,
73
- waitForOptions?: WaitForOptions
74
- ) => Promise < Array < ReactTestInstance >> {
75
- return function findAllFn ( args : T , waitForOptions : WaitForOptions = { } ) {
76
- return waitFor ( ( ) => getAllQuery ( instance ) ( args ) , waitForOptions) ;
77
- } ;
78
- }
79
- export function makeFindQuery < T > (
80
- getQuery : ( instance : ReactTestInstance ) = > ( args : any ) => ReactTestInstance ,
81
- instance : ReactTestInstance
82
- ) : ( args : T , waitForOptions ? : WaitForOptions ) = > Promise < ReactTestInstance > {
83
- return function findFn ( args : T , waitForOptions : WaitForOptions = { } ) {
84
- return waitFor ( ( ) => getQuery ( instance ) ( args ) , waitForOptions) ;
50
+ function queryByQuery ( instance : ReactTestInstance ) {
51
+ return function singleQueryFn ( args : QueryArg ) {
52
+ const results = queryAllByQuery ( instance ) ( args ) ;
53
+
54
+ if ( results . length > 1 ) {
55
+ throw new ErrorWithStack ( getMultipleError ( args ) , singleQueryFn ) ;
56
+ }
57
+
58
+ if ( results . length === 0 ) {
59
+ return null ;
60
+ }
61
+
62
+ return results [ 0 ] ;
63
+ } ;
64
+ }
65
+
66
+ function getByQuery ( instance : ReactTestInstance ) {
67
+ return function getFn ( args : QueryArg ) {
68
+ const results = queryAllByQuery ( instance ) ( args ) ;
69
+
70
+ if ( results . length > 1 ) {
71
+ throw new ErrorWithStack ( getMultipleError ( args ) , getFn ) ;
72
+ }
73
+
74
+ if ( results . length === 0 ) {
75
+ throw new ErrorWithStack ( getMissingError ( args ) , getFn ) ;
76
+ }
77
+
78
+ return results [ 0 ] ;
79
+ } ;
80
+ }
81
+
82
+ function findAllByQuery ( instance : ReactTestInstance ) {
83
+ return function findAllFn (
84
+ args : QueryArg ,
85
+ waitForOptions ? : WaitForOptions = { }
86
+ ) {
87
+ return waitFor ( ( ) => getAllByQuery ( instance ) ( args ) , waitForOptions ) ;
88
+ } ;
89
+ }
90
+
91
+ function findByQuery ( instance : ReactTestInstance ) {
92
+ return function findFn (
93
+ args : QueryArg ,
94
+ waitForOptions ? : WaitForOptions = { }
95
+ ) {
96
+ return waitFor ( ( ) => getByQuery ( instance ) ( args ) , waitForOptions ) ;
97
+ } ;
98
+ }
99
+
100
+ return {
101
+ getBy : getByQuery ,
102
+ getAllBy : getAllByQuery ,
103
+ queryBy : queryByQuery ,
104
+ findBy : findByQuery ,
105
+ findAllBy : findAllByQuery ,
85
106
} ;
86
107
}
0 commit comments