Skip to content

Commit 9f2abe6

Browse files
committed
querying.ts
1 parent 95258db commit 9f2abe6

File tree

1 file changed

+51
-42
lines changed

1 file changed

+51
-42
lines changed

packages/driver/src/cy/commands/querying/querying.ts

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,52 @@ import $errUtils from '../../../cypress/error_utils'
77
import { resolveShadowDomInclusion } from '../../../cypress/shadow_dom_utils'
88
import { getAliasedRequests, isDynamicAliasingPossible } from '../../net-stubbing/aliasing'
99

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+
1022
export default (Commands, Cypress, cy, state) => {
1123
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> = {}) {
1525
const ctx = this
1626

17-
if ((userOptions === null) || _.isArray(userOptions) || !_.isPlainObject(userOptions)) {
27+
if ((options === null) || _.isArray(options) || !_.isPlainObject(options)) {
1828
return $errUtils.throwErrByPath('get.invalid_options', {
19-
args: { options: userOptions },
29+
args: { options },
2030
})
2131
}
2232

23-
options = _.defaults({}, userOptions, {
33+
const _options: InternalGetOptions = _.defaults({}, options, {
2434
retry: true,
2535
withinSubject: state('withinSubject'),
2636
log: true,
2737
command: null,
2838
verify: true,
2939
})
3040

31-
options.includeShadowDom = resolveShadowDomInclusion(Cypress, userOptions.includeShadowDom)
41+
_options.includeShadowDom = resolveShadowDomInclusion(Cypress, _options.includeShadowDom)
3242

3343
let aliasObj
3444
const consoleProps: Record<string, any> = {}
3545
const start = (aliasType) => {
36-
if (options.log === false) {
46+
if (_options.log === false) {
3747
return
3848
}
3949

40-
if (options._log == null) {
41-
options._log = Cypress.log({
50+
if (_options._log == null) {
51+
_options._log = Cypress.log({
4252
message: selector,
4353
referencesAlias: (aliasObj != null && aliasObj.alias) ? { name: aliasObj.alias } : undefined,
4454
aliasType,
45-
timeout: options.timeout,
55+
timeout: _options.timeout,
4656
consoleProps: () => {
4757
return consoleProps
4858
},
@@ -51,11 +61,11 @@ export default (Commands, Cypress, cy, state) => {
5161
}
5262

5363
const log = (value, aliasType = 'dom') => {
54-
if (options.log === false) {
64+
if (_options.log === false) {
5565
return
5666
}
5767

58-
if (!_.isObject(options._log)) {
68+
if (!_.isObject(_options._log)) {
5969
start(aliasType)
6070
}
6171

@@ -64,7 +74,7 @@ export default (Commands, Cypress, cy, state) => {
6474
if (aliasType === 'dom') {
6575
_.extend(obj, {
6676
$el: value,
67-
numRetries: options._retries,
77+
numRetries: _options._retries,
6878
})
6979
}
7080

@@ -100,7 +110,7 @@ export default (Commands, Cypress, cy, state) => {
100110
return consoleProps
101111
}
102112

103-
options._log.set(obj)
113+
_options._log.set(obj)
104114
}
105115

106116
let allParts
@@ -176,7 +186,7 @@ export default (Commands, Cypress, cy, state) => {
176186

177187
log(subject)
178188

179-
return cy.verifyUpcomingAssertions(subject, options, {
189+
return cy.verifyUpcomingAssertions(subject, _options, {
180190
onFail (err) {
181191
// if we are failing because our aliased elements
182192
// are less than what is expected then we know we
@@ -241,7 +251,7 @@ export default (Commands, Cypress, cy, state) => {
241251
log(subject, 'primitive')
242252

243253
const verifyAssertions = () => {
244-
return cy.verifyUpcomingAssertions(subject, options, {
254+
return cy.verifyUpcomingAssertions(subject, _options, {
245255
ensureExistenceFor: false,
246256
onRetry: verifyAssertions,
247257
})
@@ -256,24 +266,24 @@ export default (Commands, Cypress, cy, state) => {
256266
start('dom')
257267

258268
const setEl = ($el) => {
259-
if (options.log === false) {
269+
if (_options.log === false) {
260270
return
261271
}
262272

263273
consoleProps.Yielded = $dom.getElements($el)
264274
consoleProps.Elements = $el != null ? $el.length : undefined
265275

266-
options._log.set({ $el })
276+
_options._log.set({ $el })
267277
}
268278

269279
const getElements = () => {
270280
let $el
271281

272282
try {
273-
let scope = options.withinSubject
283+
let scope: (typeof _options.withinSubject) | Node[] = _options.withinSubject
274284

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')
277287
const elementsWithShadow = $dom.findAllShadowRoots(root)
278288

279289
scope = elementsWithShadow.concat(root)
@@ -291,11 +301,11 @@ export default (Commands, Cypress, cy, state) => {
291301
} catch (err: any) {
292302
// this is usually a sizzle error (invalid selector)
293303
err.onFail = () => {
294-
if (options.log === false) {
304+
if (_options.log === false) {
295305
return err
296306
}
297307

298-
options._log.error(err)
308+
_options._log.error(err)
299309
}
300310

301311
throw err
@@ -304,8 +314,8 @@ export default (Commands, Cypress, cy, state) => {
304314
// if that didnt find anything and we have a within subject
305315
// and we have been explictly told to filter
306316
// 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)
309319

310320
// reset $el if this found anything
311321
if (filtered.length) {
@@ -318,8 +328,8 @@ export default (Commands, Cypress, cy, state) => {
318328

319329
// allow retry to be a function which we ensure
320330
// 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)
323333

324334
if (ret) {
325335
log($el)
@@ -335,11 +345,11 @@ export default (Commands, Cypress, cy, state) => {
335345

336346
const resolveElements = () => {
337347
return Promise.try(getElements).then(($el) => {
338-
if (options.verify === false) {
348+
if (_options.verify === false) {
339349
return $el
340350
}
341351

342-
return cy.verifyUpcomingAssertions($el, options, {
352+
return cy.verifyUpcomingAssertions($el, _options, {
343353
onRetry: resolveElements,
344354
})
345355
})
@@ -350,8 +360,7 @@ export default (Commands, Cypress, cy, state) => {
350360
})
351361

352362
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> = {}) {
355364
let userOptions = options
356365

357366
// nuke our subject if its present but not an element.
@@ -389,7 +398,7 @@ export default (Commands, Cypress, cy, state) => {
389398
$errUtils.throwErrByPath('contains.regex_conflict')
390399
}
391400

392-
options = _.defaults({}, userOptions, { log: true, matchCase: true })
401+
const _options: InternalContainsOptions = _.defaults({}, userOptions, { log: true, matchCase: true })
393402

394403
if (!(_.isString(text) || _.isFinite(text) || _.isRegExp(text))) {
395404
$errUtils.throwErrByPath('contains.invalid_argument')
@@ -435,39 +444,39 @@ export default (Commands, Cypress, cy, state) => {
435444

436445
let consoleProps
437446

438-
if (options.log !== false) {
447+
if (_options.log !== false) {
439448
consoleProps = {
440449
Content: text,
441450
'Applied To': $dom.getElements(subject || state('withinSubject')),
442451
}
443452

444-
options._log = Cypress.log({
453+
_options._log = Cypress.log({
445454
message: _.compact([filter, text]),
446455
type: subject ? 'child' : 'parent',
447-
timeout: options.timeout,
456+
timeout: _options.timeout,
448457
consoleProps: () => {
449458
return consoleProps
450459
},
451460
})
452461
}
453462

454463
const setEl = ($el) => {
455-
if (options.log === false) {
464+
if (_options.log === false) {
456465
return
457466
}
458467

459468
consoleProps.Yielded = $dom.getElements($el)
460469
consoleProps.Elements = $el != null ? $el.length : undefined
461470

462-
options._log.set({ $el })
471+
_options._log.set({ $el })
463472
}
464473

465474
// find elements by the :cy-contains psuedo selector
466475
// and any submit inputs with the attributeContainsWord selector
467-
const selector = $dom.getContainsSelector(text, filter, options)
476+
const selector = $dom.getContainsSelector(text, filter, _options)
468477

469478
const resolveElements = () => {
470-
const getOptions = _.extend({}, options, {
479+
const getOptions = _.extend({}, _options, {
471480
// error: getErr(text, phrase)
472481
withinSubject: subject || state('withinSubject') || cy.$$('body'),
473482
filter: true,
@@ -483,13 +492,13 @@ export default (Commands, Cypress, cy, state) => {
483492

484493
setEl($el)
485494

486-
return cy.verifyUpcomingAssertions($el, options, {
495+
return cy.verifyUpcomingAssertions($el, _options, {
487496
onRetry: resolveElements,
488497
onFail (err) {
489498
switch (err.type) {
490499
case 'length':
491500
if (err.expected > 1) {
492-
return $errUtils.throwErrByPath('contains.length_option', { onFail: options._log })
501+
return $errUtils.throwErrByPath('contains.length_option', { onFail: _options._log })
493502
}
494503

495504
break

0 commit comments

Comments
 (0)