-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use navigatable associated form unless submitter opts out (#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: #1246 (comment)
- Loading branch information
1 parent
9fb05e3
commit 15db627
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Form</title> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
<script src="/src/tests/fixtures/test.js"></script> | ||
</head> | ||
<body data-turbo="false"> | ||
<form id="optin-form" data-turbo="true" action="/__turbo/redirect" method="post" class="redirect"> | ||
<input type="hidden" name="path" value="/src/tests/fixtures/form.html"> | ||
<input type="hidden" name="greeting" value="Hello from a redirect"> | ||
</form> | ||
<button id="default-behavior-submit" form="optin-form" type="submit" name="button">Submit</button> | ||
<button id="opted-out-submit" data-turbo="false" form="optin-form" type="submit" name="button">Submit</button> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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") | ||
}) | ||
|
||
function formSubmitStarted(page) { | ||
return getFromLocalStorage(page, "formSubmitStarted") | ||
} | ||
|
||
function formSubmitEnded(page) { | ||
return getFromLocalStorage(page, "formSubmitEnded") | ||
} |