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

Conversation

afcapel
Copy link
Collaborator

@afcapel afcapel commented Nov 23, 2023

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

@seanpdoyle can you have a look, please?

@brunoprietog
Copy link
Collaborator

Thanks! I've upgraded to beta and can confirm that custom actions are not working. Turbo.StreamActions is undefined

@seanpdoyle
Copy link
Contributor

seanpdoyle commented Nov 27, 2023

The exports should be equivalent between the reverted change and this one. I'm not sure why this is necessary.

There's existing test coverage introduced prior to this change that guarantees Turbo.StreamActions is exported.

I was unsure of whether or not the export test was sufficient, so I experimented with adding more specific coverage locally. The following tests passed when executed on main:

diff --git a/src/tests/unit/export_tests.js b/src/tests/unit/export_tests.js
index 7b7ddf3..e3e06cc 100644
--- a/src/tests/unit/export_tests.js
+++ b/src/tests/unit/export_tests.js
@@ -3,7 +3,6 @@ import * as Turbo from "../../"
 import { StreamActions } from "../../"
 
 test("Turbo interface", () => {
-  assert.equal(typeof Turbo.StreamActions, "object")
   assert.equal(typeof Turbo.start, "function")
   assert.equal(typeof Turbo.registerAdapter, "function")
   assert.equal(typeof Turbo.visit, "function")
@@ -20,6 +19,28 @@ test("Turbo interface", () => {
   assert.equal(typeof Turbo.session, "object")
 })
 
+test("Turbo.StreamActions interface", () => {
+  assert.equal(Turbo.StreamActions, StreamActions)
+  assert.equal(typeof Turbo.StreamActions, "object")
+  assert.equal(typeof Turbo.StreamActions.after, "function")
+  assert.equal(typeof Turbo.StreamActions.append, "function")
+  assert.equal(typeof Turbo.StreamActions.before, "function")
+  assert.equal(typeof Turbo.StreamActions.prepend, "function")
+  assert.equal(typeof Turbo.StreamActions.remove, "function")
+  assert.equal(typeof Turbo.StreamActions.replace, "function")
+  assert.equal(typeof Turbo.StreamActions.update, "function")
+  assert.equal(typeof Turbo.StreamActions.refresh, "function")
+})
+
 test("StreamActions interface", () => {
+  assert.equal(StreamActions, Turbo.StreamActions)
   assert.equal(typeof StreamActions, "object")
+  assert.equal(typeof StreamActions.after, "function")
+  assert.equal(typeof StreamActions.append, "function")
+  assert.equal(typeof StreamActions.before, "function")
+  assert.equal(typeof StreamActions.prepend, "function")
+  assert.equal(typeof StreamActions.remove, "function")
+  assert.equal(typeof StreamActions.replace, "function")
+  assert.equal(typeof StreamActions.update, "function")
+  assert.equal(typeof StreamActions.refresh, "function")
 })

Copy link
Member

@jorgemanrubia jorgemanrubia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @afcapel

@afcapel
Copy link
Collaborator Author

afcapel commented Nov 27, 2023

@seanpdoyle I think what is happening is that StreamActions is being exported as part of the module, but not set up inside window.Turbo. It's a bit confusing, but what we set as window.Turbo here is just the exports of core/index.js, not the whole Turbo module.

The unit tests pass because the asserting the exports from the module, not the global window.Turbo. I've added some browser tests to check window.Turbo as well.

@afcapel afcapel merged commit 4f334da into main Nov 27, 2023
2 checks passed
@afcapel afcapel deleted the stream-actions-global branch November 27, 2023 10:33
seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Dec 1, 2023
Follow-up to [hotwired#1049][]
Follow-up to [hotwired#1073][]
Related to [hotwired#1078][]

Replace an internal `window.Turbo.session` access with an `import {
session }` statement.

Prior attempts at this change introduced circular dependencies. This
attempt does not. Additionally, this change maintains the ability for
consumers to access StreamActions through `window.Turbo.StreamActions`,
while also enabling `import { StreamActions } from "@hotwired/turbo"`
(or `from "@hotwired/turbo-rails"`).

[hotwired#1049]: hotwired#1049
[hotwired#1073]: hotwired#1073
[hotwired#1078]: hotwired#1078
domchristie pushed a commit to domchristie/turbo that referenced this pull request Jul 20, 2024
* Export StreamActions as property of window.Turbo

This was removed in hotwired#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
domchristie pushed a commit to domchristie/turbo that referenced this pull request Jul 20, 2024
Follow-up to [hotwired#1049][]
Follow-up to [hotwired#1073][]
Related to [hotwired#1078][]

Replace an internal `window.Turbo.session` access with an `import {
session }` statement.

Prior attempts at this change introduced circular dependencies. This
attempt does not. Additionally, this change maintains the ability for
consumers to access StreamActions through `window.Turbo.StreamActions`,
while also enabling `import { StreamActions } from "@hotwired/turbo"`
(or `from "@hotwired/turbo-rails"`).

[hotwired#1049]: hotwired#1049
[hotwired#1073]: hotwired#1073
[hotwired#1078]: hotwired#1078
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants