Skip to content

Conversation

@oliverguenther
Copy link

@oliverguenther oliverguenther commented Apr 23, 2024

When using a submitter with [form=id] attribute, it should respect the form's navigatable state unless the submitter explicitly opts out of turbo.

Implements the expected behavior outlined in this comment:
#1246 (comment)

Related issue: #1246

)

When using a submitter with [form=id] attribute, it should respect the
form's navigatable state unless the submitter explicitly opts out of
turbo.

Implements the expected behavior outlined in this comment:
hotwired#1246 (comment)
@oliverguenther oliverguenther force-pushed the associated-form-submitter-opt-in branch from 9d9ac17 to 15db627 Compare April 23, 2024 07:12
Comment on lines +430 to +431
} else if (this.submitterReferencesNavigatable(form, submitter)) {
return true
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this work as a recursive call to submissionIsNavigatable(submitter.form) (intentionally omitting the submitter argument) to re-use the existing code?

Suggested change
} else if (this.submitterReferencesNavigatable(form, submitter)) {
return true
} else if (this.elementIsNavigatable(submitter) && submitter.form instanceof HTMLFormElement) {
return this.submissionIsNavgatable(submitter.form)

Comment on lines +1 to +30
import { test } from "@playwright/test"
import { assert } from "chai"
import { getFromLocalStorage, nextEventNamed, readEventLogs, setLocalStorageFromEvent } from "../helpers/page"

test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/form_turbo_optin_submitter.html")
await setLocalStorageFromEvent(page, "turbo:submit-start", "formSubmitStarted", "true")
await setLocalStorageFromEvent(page, "turbo:submit-end", "formSubmitEnded", "true")
await readEventLogs(page)
})

test("allows submitting an associated data-turbo=true form without data-turbo=true on submitter", async ({ page }) => {
await page.click("#default-behavior-submit")

assert.ok(await formSubmitStarted(page), "fires turbo:submit-start")

const { fetchOptions } = await nextEventNamed(page, "turbo:before-fetch-request")

assert.ok(fetchOptions.headers["Accept"].includes("text/vnd.turbo-stream.html"))

await nextEventNamed(page, "turbo:before-fetch-response")

assert.ok(await formSubmitEnded(page), "fires turbo:submit-end")
})

test("prevents submitting an associated data-turbo=true form when explicitly opted out", async ({ page }) => {
await page.click("#opted-out-submit")

assert.notOk(await formSubmitStarted(page), "fires turbo:submit-start")
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Could these assert calls be re-written to use Playwright's expect instead?

Suggested change
import { test } from "@playwright/test"
import { assert } from "chai"
import { getFromLocalStorage, nextEventNamed, readEventLogs, setLocalStorageFromEvent } from "../helpers/page"
test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/form_turbo_optin_submitter.html")
await setLocalStorageFromEvent(page, "turbo:submit-start", "formSubmitStarted", "true")
await setLocalStorageFromEvent(page, "turbo:submit-end", "formSubmitEnded", "true")
await readEventLogs(page)
})
test("allows submitting an associated data-turbo=true form without data-turbo=true on submitter", async ({ page }) => {
await page.click("#default-behavior-submit")
assert.ok(await formSubmitStarted(page), "fires turbo:submit-start")
const { fetchOptions } = await nextEventNamed(page, "turbo:before-fetch-request")
assert.ok(fetchOptions.headers["Accept"].includes("text/vnd.turbo-stream.html"))
await nextEventNamed(page, "turbo:before-fetch-response")
assert.ok(await formSubmitEnded(page), "fires turbo:submit-end")
})
test("prevents submitting an associated data-turbo=true form when explicitly opted out", async ({ page }) => {
await page.click("#opted-out-submit")
assert.notOk(await formSubmitStarted(page), "fires turbo:submit-start")
})
import { expect, test } from "@playwright/test"
import { getFromLocalStorage, nextEventNamed, readEventLogs, setLocalStorageFromEvent } from "../helpers/page"
test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/form_turbo_optin_submitter.html")
await setLocalStorageFromEvent(page, "turbo:submit-start", "formSubmitStarted", "true")
await setLocalStorageFromEvent(page, "turbo:submit-end", "formSubmitEnded", "true")
await readEventLogs(page)
})
test("allows submitting an associated data-turbo=true form without data-turbo=true on submitter", async ({ page }) => {
await page.click("#default-behavior-submit")
expect(await formSubmitStarted(page), "fires turbo:submit-start").toBeTruthy()
const { fetchOptions } = await nextEventNamed(page, "turbo:before-fetch-request")
expect(fetchOptions.headers["Accept"]).toContain("text/vnd.turbo-stream.html")
await nextEventNamed(page, "turbo:before-fetch-response")
expect(await formSubmitEnded(page), "fires turbo:submit-end").toBeTruthy()
})
test("prevents submitting an associated data-turbo=true form when explicitly opted out", async ({ page }) => {
await page.click("#opted-out-submit")
expect(await formSubmitStarted(page), "fires turbo:submit-start").not.toBeTruthy()
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants