Only submit forms with buttons of type='submit' or that are not type='reset' or type='button' (#4365)

* Fix issue where button of type='reset' would be activated during implicit form submission instead of button type='submit'

* Update to account for the fact that default type of button is 'submit'


Co-authored-by: Ben Kucera <14625260+Bkucera@users.noreply.github.com>
This commit is contained in:
Jennifer Shehane
2019-06-04 10:05:12 +06:30
committed by GitHub
parent 1e640045d9
commit e354d160fb
5 changed files with 62 additions and 3 deletions

View File

@@ -108,4 +108,4 @@
"index.js",
"types/**/*.d.ts"
]
}
}

View File

@@ -162,7 +162,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->
form.find("input, button").filter (__, el) ->
$el = $dom.wrap(el)
($dom.isSelector($el, "input") and $dom.isType($el, "submit")) or
($dom.isSelector($el, "button") and not $dom.isType($el, "button"))
($dom.isSelector($el, "button") and not $dom.isType($el, "button") and not $dom.isType($el, "reset") )
type = ->
simulateSubmitHandler = ->

View File

@@ -255,12 +255,27 @@
<button type="submit">submit me</button>
</form>
<form id="multiple-inputs-and-reset-and-submit-buttons">
<input name="fname" />
<input name="lname" />
<button type="reset">reset</button>
<button type="submit">submit me</button>
</form>
<form id="multiple-inputs-and-button-with-no-type">
<input name="fname" />
<input name="lname" />
<button>submit me</button>
</form>
<form id="multiple-inputs-and-other-type-buttons-and-button-with-no-type">
<input name="fname" />
<input name="lname" />
<button type='button'>button</button>
<button type='reset'>reset</button>
<button>submit me</button>
</form>
<form id="multiple-inputs-and-multiple-submits">
<input name="fname" />
<input name="lname" />

View File

@@ -1109,7 +1109,7 @@ describe "src/cy/commands/actions/click", ->
num = cy.$$("button").length
cy.on "fail", (err) ->
expect(err.message).to.eq "cy.click() can only be called on a single element. Your subject contained 17 elements. Pass { multiple: true } if you want to serially click each element."
expect(err.message).to.eq "cy.click() can only be called on a single element. Your subject contained #{num} elements. Pass { multiple: true } if you want to serially click each element."
done()
cy.get("button").click()

View File

@@ -2624,6 +2624,50 @@ describe "src/cy/commands/actions/type", ->
cy.get("#multiple-inputs-and-button-submit input:first").type("f{enter}").then -> done()
context "2 inputs, 1 'submit' button[type=submit], 1 'reset' button[type=reset]", ->
it "triggers form submit", (done) ->
@$forms.find("#multiple-inputs-and-reset-and-submit-buttons").submit (e) ->
e.preventDefault()
done()
cy.get("#multiple-inputs-and-reset-and-submit-buttons input:first").type("foo{enter}")
it "causes click event on the button[type=submit]", (done) ->
@$forms.find("#multiple-inputs-and-reset-and-submit-buttons button[type=submit]").click (e) ->
e.preventDefault()
done()
cy.get("#multiple-inputs-and-reset-and-submit-buttons input:first").type("foo{enter}")
it "does not cause click event on the button[type=submit] if keydown is defaultPrevented on input", (done) ->
form = @$forms.find("#multiple-inputs-and-reset-and-submit-buttons").submit ->
done("err: should not have submitted")
form.find("input").keypress (e) -> e.preventDefault()
cy.get("#multiple-inputs-and-reset-and-submit-buttons input:first").type("f{enter}").then -> done()
context "2 inputs, 1 'reset' button, 1 'button' button, and 1 button with no type (default submit)", ->
it "triggers form submit", (done) ->
@$forms.find("#multiple-inputs-and-other-type-buttons-and-button-with-no-type").submit (e) ->
e.preventDefault()
done()
cy.get("#multiple-inputs-and-other-type-buttons-and-button-with-no-type input:first").type("foo{enter}")
it "causes click event on the button", (done) ->
@$forms.find("#multiple-inputs-and-other-type-buttons-and-button-with-no-type button:last").click (e) ->
e.preventDefault()
done()
cy.get("#multiple-inputs-and-other-type-buttons-and-button-with-no-type input:first").type("foo{enter}")
it "does not cause click event on the button if keydown is defaultPrevented on input", (done) ->
form = @$forms.find("#multiple-inputs-and-other-type-buttons-and-button-with-no-type").submit ->
done("err: should not have submitted")
form.find("input").keypress (e) -> e.preventDefault()
cy.get("#multiple-inputs-and-other-type-buttons-and-button-with-no-type input:first").type("f{enter}").then -> done()
context "2 inputs, 1 'submit' element button", ->
it "triggers form submit", (done) ->
@$forms.find("#multiple-inputs-and-button-with-no-type").submit (e) ->