From 52db399185166d60b7f18bdadc71f05457c3ed2a Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Mon, 22 Jan 2024 10:18:16 -0500 Subject: [PATCH] Add test coverage for driving `turbo-frame[data-turbo-action]` Closes [#489][] First, add test coverage to exercise the `` and `` as outlined in a [comment on #489][]. Next, add coverage to support driving a `` through the `Turbo.visit` method. To pass that test coverage, invoke `frameElement.delegate.proposeVisitIfNavigatedWithAction(frameElement)` prior to modifying the element's `[src]` attribute. [#489]: https://github.com/hotwired/turbo/issues/489 [comment on #489]: https://github.com/hotwired/turbo/issues/489#issuecomment-1503078653 --- src/core/session.js | 2 +- src/tests/fixtures/frames.html | 4 ++-- src/tests/functional/frame_tests.js | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/core/session.js b/src/core/session.js index 7bfb20ca2..b090c6e3f 100644 --- a/src/core/session.js +++ b/src/core/session.js @@ -98,8 +98,8 @@ export class Session { const frameElement = options.frame ? document.getElementById(options.frame) : null if (frameElement instanceof FrameElement) { + frameElement.delegate.proposeVisitIfNavigatedWithAction(frameElement) frameElement.src = location.toString() - frameElement.loaded } else { this.navigator.proposeVisit(expandURL(location), options) } diff --git a/src/tests/fixtures/frames.html b/src/tests/fixtures/frames.html index d6a82d831..24c3272b9 100644 --- a/src/tests/fixtures/frames.html +++ b/src/tests/fixtures/frames.html @@ -27,7 +27,7 @@

Frames: #frame

- + Navigate #frame from within Navigate #frame with ?key=value Navigate #frame from within with a[data-turbo-action="advance"] @@ -57,7 +57,7 @@

Frames: #frame

Frames: #hello

- Load #frame + Load #frame
diff --git a/src/tests/functional/frame_tests.js b/src/tests/functional/frame_tests.js index b090aff8a..530e18383 100644 --- a/src/tests/functional/frame_tests.js +++ b/src/tests/functional/frame_tests.js @@ -652,6 +652,28 @@ test("navigating turbo-frame[data-turbo-action=advance] from within pushes URL s assert.equal(pathname(page.url()), "/src/tests/fixtures/frames/frame.html") }) +test("navigating turbo-frame[data-turbo-action=advance] from outside with target pushes URL state", async ({ page }) => { + await page.click("#add-turbo-action-to-frame") + await page.click("#hello-link-frame") + await nextEventNamed(page, "turbo:load") + + await expect(page.locator("h1")).toHaveText("Frames") + await expect(page.locator("#frame h2")).toHaveText("Frame: Loaded") + expect(pathname(page.url())).toEqual("/src/tests/fixtures/frames/frame.html") +}) + +test("navigating turbo-frame[data-turbo-action=advance] with Turbo.visit pushes URL state", async ({ page }) => { + const path = "/src/tests/fixtures/frames/frame.html" + + await page.click("#add-turbo-action-to-frame") + await page.evaluate((path) => window.Turbo.visit(path, { frame: "frame" }), path) + await nextEventNamed(page, "turbo:load") + + await expect(page.locator("h1")).toHaveText("Frames") + await expect(page.locator("#frame h2")).toHaveText("Frame: Loaded") + expect(pathname(page.url())).toEqual(path) +}) + test("navigating turbo-frame[data-turbo-action=advance] to the same URL clears the [aria-busy] and [data-turbo-preview] state", async ({ page }) => {