-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into turbo-7-2-0
* main: Improve upon test suite flakiness (#327) Give html: rendering param the same treatment as content: (#362)
- Loading branch information
Showing
21 changed files
with
86 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
test/dummy/app/views/application/_template_button.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<button type="button"> | ||
<%= content %> | ||
|
||
<script> | ||
document.currentScript.parentElement.addEventListener("click", ({ currentTarget }) => { | ||
for (const template of currentTarget.querySelectorAll("template")) { | ||
currentTarget.parentElement.insertBefore(template.content, currentTarget) | ||
} | ||
currentTarget.remove() | ||
}) | ||
</script> | ||
|
||
<template><%= yield %></template> | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
<h1>Echo Messages</h1> | ||
|
||
<%= turbo_stream_from "messages", channel: EchoChannel, data: {message: "Hello, world!"} %> | ||
<%= render "template_button", content: "Start listening for broadcasts" do %> | ||
<%= turbo_stream_from "messages", channel: EchoChannel, data: {message: "Hello, world!"} %> | ||
<% end %> | ||
|
||
<div id="messages"> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
<h1>Messages</h1> | ||
|
||
<span id="message-count"> | ||
<%= @messages.count %> messages sent | ||
</span> | ||
<%= render "template_button", content: "Start listening for broadcasts" do %> | ||
<%= turbo_stream_from "messages" %> | ||
<% end %> | ||
|
||
<%= turbo_stream_from "messages" %> | ||
<div id="messages"> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
<h1>Users::Profiles</h1> | ||
|
||
<%= turbo_stream_from "users_profiles" %> | ||
<div id="users_profiles"></div> | ||
<%= render "template_button", content: "Start listening for broadcasts" do %> | ||
<%= turbo_stream_from "users_profiles" %> | ||
<% end %> | ||
|
||
<div id="users_profiles"> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ development: | |
adapter: async | ||
|
||
test: | ||
adapter: async | ||
adapter: inline | ||
|
||
production: | ||
adapter: redis | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require "turbo_test" | ||
require "test_helper" | ||
|
||
class TestChannel < ApplicationCable::Channel; end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,70 @@ | ||
require "application_system_test_case" | ||
|
||
class BroadcastsTest < ApplicationSystemTestCase | ||
setup { Message.delete_all } | ||
|
||
include ActionCable::TestHelper | ||
|
||
test "Message broadcasts Turbo Streams" do | ||
visit messages_path | ||
wait_for_subscriber | ||
subscribe_to_broadcasts | ||
|
||
assert_text "Messages" | ||
assert_broadcasts_text "Message 1" do |text| | ||
Message.create(content: text).broadcast_append_to(:messages) | ||
assert_broadcasts_text "Message 1", to: :messages do |text, target| | ||
Message.create(content: text).broadcast_append_to(target) | ||
end | ||
end | ||
|
||
test "New messages update the message count with html" do | ||
test "Message broadcasts with html: render option" do | ||
visit messages_path | ||
wait_for_subscriber | ||
|
||
assert_text "Messages" | ||
message = Message.create(content: "A new message") | ||
subscribe_to_broadcasts | ||
|
||
message.broadcast_update_to(:messages, target: "message-count", | ||
html: "#{Message.count} messages sent") | ||
assert_selector("#message-count", text: Message.count, wait: 10) | ||
assert_broadcasts_text "Hello, with html: option", to: :messages do |text, target| | ||
Message.create(content: "Ignored").broadcast_append_to(target, html: text) | ||
end | ||
end | ||
|
||
test "Users::Profile broadcasts Turbo Streams" do | ||
visit users_profiles_path | ||
wait_for_subscriber | ||
subscribe_to_broadcasts | ||
|
||
assert_text "Users::Profiles" | ||
assert_broadcasts_text "Profile 1" do |text| | ||
Users::Profile.new(id: 1, name: text).broadcast_append_to(:users_profiles) | ||
assert_broadcasts_text "Profile 1", to: :users_profiles do |text, target| | ||
Users::Profile.new(id: 1, name: text).broadcast_append_to(target) | ||
end | ||
end | ||
|
||
test "passing extra parameters to channel" do | ||
visit echo_messages_path | ||
wait_for_subscriber | ||
|
||
assert_text "Hello, world!", wait: 100 | ||
assert_broadcasts_text "Hello, world!", to: :messages do | ||
subscribe_to_broadcasts | ||
end | ||
end | ||
|
||
private | ||
|
||
def assert_broadcasts_text(text, wait: 5, &block) | ||
assert_no_text text | ||
perform_enqueued_jobs { block.call(text) } | ||
assert_text text, wait: wait | ||
def subscribe_to_broadcasts | ||
click_on "Start listening for broadcasts" | ||
|
||
assert_no_button "Start listening for broadcasts" | ||
|
||
Timeout.timeout(Capybara.default_max_wait_time) { wait_for_subscriber } | ||
end | ||
|
||
def assert_broadcasts_text(text, to:, &block) | ||
within(:element, id: to) { assert_no_text text } | ||
|
||
[text, to].yield_self(&block) | ||
|
||
within(:element, id: to) { assert_text text } | ||
end | ||
|
||
def wait_for_subscriber(timeout: 10) | ||
time = Time.now | ||
def wait_for_subscriber | ||
loop do | ||
subscriber_map = pubsub_adapter.instance_variable_get(:@subscriber_map) | ||
subscriber_map = ActionCable.server.pubsub.instance_variable_get(:@subscriber_map) | ||
if subscriber_map.is_a?(ActionCable::SubscriptionAdapter::SubscriberMap) | ||
subscribers = subscriber_map.instance_variable_get(:@subscribers) | ||
sync = subscriber_map.instance_variable_get(:@sync) | ||
sync.synchronize do | ||
return unless subscribers.empty? | ||
end | ||
end | ||
assert_operator(Time.now - time, :<, timeout, "subscriber waiting timed out") | ||
sleep 0.1 | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters