Skip to content

Commit

Permalink
fix: Allow submit button to be outside of the form for implicit submi…
Browse files Browse the repository at this point in the history
…ssion (#21279)

Co-authored-by: Zach Bloomquist <github@chary.us>
Co-authored-by: Zach Bloomquist <git@chary.us>
  • Loading branch information
3 people committed May 5, 2022
1 parent 05ef83a commit aa6f3b8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/driver/cypress/fixtures/dom.html
Expand Up @@ -323,6 +323,12 @@
<button type="submit">submit me</button>
</form>

<form id="multiple-inputs-and-button-submit.outside-form">
<input name="fname" />
<input name="lname" />
</form>
<button form="multiple-inputs-and-button-submit.outside-form" type="submit">submit me</button>

<form id="multiple-inputs-and-reset-and-submit-buttons">
<input name="fname" />
<input name="lname" />
Expand Down
Expand Up @@ -1486,6 +1486,16 @@ describe('src/cy/commands/actions/type - #type special chars', () => {
cy.get('#multiple-inputs-and-button-submit input:first').type('foo{enter}')
})

it('triggers form submit when the submit button is outside of the form', function (done) {
this.$forms.find('[id="multiple-inputs-and-button-submit.outside-form"]').submit((e) => {
e.preventDefault()

done()
})

cy.get('[id="multiple-inputs-and-button-submit.outside-form"] input:first').type('foo{enter}')
})

it('causes click event on the button[type=submit]', function (done) {
this.$forms.find('#multiple-inputs-and-button-submit button[type=submit]').click((e) => {
e.preventDefault()
Expand Down
10 changes: 9 additions & 1 deletion packages/driver/src/cy/commands/actions/type.ts
Expand Up @@ -173,7 +173,15 @@ export default function (Commands, Cypress, cy, state, config) {
const win = state('window')

const getDefaultButtons = (form) => {
return form.find('input, button').filter((__, el) => {
const formId = CSS.escape(form.attr('id'))
const nestedButtons = form.find('input, button')

const possibleDefaultButtons: JQuery<any> = formId ? $dom.wrap(_.uniq([
...nestedButtons,
...$dom.query('body', form.prop('ownerDocument')).find(`input[form="${formId}"], button[form="${formId}"]`),
])) : nestedButtons

return possibleDefaultButtons.filter((__, el) => {
const $el = $dom.wrap(el)

return (
Expand Down

2 comments on commit aa6f3b8

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on aa6f3b8 May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.6.1/linux-x64/develop-aa6f3b8039495ac37ced4ac581129a020ba66fa4/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on aa6f3b8 May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.6.1/darwin-x64/develop-aa6f3b8039495ac37ced4ac581129a020ba66fa4/cypress.tgz

Please sign in to comment.