Skip to content

Commit

Permalink
Export StreamActions as property of window.Turbo (#1073)
Browse files Browse the repository at this point in the history
* Export StreamActions as property of window.Turbo

This was removed in #1049
but it's still used in the turbo-rails test suite.

https://github.com/hotwired/turbo-rails/blob/c3ca7009c0759563ec65ecf1bc60a9af1ff33728/test/system/broadcasts_test.rb#L137

* Remove circular dependency

* Assert that Turbo interface is exposed via window global
Alberto Fernández-Capel authored Nov 27, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 325216e commit 4f334da
Showing 6 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion playwright.config.js
Original file line number Diff line number Diff line change
@@ -37,4 +37,4 @@ const config = {
}
}

export default config
export default config
3 changes: 2 additions & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
@@ -3,11 +3,12 @@ import { PageRenderer } from "./drive/page_renderer"
import { PageSnapshot } from "./drive/page_snapshot"
import { FrameRenderer } from "./frames/frame_renderer"
import { FormSubmission } from "./drive/form_submission"
import { StreamActions } from "./streams/stream_actions"
import { fetch } from "../http/fetch"

const session = new Session()
const { cache, navigator } = session
export { navigator, session, cache, PageRenderer, PageSnapshot, FrameRenderer, fetch }
export { navigator, session, cache, PageRenderer, PageSnapshot, FrameRenderer, StreamActions, fetch }

/**
* Starts the main session.
4 changes: 1 addition & 3 deletions src/core/streams/stream_actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { session } from "../"

export const StreamActions = {
after() {
this.targetElements.forEach((e) => e.parentElement?.insertBefore(this.templateContent, e.nextSibling))
@@ -35,6 +33,6 @@ export const StreamActions = {
},

refresh() {
session.refresh(this.baseURI, this.requestId)
window.Turbo.session.refresh(this.baseURI, this.requestId)
}
}
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import * as Turbo from "./core"
window.Turbo = Turbo
Turbo.start()

export { StreamActions } from "./core/streams/stream_actions"
export * from "./core"
export * from "./elements"
export * from "./http"
15 changes: 15 additions & 0 deletions src/tests/fixtures/umd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>UMD</title>
<script type="module" data-turbo-track="reload">
import "/dist/turbo.es2017-umd.js"
</script>
<script src="/src/tests/fixtures/test.js"></script>
<meta name="test" content="foo">
</head>
<body>
<h1>UMD</h1>
</body>
</html>
42 changes: 38 additions & 4 deletions src/tests/functional/import_tests.js
Original file line number Diff line number Diff line change
@@ -3,8 +3,42 @@ import { assert } from "chai"

test("window variable with ESM", async ({ page }) => {
await page.goto("/src/tests/fixtures/esm.html")
const type = await page.evaluate(() => {
return typeof window.Turbo
})
assert.equal(type, "object")
await assertTurboInterface(page)
})

test("window variable with UMD", async ({ page }) => {
await page.goto("/src/tests/fixtures/umd.html")
await assertTurboInterface(page)
})

async function assertTurboInterface(page) {
await assertTypeOf(page, "Turbo", "object")
await assertTypeOf(page, "Turbo.StreamActions", "object")
await assertTypeOf(page, "Turbo.start", "function")
await assertTypeOf(page, "Turbo.registerAdapter", "function")
await assertTypeOf(page, "Turbo.visit", "function")
await assertTypeOf(page, "Turbo.connectStreamSource", "function")
await assertTypeOf(page, "Turbo.disconnectStreamSource", "function")
await assertTypeOf(page, "Turbo.renderStreamMessage", "function")
await assertTypeOf(page, "Turbo.clearCache", "function")
await assertTypeOf(page, "Turbo.setProgressBarDelay", "function")
await assertTypeOf(page, "Turbo.setConfirmMethod", "function")
await assertTypeOf(page, "Turbo.setFormMode", "function")
await assertTypeOf(page, "Turbo.cache", "object")
await assertTypeOf(page, "Turbo.cache.clear", "function")
await assertTypeOf(page, "Turbo.navigator", "object")
await assertTypeOf(page, "Turbo.session", "object")
}

async function assertTypeOf(page, propertyName, propertyType) {
const type = await page.evaluate((propertyName) => {
const parts = propertyName.split(".")
let object = window
parts.forEach((_part, i) => {
object = object[parts[i]]
})
return typeof object
}, propertyName)

assert.equal(type, propertyType, `Expected ${propertyName} to be ${propertyType}`)
}

0 comments on commit 4f334da

Please sign in to comment.