Skip to content

Commit

Permalink
added {length: n} command options + specs
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mann committed Apr 30, 2015
1 parent 290bc9b commit 89f96c4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/js/api/cypress/cy/commands/querying.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $Cypress.register "Querying", (Cypress, _, $) ->
_.defaults options,
retry: true
withinSubject: @prop("withinSubject")
length: null
visible: null
exist: true
exists: true
Expand Down Expand Up @@ -114,6 +115,14 @@ $Cypress.register "Querying", (Cypress, _, $) ->
length = $el.length

switch
when options.length isnt null
if not _.isFinite(options.length)
@throwErr("options.length must be a number")

if length is options.length
log($el)
return $el

when options.exist is false
## return if we didnt find anything and our options have asked
## us for the element not to exist
Expand Down Expand Up @@ -145,6 +154,11 @@ $Cypress.register "Querying", (Cypress, _, $) ->

getErr = ->
err = switch
when options.length isnt null
if $el.length > options.length
"Too many elements found. Found '#{$el.length}', expected '#{options.length}':"
else
"Not enough elements found. Found '#{$el.length}', expected '#{options.length}':"
when options.exist is false #and not $el.length
"Found existing element:"
when options.visible is false and $el.length
Expand Down
13 changes: 13 additions & 0 deletions app/js/api/cypress/cy/commands/traversals.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $Cypress.register "Traversals", (Cypress, _, $) ->
options ?= {}

_.defaults options,
length: null
visible: null
exist: true
exists: true
Expand Down Expand Up @@ -63,6 +64,13 @@ $Cypress.register "Traversals", (Cypress, _, $) ->
length = $el.length

switch
when options.length isnt null
if not _.isFinite(options.length)
@throwErr("options.length must be a number")

if length is options.length
log($el)

when options.exist is false
## return if we didnt find anything and our options have asked
## us for the element not to exist
Expand Down Expand Up @@ -90,6 +98,11 @@ $Cypress.register "Traversals", (Cypress, _, $) ->

getErr = ->
err = switch
when options.length isnt null
if $el.length > options.length
"Too many elements found. Found '#{$el.length}', expected '#{options.length}':"
else
"Not enough elements found. Found '#{$el.length}', expected '#{options.length}':"
when options.exist is false #and not $el.length
"Found existing element:"
when options.visible is false and $el.length
Expand Down
45 changes: 45 additions & 0 deletions spec/client/api/cy/querying_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,26 @@ describe "$Cypress.Cy Querying Commands", ->
@cy.get("#button", {visible: true}).then ($button) ->
expect($button.get(0)).to.eq button.get(0)

describe "{length: n}", ->
it "resolves once length equals n", ->
forms = @cy.$("form")

@cy.get("form", {length: forms.length}).then ($forms) ->
expect($forms.length).to.eq forms.length

it "retries until length equals n", ->
buttons = @cy.$("button")

length = buttons.length - 2

@cy.on "retry", _.after 2, =>
buttons.last().remove()
buttons = @cy.$("button")

## should resolving after removing 2 buttons
@cy.get("button", {length: length}).then ($buttons) ->
expect($buttons.length).to.eq length

describe ".log", ->
beforeEach ->
@Cypress.on "log", (@log) =>
Expand Down Expand Up @@ -548,6 +568,31 @@ describe "$Cypress.Cy Querying Commands", ->
beforeEach ->
@allowErrors()

it "throws when options.length isnt a number", (done) ->
@cy.on "fail", (err) ->
expect(err.message).to.include "options.length must be a number"
done()

@cy.get("button", {length: "asdf"})

it "throws on too many elements after timing out waiting for length", (done) ->
buttons = @cy.$("button")

@cy.on "fail", (err) ->
expect(err.message).to.include "Too many elements found. Found '#{buttons.length}', expected '#{buttons.length - 1}': button"
done()

@cy.get("button", {length: buttons.length - 1})

it "throws on too few elements after timing out waiting for length", (done) ->
buttons = @cy.$("button")

@cy.on "fail", (err) ->
expect(err.message).to.include "Not enough elements found. Found '#{buttons.length}', expected '#{buttons.length + 1}': button"
done()

@cy.get("button", {length: buttons.length + 1})

it "throws after timing out not finding element", (done) ->
@cy.get("#missing-el")

Expand Down
32 changes: 31 additions & 1 deletion spec/client/api/cy/traversals_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,38 @@ describe "$Cypress.Cy Traversal Commands", ->

describe "errors", ->
beforeEach ->
@currentTest.timeout(150)
@allowErrors()

it "throws when options.length isnt a number", (done) ->
@cy.on "fail", (err) ->
expect(err.message).to.include "options.length must be a number"
done()

@cy.get("#list")[name](arg, {length: "asdf"})

it "throws on too many elements after timing out waiting for length", (done) ->
el = @cy.$("#list")[name](arg)

node = $Cypress.Utils.stringifyElement @cy.$("#list"), "short"

@cy.on "fail", (err) ->
expect(err.message).to.include "Too many elements found. Found '#{el.length}', expected '#{el.length - 1}': #{arg ? ''} from #{node}"
done()

@cy.get("#list")[name](arg, {length: el.length - 1})

it "throws on too few elements after timing out waiting for length", (done) ->
el = @cy.$("#list")[name](arg)

node = $Cypress.Utils.stringifyElement @cy.$("#list"), "short"

@cy.on "fail", (err) ->
expect(err.message).to.include "Not enough elements found. Found '#{el.length}', expected '#{el.length + 1}': #{arg ? ''} from #{node}"
done()

@cy.get("#list")[name](arg, {length: el.length + 1})

it "without a dom element", (done) ->
@cy.noop({})[name](arg)
@cy.on "fail", -> done()
Expand All @@ -45,7 +75,7 @@ describe "$Cypress.Cy Traversal Commands", ->
@cy._timeout(100)

errIncludes = (el, node) =>
node = $Cypress.Utils.stringifyElement cy.$(node), "short"
node = $Cypress.Utils.stringifyElement @cy.$(node), "short"

@cy.on "fail", (err) ->
expect(err.message).to.include "Could not find element: #{el} from #{node}"
Expand Down

0 comments on commit 89f96c4

Please sign in to comment.