Skip to content

Commit

Permalink
fixes 174, contains was retrying without being cancelled after Cypres…
Browse files Browse the repository at this point in the history
…s.abort was called
  • Loading branch information
brian-mann committed Jun 17, 2016
1 parent 1a680d0 commit ea8c283
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
18 changes: 13 additions & 5 deletions app/js/driver/cypress/cy/commands/querying.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ $Cypress.register "Querying", (Cypress, _, $) ->

$contains = $expr.contains

restoreContains = ->
$expr.contains = $contains

Cypress.on "abort", restoreContains

Cypress.addParentCommand
get: (selector, options = {}) ->
_.defaults options,
Expand Down Expand Up @@ -301,7 +306,7 @@ $Cypress.register "Querying", (Cypress, _, $) ->
## throw here to cause the .catch to trigger
throw new Error()

do resolveElements = =>
resolveElements = =>
@execute("get", selector, getOpts).then ($elements) =>
$el = switch
when $elements and $elements.length
Expand All @@ -321,10 +326,13 @@ $Cypress.register "Querying", (Cypress, _, $) ->
when "existence"
err.longMessage = getErr(err)
})
.finally ->
## always restore contains in case
## we used a regexp!
$expr.contains = $contains

Promise
.try(resolveElements)
.finally ->
## always restore contains in case
## we used a regexp!
restoreContains()

Cypress.addChildCommand
within: (subject, options, fn) ->
Expand Down
67 changes: 64 additions & 3 deletions spec/client/driver/cy/querying_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,30 @@ describe "$Cypress.Cy Querying Commands", ->
## to 300 after successfully finished get method
expect(@cy._timeout()).to.eq 300

it "cancels existing promises", (done) ->
logs = []

@Cypress.on "log", (log) ->
logs.push(log)

retrys = 0

abort = _.after 2, =>
@Cypress.abort()

@cy.on "cancel", ->
_.delay ->
expect(retrys).to.eq(2)
console.log logs
done()
, 500

@cy.on "retry", ->
retrys += 1
abort()

@cy.get("doesNotExist")

describe "custom elements", ->
## <foobarbazquux>custom element</foobarbazquux>

Expand Down Expand Up @@ -1024,18 +1048,39 @@ describe "$Cypress.Cy Querying Commands", ->
it "finds text by regexp and restores contains", ->
contains = $.expr[":"].contains

cy.contains(/^asdf \d+/).then ($li) ->
@cy.contains(/^asdf \d+/).then ($li) ->
expect($li).to.have.text("asdf 1")
expect($.expr[":"].contains).to.eq(contains)

it "returns elements found first when multiple siblings found", ->
cy.contains("li", "asdf").then ($li) ->
@cy.contains("li", "asdf").then ($li) ->
expect($li).to.have.text("asdf 1")

it "returns first ul when multiple uls", ->
cy.contains("ul", "jkl").then ($ul) ->
@cy.contains("ul", "jkl").then ($ul) ->
expect($ul.find("li:first")).to.have.text("jkl 1")

it "cancels existing promises", (done) ->
get = @sandbox.spy(@cy.sync, "get")

retrys = 0

abort = _.after 2, =>
@Cypress.abort()

@cy.on "cancel", ->
_.delay ->
expect(get.callCount).to.be.within(2, 3)
expect(retrys).to.eq(2)
done()
, 50

@cy.on "retry", ->
retrys += 1
abort()

@cy.contains("DOES NOT CONTAIN THIS!")

describe "deprecated command options", ->
beforeEach ->
@allowErrors()
Expand Down Expand Up @@ -1357,3 +1402,19 @@ describe "$Cypress.Cy Querying Commands", ->

cy.contains(/^asdf \d+/)

it "restores contains on abort", (done) ->
@timeout(1000)

contains = $.expr[":"].contains

@cy.on "cancel", ->
_.delay ->
expect($.expr[":"].contains).to.eq(contains)
done()
, 50

@cy.on "retry", _.after 2, ->
@Cypress.abort()

@cy.contains(/^does not contain asdfasdf at all$/)

0 comments on commit ea8c283

Please sign in to comment.