Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export StreamActions as property of window.Turbo #1073

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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.
Expand Down
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))
Expand Down Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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}`)
}