@@ -25,13 +25,15 @@ export type QueryAllByQuery<Predicate, Options = void> = (
25
25
26
26
export type FindByQuery < Predicate , Options = void > = (
27
27
predicate : Predicate ,
28
- options ?: Options ,
28
+ // Remove `& WaitForOptions` when all queries have been migrated to support 2nd arg query options.
29
+ options ?: Options & WaitForOptions ,
29
30
waitForOptions ?: WaitForOptions
30
31
) => Promise < ReactTestInstance > ;
31
32
32
33
export type FindAllByQuery < Predicate , Options = void > = (
33
34
predicate : Predicate ,
34
- options ?: Options ,
35
+ // Remove `& WaitForOptions` when all queries have been migrated to support 2nd arg query options.
36
+ options ?: Options & WaitForOptions ,
35
37
waitForOptions ?: WaitForOptions
36
38
) => Promise < ReactTestInstance [ ] > ;
37
39
@@ -46,6 +48,41 @@ export type UnboundQueries<Predicate, Options> = {
46
48
findAllBy : UnboundQuery < FindAllByQuery < Predicate , Options > > ;
47
49
} ;
48
50
51
+ const deprecatedKeys : ( keyof WaitForOptions ) [ ] = [
52
+ 'timeout' ,
53
+ 'interval' ,
54
+ 'stackTraceError' ,
55
+ ] ;
56
+
57
+ // The WaitForOptions has been moved to the second option param of findBy* methods with the adding of TextMatchOptions
58
+ // To make the migration easier and avoid a breaking change, keep reading this options from the first param but warn
59
+ function extractDeprecatedWaitForOptions ( options ?: WaitForOptions ) {
60
+ if ( ! options ) {
61
+ return undefined ;
62
+ }
63
+
64
+ const waitForOptions : WaitForOptions = {
65
+ timeout : options . timeout ,
66
+ interval : options . interval ,
67
+ stackTraceError : options . stackTraceError ,
68
+ } ;
69
+
70
+ deprecatedKeys . forEach ( ( key ) => {
71
+ const option = options [ key ] ;
72
+ if ( option ) {
73
+ // eslint-disable-next-line no-console
74
+ console . warn (
75
+ `Use of option "${ key } " in a findBy* query options (2nd parameter) is deprecated. Please pass this option in the waitForOptions (3rd parameter).
76
+ Example:
77
+
78
+ findByText(text, {}, { ${ key } : ${ option . toString ( ) } })`
79
+ ) ;
80
+ }
81
+ } ) ;
82
+
83
+ return waitForOptions ;
84
+ }
85
+
49
86
export function makeQueries < Predicate , Options > (
50
87
queryAllByQuery : UnboundQuery < QueryAllByQuery < Predicate , Options > > ,
51
88
getMissingError : ( predicate : Predicate , options ?: Options ) => string ,
@@ -101,26 +138,30 @@ export function makeQueries<Predicate, Options>(
101
138
function findAllByQuery ( instance : ReactTestInstance ) {
102
139
return function findAllFn (
103
140
predicate : Predicate ,
104
- queryOptions ?: Options ,
105
- waitForOptions ? : WaitForOptions
141
+ queryOptions ?: Options & WaitForOptions ,
142
+ waitForOptions : WaitForOptions = { }
106
143
) {
107
- return waitFor (
108
- ( ) => getAllByQuery ( instance ) ( predicate , queryOptions ) ,
109
- waitForOptions
110
- ) ;
144
+ const deprecatedWaitForOptions =
145
+ extractDeprecatedWaitForOptions ( queryOptions ) ;
146
+ return waitFor ( ( ) => getAllByQuery ( instance ) ( predicate , queryOptions ) , {
147
+ ...deprecatedWaitForOptions ,
148
+ ...waitForOptions ,
149
+ } ) ;
111
150
} ;
112
151
}
113
152
114
153
function findByQuery ( instance : ReactTestInstance ) {
115
154
return function findFn (
116
155
predicate : Predicate ,
117
- queryOptions ?: Options ,
118
- waitForOptions ? : WaitForOptions
156
+ queryOptions ?: Options & WaitForOptions ,
157
+ waitForOptions : WaitForOptions = { }
119
158
) {
120
- return waitFor (
121
- ( ) => getByQuery ( instance ) ( predicate , queryOptions ) ,
122
- waitForOptions
123
- ) ;
159
+ const deprecatedWaitForOptions =
160
+ extractDeprecatedWaitForOptions ( queryOptions ) ;
161
+ return waitFor ( ( ) => getByQuery ( instance ) ( predicate , queryOptions ) , {
162
+ ...deprecatedWaitForOptions ,
163
+ ...waitForOptions ,
164
+ } ) ;
124
165
} ;
125
166
}
126
167
0 commit comments