Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reload page if failed form changes tracked content #759

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/core/native/browser_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ export class BrowserAdapter implements Adapter {
reload(reason: ReloadReason) {
dispatch("turbo:reload", { detail: reason })

if (!this.location) return

window.location.href = this.location.toString()
window.location.href = this.location?.toString() || window.location.href
}

get navigator() {
Expand Down
1 change: 1 addition & 0 deletions src/tests/fixtures/rendering.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<h1>Rendering</h1>
<p><a id="same-origin-link" href="/src/tests/fixtures/one.html">Same-origin link</a></p>
<p><a id="tracked-asset-change-link" href="/src/tests/fixtures/tracked_asset_change.html">Tracked asset change</a></p>
<form id="tracked-asset-change-form" action="/__turbo/notfound" method="post"><button type="submit">Submit</button></form>
<p><a id="tracked-nonce-tag-link" href="/src/tests/fixtures/tracked_nonce_tag.html">Tracked nonce tag</a></p>
<p><a id="additional-assets-link" href="/src/tests/fixtures/additional_assets.html">Additional assets</a></p>
<p><a id="head-script-link" href="/src/tests/fixtures/head_script.html">Head script</a></p>
Expand Down
31 changes: 31 additions & 0 deletions src/tests/functional/rendering_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ test("test reloads when tracked elements change", async ({ page }) => {
assert.equal(reason, "tracked_element_mismatch")
})

test("test reloads when tracked elements change due to failed form submission", async ({ page }) => {
await page.click("#tracked-asset-change-form button")
await page.evaluate(() => {
window.addEventListener(
"turbo:reload",
(e: any) => {
localStorage.setItem("reason", e.detail.reason)
},
{ once: true }
)

window.addEventListener(
"beforeunload",
() => {
localStorage.setItem("unloaded", "true")
},
{ once: true }
)
})

await page.click("#tracked-asset-change-form button")

const reason = await page.evaluate(() => localStorage.getItem("reason"))
const unloaded = await page.evaluate(() => localStorage.getItem("unloaded"))

assert.equal(pathname(page.url()), "/src/tests/fixtures/rendering.html")
assert.equal(await visitAction(page), "load")
assert.equal(reason, "tracked_element_mismatch")
assert.equal(unloaded, "true")
})

test("test before-render event supports custom render function", async ({ page }) => {
await page.evaluate(() =>
addEventListener("turbo:before-render", (event) => {
Expand Down
4 changes: 4 additions & 0 deletions src/tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ router.post("/messages", (request, response) => {
}
})

router.post("/notfound", (request, response) => {
response.type("html").status(404).send("<html><body><h1>Not found</h1></body></html>")
})

router.get("/stream-response", (request, response) => {
const params = { ...request.body, ...request.query }
const { content, target, targets } = params
Expand Down