From f0f0dee846c59215f82876a6202711c62d64ff2f Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Wed, 17 Nov 2021 17:28:23 -0500 Subject: [PATCH] Yield `visit()` as part of Event Details Provide handlers with a way to transform the `fetchResponse` into a `Turbo.visit()` call without any knowledge of the internal structure or logic necessary to do so. --- src/core/session.ts | 11 +++++++++-- src/tests/fixtures/frames.html | 11 ++--------- src/tests/functional/frame_tests.ts | 10 ++++++---- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/core/session.ts b/src/core/session.ts index b808a53ba..7e724f203 100644 --- a/src/core/session.ts +++ b/src/core/session.ts @@ -267,8 +267,15 @@ export class Session implements FormSubmitObserverDelegate, HistoryDelegate, Lin this.notifyApplicationAfterFrameRender(fetchResponse, frame); } - async frameMissing(fetchResponse: FetchResponse, target: FrameElement) { - dispatch("turbo:frame-missing", { target, detail: { fetchResponse } }) + frameMissing(fetchResponse: FetchResponse, target: FrameElement) { + const visit = async (options: Partial = {}) => { + const responseHTML = await fetchResponse.responseHTML + const { location, redirected, statusCode } = fetchResponse + + this.visit(location, { response: { redirected, statusCode, responseHTML }, ...options }) + } + + dispatch("turbo:frame-missing", { target, detail: { fetchResponse, visit }, cancelable: true }) } // Application events diff --git a/src/tests/fixtures/frames.html b/src/tests/fixtures/frames.html index 1997aaf83..c7cc7030f 100644 --- a/src/tests/fixtures/frames.html +++ b/src/tests/fixtures/frames.html @@ -10,13 +10,7 @@ if (target.id == "add-turbo-action-to-frame") { target.closest("turbo-frame")?.setAttribute("data-turbo-action", "advance") } else if (target.id == "propose-visit-when-frame-missing") { - addEventListener("turbo:frame-missing", async (event) => { - const { detail: { fetchResponse } } = event - const { location, redirected, statusCode, responseHTML } = fetchResponse - const response = { redirected, statusCode, responseHTML: await responseHTML } - - window.Turbo.visit(location, { response }) - }, { once: true }) + addEventListener("turbo:frame-missing", ({ detail: { visit } }) => visit(), { once: true }) } }) @@ -86,8 +80,7 @@

Frames: #nested-child

Missing frame -
- +
diff --git a/src/tests/functional/frame_tests.ts b/src/tests/functional/frame_tests.ts index 665d3e2f5..72e1ee4f7 100644 --- a/src/tests/functional/frame_tests.ts +++ b/src/tests/functional/frame_tests.ts @@ -424,7 +424,8 @@ export class FrameTests extends TurboDriveTestCase { async "test navigating frame resulting in response without matching frame can be re-purposed to navigate entire page"() { await this.proposeVisitWhenFrameIsMissingInResponse() await this.clickSelector("#missing a") - await this.nextEventNamed("turbo:load") + await this.nextEventOnTarget("missing", "turbo:frame-missing") + await this.nextBody this.assert.notOk(await this.hasSelector("#missing")) this.assert.equal(await (await this.querySelector("h1")).getVisibleText(), "Frames: #frame") @@ -434,7 +435,8 @@ export class FrameTests extends TurboDriveTestCase { async "test submitting frame resulting in response without matching frame can be re-purposed to navigate entire page"() { await this.proposeVisitWhenFrameIsMissingInResponse() await this.clickSelector("#missing button") - await this.nextEventNamed("turbo:load") + await this.nextEventOnTarget("missing", "turbo:frame-missing") + await this.nextBody this.assert.notOk(await this.hasSelector("#missing")) this.assert.equal(await (await this.querySelector("h1")).getVisibleText(), "Frames: #frame") @@ -455,8 +457,8 @@ export class FrameTests extends TurboDriveTestCase { return this.evaluate(() => window.frameScriptEvaluationCount) } - proposeVisitWhenFrameIsMissingInResponse(): Promise { - return this.clickSelector("#propose-visit-when-frame-missing") + async proposeVisitWhenFrameIsMissingInResponse(): Promise { + return await this.clickSelector("#propose-visit-when-frame-missing") } }