Skip to content

Commit

Permalink
Fix data-turbo-confirm on <a> without data-turbo-method
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth committed Feb 16, 2023
1 parent e013072 commit 634dd95
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/observers/form_link_click_observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class FormLinkClickObserver implements LinkClickObserverDelegate {
willFollowLinkToLocation(link: Element, location: URL, originalEvent: MouseEvent): boolean {
return (
this.delegate.willSubmitFormLinkToLocation(link, location, originalEvent) &&
link.hasAttribute("data-turbo-method")
(link.hasAttribute("data-turbo-method") || link.hasAttribute("data-turbo-confirm"))
)
}

Expand Down
25 changes: 25 additions & 0 deletions src/tests/functional/visit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,28 @@ test("test Visit with network error", async ({ page }) => {
async function visitLocation(page: Page, location: string) {
return page.evaluate((location) => window.Turbo.visit(location), location)
}

test("test data-turbo-confirm on anchor element without data-turbo-method", async ({ page }) => {
let confirmed = false

page.on("dialog", (alert) => {
assert.equal(alert.message(), "Are you sure?")
alert.accept()
confirmed = true
})

await page.evaluate(() => {
const link = document.querySelector<HTMLAnchorElement>("#same-origin-link")

if (link) link.dataset.turboConfirm = "Are you sure?"
})

assert.equal(await page.locator("#same-origin-link[data-turbo-confirm]:not([data-turbo-method])").count(), 1)
assert.equal(pathname(page.url()), "/src/tests/fixtures/visit.html")

await page.click("#same-origin-link")
await nextEventNamed(page, "turbo:load")

assert.isTrue(confirmed)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
})

0 comments on commit 634dd95

Please sign in to comment.