@@ -7,42 +7,52 @@ import $errUtils from '../../../cypress/error_utils'
7
7
import { resolveShadowDomInclusion } from '../../../cypress/shadow_dom_utils'
8
8
import { getAliasedRequests , isDynamicAliasingPossible } from '../../net-stubbing/aliasing'
9
9
10
+ interface InternalGetOptions extends Partial < Cypress . Loggable & Cypress . Timeoutable & Cypress . Withinable & Cypress . Shadow > {
11
+ _log ?: any
12
+ _retries ?: number
13
+ filter ?: any
14
+ onRetry ?: Function
15
+ verify ?: boolean
16
+ }
17
+
18
+ interface InternalContainsOptions extends Partial < Cypress . Loggable & Cypress . Timeoutable & Cypress . CaseMatchable & Cypress . Shadow > {
19
+ _log ?: any
20
+ }
21
+
10
22
export default ( Commands , Cypress , cy , state ) => {
11
23
Commands . addAll ( {
12
- // TODO: any -> Partial<Cypress.Loggable & Cypress.Timeoutable & Cypress.Withinable & Cypress.Shadow>
13
- get ( selector , options : any = { } ) {
14
- const userOptions = options
24
+ get ( selector , options : Partial < Cypress . Loggable & Cypress . Timeoutable & Cypress . Withinable & Cypress . Shadow > = { } ) {
15
25
const ctx = this
16
26
17
- if ( ( userOptions === null ) || _ . isArray ( userOptions ) || ! _ . isPlainObject ( userOptions ) ) {
27
+ if ( ( options === null ) || _ . isArray ( options ) || ! _ . isPlainObject ( options ) ) {
18
28
return $errUtils . throwErrByPath ( 'get.invalid_options' , {
19
- args : { options : userOptions } ,
29
+ args : { options } ,
20
30
} )
21
31
}
22
32
23
- options = _ . defaults ( { } , userOptions , {
33
+ const _options : InternalGetOptions = _ . defaults ( { } , options , {
24
34
retry : true ,
25
35
withinSubject : state ( 'withinSubject' ) ,
26
36
log : true ,
27
37
command : null ,
28
38
verify : true ,
29
39
} )
30
40
31
- options . includeShadowDom = resolveShadowDomInclusion ( Cypress , userOptions . includeShadowDom )
41
+ _options . includeShadowDom = resolveShadowDomInclusion ( Cypress , _options . includeShadowDom )
32
42
33
43
let aliasObj
34
44
const consoleProps : Record < string , any > = { }
35
45
const start = ( aliasType ) => {
36
- if ( options . log === false ) {
46
+ if ( _options . log === false ) {
37
47
return
38
48
}
39
49
40
- if ( options . _log == null ) {
41
- options . _log = Cypress . log ( {
50
+ if ( _options . _log == null ) {
51
+ _options . _log = Cypress . log ( {
42
52
message : selector ,
43
53
referencesAlias : ( aliasObj != null && aliasObj . alias ) ? { name : aliasObj . alias } : undefined ,
44
54
aliasType,
45
- timeout : options . timeout ,
55
+ timeout : _options . timeout ,
46
56
consoleProps : ( ) => {
47
57
return consoleProps
48
58
} ,
@@ -51,11 +61,11 @@ export default (Commands, Cypress, cy, state) => {
51
61
}
52
62
53
63
const log = ( value , aliasType = 'dom' ) => {
54
- if ( options . log === false ) {
64
+ if ( _options . log === false ) {
55
65
return
56
66
}
57
67
58
- if ( ! _ . isObject ( options . _log ) ) {
68
+ if ( ! _ . isObject ( _options . _log ) ) {
59
69
start ( aliasType )
60
70
}
61
71
@@ -64,7 +74,7 @@ export default (Commands, Cypress, cy, state) => {
64
74
if ( aliasType === 'dom' ) {
65
75
_ . extend ( obj , {
66
76
$el : value ,
67
- numRetries : options . _retries ,
77
+ numRetries : _options . _retries ,
68
78
} )
69
79
}
70
80
@@ -100,7 +110,7 @@ export default (Commands, Cypress, cy, state) => {
100
110
return consoleProps
101
111
}
102
112
103
- options . _log . set ( obj )
113
+ _options . _log . set ( obj )
104
114
}
105
115
106
116
let allParts
@@ -176,7 +186,7 @@ export default (Commands, Cypress, cy, state) => {
176
186
177
187
log ( subject )
178
188
179
- return cy . verifyUpcomingAssertions ( subject , options , {
189
+ return cy . verifyUpcomingAssertions ( subject , _options , {
180
190
onFail ( err ) {
181
191
// if we are failing because our aliased elements
182
192
// are less than what is expected then we know we
@@ -241,7 +251,7 @@ export default (Commands, Cypress, cy, state) => {
241
251
log ( subject , 'primitive' )
242
252
243
253
const verifyAssertions = ( ) => {
244
- return cy . verifyUpcomingAssertions ( subject , options , {
254
+ return cy . verifyUpcomingAssertions ( subject , _options , {
245
255
ensureExistenceFor : false ,
246
256
onRetry : verifyAssertions ,
247
257
} )
@@ -256,24 +266,24 @@ export default (Commands, Cypress, cy, state) => {
256
266
start ( 'dom' )
257
267
258
268
const setEl = ( $el ) => {
259
- if ( options . log === false ) {
269
+ if ( _options . log === false ) {
260
270
return
261
271
}
262
272
263
273
consoleProps . Yielded = $dom . getElements ( $el )
264
274
consoleProps . Elements = $el != null ? $el . length : undefined
265
275
266
- options . _log . set ( { $el } )
276
+ _options . _log . set ( { $el } )
267
277
}
268
278
269
279
const getElements = ( ) => {
270
280
let $el
271
281
272
282
try {
273
- let scope = options . withinSubject
283
+ let scope : ( typeof _options . withinSubject ) | Node [ ] = _options . withinSubject
274
284
275
- if ( options . includeShadowDom ) {
276
- const root = options . withinSubject ? options . withinSubject [ 0 ] : cy . state ( 'document' )
285
+ if ( _options . includeShadowDom ) {
286
+ const root = _options . withinSubject ? _options . withinSubject [ 0 ] : cy . state ( 'document' )
277
287
const elementsWithShadow = $dom . findAllShadowRoots ( root )
278
288
279
289
scope = elementsWithShadow . concat ( root )
@@ -291,11 +301,11 @@ export default (Commands, Cypress, cy, state) => {
291
301
} catch ( err : any ) {
292
302
// this is usually a sizzle error (invalid selector)
293
303
err . onFail = ( ) => {
294
- if ( options . log === false ) {
304
+ if ( _options . log === false ) {
295
305
return err
296
306
}
297
307
298
- options . _log . error ( err )
308
+ _options . _log . error ( err )
299
309
}
300
310
301
311
throw err
@@ -304,8 +314,8 @@ export default (Commands, Cypress, cy, state) => {
304
314
// if that didnt find anything and we have a within subject
305
315
// and we have been explictly told to filter
306
316
// then just attempt to filter out elements from our within subject
307
- if ( ! $el . length && options . withinSubject && options . filter ) {
308
- const filtered = options . withinSubject . filter ( selector )
317
+ if ( ! $el . length && _options . withinSubject && _options . filter ) {
318
+ const filtered = ( _options . withinSubject as JQuery ) . filter ( selector )
309
319
310
320
// reset $el if this found anything
311
321
if ( filtered . length ) {
@@ -318,8 +328,8 @@ export default (Commands, Cypress, cy, state) => {
318
328
319
329
// allow retry to be a function which we ensure
320
330
// returns truthy before returning its
321
- if ( _ . isFunction ( options . onRetry ) ) {
322
- const ret = options . onRetry . call ( ctx , $el )
331
+ if ( _ . isFunction ( _options . onRetry ) ) {
332
+ const ret = _options . onRetry . call ( ctx , $el )
323
333
324
334
if ( ret ) {
325
335
log ( $el )
@@ -335,11 +345,11 @@ export default (Commands, Cypress, cy, state) => {
335
345
336
346
const resolveElements = ( ) => {
337
347
return Promise . try ( getElements ) . then ( ( $el ) => {
338
- if ( options . verify === false ) {
348
+ if ( _options . verify === false ) {
339
349
return $el
340
350
}
341
351
342
- return cy . verifyUpcomingAssertions ( $el , options , {
352
+ return cy . verifyUpcomingAssertions ( $el , _options , {
343
353
onRetry : resolveElements ,
344
354
} )
345
355
} )
@@ -350,8 +360,7 @@ export default (Commands, Cypress, cy, state) => {
350
360
} )
351
361
352
362
Commands . addAll ( { prevSubject : [ 'optional' , 'window' , 'document' , 'element' ] } , {
353
- // TODO: any -> Partial<Cypress.Loggable & Cypress.Timeoutable & Cypress.CaseMatchable & Cypress.Shadow>
354
- contains ( subject , filter , text , options : any = { } ) {
363
+ contains ( subject , filter , text , options : Partial < Cypress . Loggable & Cypress . Timeoutable & Cypress . CaseMatchable & Cypress . Shadow > = { } ) {
355
364
let userOptions = options
356
365
357
366
// nuke our subject if its present but not an element.
@@ -389,7 +398,7 @@ export default (Commands, Cypress, cy, state) => {
389
398
$errUtils . throwErrByPath ( 'contains.regex_conflict' )
390
399
}
391
400
392
- options = _ . defaults ( { } , userOptions , { log : true , matchCase : true } )
401
+ const _options : InternalContainsOptions = _ . defaults ( { } , userOptions , { log : true , matchCase : true } )
393
402
394
403
if ( ! ( _ . isString ( text ) || _ . isFinite ( text ) || _ . isRegExp ( text ) ) ) {
395
404
$errUtils . throwErrByPath ( 'contains.invalid_argument' )
@@ -435,39 +444,39 @@ export default (Commands, Cypress, cy, state) => {
435
444
436
445
let consoleProps
437
446
438
- if ( options . log !== false ) {
447
+ if ( _options . log !== false ) {
439
448
consoleProps = {
440
449
Content : text ,
441
450
'Applied To' : $dom . getElements ( subject || state ( 'withinSubject' ) ) ,
442
451
}
443
452
444
- options . _log = Cypress . log ( {
453
+ _options . _log = Cypress . log ( {
445
454
message : _ . compact ( [ filter , text ] ) ,
446
455
type : subject ? 'child' : 'parent' ,
447
- timeout : options . timeout ,
456
+ timeout : _options . timeout ,
448
457
consoleProps : ( ) => {
449
458
return consoleProps
450
459
} ,
451
460
} )
452
461
}
453
462
454
463
const setEl = ( $el ) => {
455
- if ( options . log === false ) {
464
+ if ( _options . log === false ) {
456
465
return
457
466
}
458
467
459
468
consoleProps . Yielded = $dom . getElements ( $el )
460
469
consoleProps . Elements = $el != null ? $el . length : undefined
461
470
462
- options . _log . set ( { $el } )
471
+ _options . _log . set ( { $el } )
463
472
}
464
473
465
474
// find elements by the :cy-contains psuedo selector
466
475
// and any submit inputs with the attributeContainsWord selector
467
- const selector = $dom . getContainsSelector ( text , filter , options )
476
+ const selector = $dom . getContainsSelector ( text , filter , _options )
468
477
469
478
const resolveElements = ( ) => {
470
- const getOptions = _ . extend ( { } , options , {
479
+ const getOptions = _ . extend ( { } , _options , {
471
480
// error: getErr(text, phrase)
472
481
withinSubject : subject || state ( 'withinSubject' ) || cy . $$ ( 'body' ) ,
473
482
filter : true ,
@@ -483,13 +492,13 @@ export default (Commands, Cypress, cy, state) => {
483
492
484
493
setEl ( $el )
485
494
486
- return cy . verifyUpcomingAssertions ( $el , options , {
495
+ return cy . verifyUpcomingAssertions ( $el , _options , {
487
496
onRetry : resolveElements ,
488
497
onFail ( err ) {
489
498
switch ( err . type ) {
490
499
case 'length' :
491
500
if ( err . expected > 1 ) {
492
- return $errUtils . throwErrByPath ( 'contains.length_option' , { onFail : options . _log } )
501
+ return $errUtils . throwErrByPath ( 'contains.length_option' , { onFail : _options . _log } )
493
502
}
494
503
495
504
break
0 commit comments