Skip to content

Commit 16f7613

Browse files
Vivalldiseanpdoyle
authored andcommitted
Only return messages produced by block in capture_turbo_stream_broadcasts
1 parent dda27a8 commit 16f7613

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/turbo/broadcastable/test_helper.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ def assert_no_turbo_stream_broadcasts(stream_name_or_object, &block)
155155
# assert_equal "replace", remove["action"]
156156
#
157157
def capture_turbo_stream_broadcasts(stream_name_or_object, &block)
158-
block&.call
159-
160158
stream_name = stream_name_from(stream_name_or_object)
161-
payloads = broadcasts(stream_name)
159+
payloads = if block_given?
160+
new_broadcasts_from(broadcasts(stream_name), stream_name, "capture_turbo_stream_broadcasts", &block)
161+
else
162+
broadcasts(stream_name)
163+
end
162164

163165
payloads.flat_map do |payload|
164166
html = ActiveSupport::JSON.decode(payload)

test/broadcastable/test_helper_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,22 @@ class Turbo::Broadcastable::TestHelper::AssertTurboStreamBroadcastsTest < Active
193193
end
194194
end
195195
end
196+
197+
test "#capture_turbo_stream_broadcasts only returns new <turbo-stream> elements when block given" do
198+
message = Message.new(id: 1)
199+
200+
message.broadcast_update_to "messages"
201+
message.broadcast_refresh_to "messages"
202+
203+
replace, remove, *rest = capture_turbo_stream_broadcasts "messages" do
204+
message.broadcast_replace_to "messages"
205+
message.broadcast_remove_to "messages"
206+
end
207+
208+
assert_empty rest
209+
assert_equal "replace", replace["action"]
210+
assert_equal "remove", remove["action"]
211+
end
196212
end
197213

198214
class Turbo::Broadcastable::TestHelper::AssertNoTurboStreamBroadcastsTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)