Skip to content

Commit c29f383

Browse files
datnerflotwig
authored andcommitted
Issue #6216 -- have .its() receive the number 0 as parameter (#6234)
* add support and tests for 0 as path for its() * Fix typo in README of driver * export prop checking to a helper function * add tests for .invoke() cases
1 parent ae81d5d commit c29f383

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

packages/driver/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ If you want to run tests in Chrome and keep it open after the spec finishes, you
7777

7878
```bash
7979
npm run cypress:run -- --config testFiles=e2e/focus_blur_spec.js --browser chrome --no-exit
80+
```
81+
8082
If you would like to run a particular integration test, see the GUI and poke around during the test, you can an exclusive test like:
8183

8284
```bash

packages/driver/src/cy/commands/connectors.coffee

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ module.exports = (Commands, Cypress, cy, state, config) ->
168168

169169
return ".#{str}(" + $utils.stringify(args) + ")"
170170

171+
## to allow the falsy value 0 to be used
172+
isProp = (str) -> !!str or str is 0
173+
171174
message = getMessage()
172175

173176
traversalErr = null
@@ -179,7 +182,8 @@ module.exports = (Commands, Cypress, cy, state, config) ->
179182
consoleProps: ->
180183
Subject: subject
181184

182-
if not str
185+
## check for false positive (negative?) with 0 given as index
186+
if not isProp(str)
183187
$utils.throwErrByPath("invoke_its.null_or_undefined_property_name", {
184188
onFail: options._log
185189
args: { cmd: name, identifier: if isCmdIts then "property" else "function" }
@@ -253,7 +257,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->
253257

254258
## if we're attempting to tunnel into
255259
## a null or undefined object...
256-
if prop and valIsNullOrUndefined
260+
if isProp(prop) and valIsNullOrUndefined
257261
if index is 0
258262
## give an error stating the current subject is nil
259263
traversalErr = subjectNullOrUndefinedErr(prop, acc)
@@ -265,7 +269,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->
265269
return acc
266270

267271
## if we have no more properties to traverse
268-
if not prop
272+
if not isProp(prop)
269273
if valIsNullOrUndefined
270274
## set traversal error that the final value is null or undefined
271275
traversalErr = propertyValueNullOrUndefinedErr(previousProp, acc)

packages/driver/test/cypress/integration/commands/connectors_spec.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ describe "src/cy/commands/connectors", ->
345345

346346
cy.noop([_.noop, fn]).invoke(1).should('be.true')
347347

348+
it "works with 0 as a value if object has property 0", ->
349+
i = 0
350+
fn = ->
351+
if i++ is 0 then "cy.noop is undocumented"
352+
else "and I don't understand what it is"
353+
354+
cy.wrap([fn, "bar"]).invoke(0).should("eq", "cy.noop is undocumented")
355+
cy.wrap({"0": fn}).invoke(0).should("eq", "and I don't understand what it is")
356+
357+
348358
it "forwards any additional arguments", ->
349359
cy.noop(@obj).invoke("bar", 1, 2).then (num) ->
350360
expect(num).to.eq 3
@@ -813,6 +823,11 @@ describe "src/cy/commands/connectors", ->
813823
it "works with numerical indexes", ->
814824
cy.wrap(['foo', 'bar']).its(1).should('eq', 'bar')
815825

826+
it "works with 0 as a value if object has property 0", ->
827+
cy.wrap(["foo", "bar"]).its(0).should("eq", "foo")
828+
cy.wrap({"0": "whoa"}).its(0).should("eq", "whoa")
829+
cy.wrap([###empty###, "spooky"]).its(0).should("not.exist")
830+
816831
it "reduces into dot separated values", ->
817832
obj = {
818833
foo: {

0 commit comments

Comments
 (0)