Skip to content

Commit

Permalink
Make data-turbo-confirm work with multiple submitters inside a form; …
Browse files Browse the repository at this point in the history
…add tests (#564)

Co-authored-by: Felipe <felipe@temporadalivre.com>
  • Loading branch information
feliperaul and Felipe authored Apr 30, 2022
1 parent 5e52e25 commit e521872
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/drive/form_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class FormSubmission {
}

get confirmationMessage() {
return this.formElement.getAttribute("data-turbo-confirm")
return this.submitter?.getAttribute("data-turbo-confirm") || this.formElement.getAttribute("data-turbo-confirm")
}

get needsConfirmation() {
Expand Down
1 change: 1 addition & 0 deletions src/tests/fixtures/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ <h1>Form</h1>
</form>
<form action="/__turbo/redirect" method="post" class="confirm" data-turbo-confirm="Are you sure?">
<input type="submit">
<button type="submit" name="greeting" id="secondary_submitter" data-turbo-confirm="Are you really sure?" formaction="/__turbo/redirect?path=/src/tests/fixtures/one.html" formmethod="post" value="secondary_submitter">Secondary action</button>
</form>
</div>
<hr>
Expand Down
20 changes: 20 additions & 0 deletions src/tests/functional/form_submission_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class FormSubmissionTests extends TurboDriveTestCase {
this.assert.equal(await this.getAlertText(), "Are you sure?")
await this.acceptAlert()
this.assert.ok(await this.formSubmitStarted)
this.assert.equal(await this.pathname, "/src/tests/fixtures/one.html")
}

async "test form submission with confirmation cancelled"() {
Expand All @@ -36,6 +37,25 @@ export class FormSubmissionTests extends TurboDriveTestCase {
this.assert.notOk(await this.formSubmitStarted)
}

async "test form submission with secondary submitter click - confirmation confirmed"() {
await this.clickSelector("#standard form.confirm #secondary_submitter")

this.assert.equal(await this.getAlertText(), "Are you really sure?")
await this.acceptAlert()
this.assert.ok(await this.formSubmitStarted)
this.assert.equal(await this.pathname, "/src/tests/fixtures/one.html")
this.assert.equal(await this.visitAction, "advance")
this.assert.equal(await this.getSearchParam("greeting"), "secondary_submitter")
}

async "test form submission with secondary submitter click - confirmation cancelled"() {
await this.clickSelector("#standard form.confirm #secondary_submitter")

this.assert.equal(await this.getAlertText(), "Are you really sure?")
await this.dismissAlert()
this.assert.notOk(await this.formSubmitStarted)
}

async "test from submission with confirmation overriden"() {
await this.remote.execute(() => window.Turbo.setConfirmMethod(() => confirm("Overriden message")))

Expand Down

0 comments on commit e521872

Please sign in to comment.