Skip to content

Commit

Permalink
Add test coverage for driving turbo-frame[data-turbo-action]
Browse files Browse the repository at this point in the history
Closes [hotwired#489][]

First, add test coverage to exercise the `<turbo-frame id="frame">` and
`<turbo-frame id="hello" target="frame">` as outlined in a
[comment on hotwired#489][].

Next, add coverage to support driving a `<turbo-frame
data-turbo-action="advance">` through the `Turbo.visit` method. To pass
that test coverage, invoke
`frameElement.delegate.proposeVisitIfNavigatedWithAction(frameElement)`
prior to modifying the element's `[src]` attribute.

[hotwired#489]: hotwired#489
[comment on hotwired#489]: hotwired#489 (comment)
  • Loading branch information
seanpdoyle committed Jan 22, 2024
1 parent 95c98d4 commit 52db399
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/tests/fixtures/frames.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h2>Frames: #frame</h2>
<form action="/src/tests/fixtures/frames/frame.html">
<button id="frame-form-get-no-redirect">Navigate #frame without a redirect</button>
</form>
<button id="add-turbo-action-to-frame" type="button">Add [data-turbo-action="advance"] to frame</button>
<button id="add-turbo-action-to-frame" type="button">Add [data-turbo-action="advance"] to #frame</button>
<a id="link-frame" href="/src/tests/fixtures/frames/frame.html">Navigate #frame from within</a>
<a id="link-frame-with-search-params" href="/src/tests/fixtures/frames/frame.html?key=value">Navigate #frame with ?key=value</a>
<a id="link-nested-frame-action-advance" href="/src/tests/fixtures/frames/frame.html" data-turbo-action="advance">Navigate #frame from within with a[data-turbo-action="advance"]</a>
Expand Down Expand Up @@ -57,7 +57,7 @@ <h2>Frames: #frame</h2>
<turbo-frame id="hello" target="frame">
<h2>Frames: #hello</h2>

<a href="/src/tests/fixtures/frames/frame.html">Load #frame</a>
<a id="hello-link-frame" href="/src/tests/fixtures/frames/frame.html">Load #frame</a>
<button type="button" id="remove-target-from-hello">Remove #hello[target]</button>

</turbo-frame>
Expand Down
22 changes: 22 additions & 0 deletions src/tests/functional/frame_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}) => {
Expand Down

0 comments on commit 52db399

Please sign in to comment.