Skip to content

Commit

Permalink
Return Promise<void> from FrameElement.reload
Browse files Browse the repository at this point in the history
Following the pattern established by `Turbo.visit(...)` and
`Turbo.visit(..., { frame: "..." })`, return the `FrameElement.loaded`
promise from calls to `FrameElement.reload()`.

That way, callers can block on it:

```js
const frame = document.getElementById("my-frame")
await frame.reload()
// ...
```
  • Loading branch information
seanpdoyle committed Aug 1, 2022
1 parent 11591fd commit f64957b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/elements/frame_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ export class FrameElement extends HTMLElement {
this.delegate.disconnect()
}

reload() {
reload(): Promise<void> {
const { src } = this
this.removeAttribute("complete")
this.src = null
this.src = src
return this.loaded
}

attributeChangedCallback(name: string) {
Expand Down
9 changes: 4 additions & 5 deletions src/tests/functional/loading_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { assert } from "chai"
import {
attributeForSelector,
hasSelector,
nextAttributeMutationNamed,
nextBeat,
nextBody,
nextEventNamed,
Expand Down Expand Up @@ -103,18 +104,16 @@ test("test changing src attribute on a frame with loading=eager navigates", asyn
})

test("test reloading a frame reloads the content", async ({ page }) => {
await nextBeat()

await page.click("#loading-eager summary")
await nextBeat()
await nextEventOnTarget(page, "frame", "turbo:frame-load")

const frameContent = "#loading-eager turbo-frame#frame h2"
assert.ok(await hasSelector(page, frameContent))
assert.ok(await hasSelector(page, "#loading-eager turbo-frame[complete]"), "has [complete] attribute")
assert.equal(await nextAttributeMutationNamed(page, "frame", "complete"), "", "has [complete] attribute")

await page.evaluate(() => (document.querySelector("#loading-eager turbo-frame") as any)?.reload())
assert.ok(await hasSelector(page, frameContent))
assert.ok(await hasSelector(page, "#loading-eager turbo-frame:not([complete])"), "clears [complete] attribute")
assert.equal(await nextAttributeMutationNamed(page, "frame", "complete"), null, "clears [complete] attribute")
})

test("test navigating away from a page does not reload its frames", async ({ page }) => {
Expand Down

0 comments on commit f64957b

Please sign in to comment.