@@ -3,37 +3,36 @@ import waitFor from '../waitFor';
3
3
import type { WaitForOptions } from '../waitFor' ;
4
4
import { ErrorWithStack } from './errors' ;
5
5
6
- type AllQuery = (
6
+ type AllQuery = < T > (
7
7
instance: ReactTestInstance
8
- ) => ( args : any ) => Array < ReactTestInstance > ;
8
+ ) => ( ... args : T ) => Array < ReactTestInstance > ;
9
9
10
- export const makeGetAllQuery = (
10
+ export function makeGetAllQuery < T > (
11
11
allQuery : AllQuery ,
12
12
instance : ReactTestInstance ,
13
- getMissingElementError : ( args : any ) => string
14
- ) =>
15
- function getAllFn ( ...args : Array < any > ) {
13
+ getMissingError : ( args : T ) => string
14
+ ) : ( ... args : Array < T > ) => Array < ReactTestInstance > {
15
+ return function getAllFn ( ...args : Array < T > ) {
16
16
const results = allQuery ( instance ) ( ...args ) ;
17
- if ( ! results . length ) {
18
- throw new ErrorWithStack ( getMissingElementError ( ...args ) , getAllFn ) ;
17
+
18
+ if ( results . length === 0 ) {
19
+ throw new ErrorWithStack ( getMissingError ( ...args ) , getAllFn ) ;
19
20
}
20
21
21
22
return results ;
22
23
} ;
24
+ }
23
25
24
- export const makeSingleQuery = (
26
+ export function makeSingleQuery < T > (
25
27
allQuery : AllQuery ,
26
28
instance : ReactTestInstance ,
27
- getMissingElementError : ( args : any , nbResults : number ) => string
28
- ) =>
29
- function singleQueryFn ( ...args : Array < any > ) {
29
+ getMultipleError : ( args : T ) => string
30
+ ) : ( ... args : Array < T > ) => null | ReactTestInstance {
31
+ return function singleQueryFn ( ...args : Array < T > ) {
30
32
const results = allQuery ( instance ) ( ...args ) ;
31
33
32
34
if ( results . length > 1 ) {
33
- throw new ErrorWithStack (
34
- getMissingElementError ( ...args , results . length ) ,
35
- singleQueryFn
36
- ) ;
35
+ throw new ErrorWithStack ( getMultipleError ( ...args ) , singleQueryFn ) ;
37
36
}
38
37
39
38
if ( results . length === 0 ) {
@@ -42,37 +41,45 @@ export const makeSingleQuery = (
42
41
43
42
return results [ 0 ] ;
44
43
} ;
44
+ }
45
45
46
- export const makeGetQuery = (
46
+ export function makeGetQuery < T > (
47
47
allQuery : AllQuery ,
48
48
instance : ReactTestInstance ,
49
- getMissingElementError : ( args : any , nbResults : number ) => string
50
- ) =>
51
- function getFn ( ...args : Array < any > ) {
49
+ getMultipleError : ( args : T ) => string ,
50
+ getMissingError : ( args : T ) => string
51
+ ) : ( ...args : Array < T > ) => ReactTestInstance {
52
+ return function getFn ( ...args : Array < T > ) {
52
53
const results = allQuery ( instance ) ( ...args ) ;
53
54
54
- if ( results . length !== 1 ) {
55
- throw new ErrorWithStack (
56
- getMissingElementError ( ...args , results . length ) ,
57
- getFn
58
- ) ;
55
+ if ( results . length > 1 ) {
56
+ throw new ErrorWithStack ( getMultipleError ( ...args ) , getFn ) ;
57
+ }
58
+
59
+ if ( results . length === 0 ) {
60
+ throw new ErrorWithStack ( getMissingError ( ...args ) , getFn ) ;
59
61
}
60
62
61
63
return results [ 0 ] ;
62
64
} ;
65
+ }
63
66
64
- export const makeFindAllQuery = (
67
+ export function makeFindAllQuery < T > (
65
68
getAllQuery: AllQuery,
66
69
instance: ReactTestInstance
67
- ) =>
68
- function findAllFn ( args : any , waitForOptions : WaitForOptions = { } ) {
70
+ ): (
71
+ args: T,
72
+ waitForOptions?: WaitForOptions
73
+ ) => Promise < Array < ReactTestInstance >> {
74
+ return function findAllFn ( args : T , waitForOptions : WaitForOptions = { } ) {
69
75
return waitFor ( ( ) => getAllQuery ( instance ) ( args ) , waitForOptions) ;
70
76
} ;
71
-
72
- export const makeFindQuery = (
77
+ }
78
+ export function makeFindQuery < T > (
73
79
getQuery : ( instance : ReactTestInstance ) = > ( args : any ) => ReactTestInstance ,
74
80
instance : ReactTestInstance
75
- ) =>
76
- function findFn ( args : any , waitForOptions : WaitForOptions = { } ) {
81
+ ) : ( args : T , waitForOptions ? : WaitForOptions ) = > Promise < ReactTestInstance > {
82
+ return function findFn ( args : T , waitForOptions : WaitForOptions = { } ) {
77
83
return waitFor ( ( ) => getQuery ( instance ) ( args ) , waitForOptions) ;
78
84
} ;
85
+ }
0 commit comments