From a21f55f9492fbbba6bfcc6df33bb26ffa43f17ff Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Wed, 5 Jul 2023 15:05:18 -0400 Subject: [PATCH] Introduce and re-use `turbo_stream_broadcasts` helper --- lib/turbo/broadcastable/test_helper.rb | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/turbo/broadcastable/test_helper.rb b/lib/turbo/broadcastable/test_helper.rb index 275b607a..102aa780 100644 --- a/lib/turbo/broadcastable/test_helper.rb +++ b/lib/turbo/broadcastable/test_helper.rb @@ -60,16 +60,10 @@ module TestHelper def assert_turbo_stream_broadcasts(stream_name_or_object, &block) block&.call - stream_name = stream_name_from(stream_name_or_object) - payloads = broadcasts(stream_name) - - assert payloads.present?, "Expected at least one broadcast on #{stream_name.inspect}, but there were none" - - payloads.flat_map do |payload| - html = ActiveSupport::JSON.decode(payload) - document = Nokogiri::HTML5.parse(html) + turbo_stream_broadcasts(stream_name_or_object).tap do |payloads| + stream_name = stream_name_from(stream_name_or_object) - document.at("body").element_children + assert payloads.present?, "Expected at least one broadcast on #{stream_name.inspect}, but there were none" end end @@ -115,6 +109,18 @@ def assert_no_turbo_stream_broadcasts(stream_name_or_object, &block) assert payloads.empty?, "Expected no broadcasts on #{stream_name.inspect}, but there were #{payloads.count}" end + + def turbo_stream_broadcasts(stream_name_or_object) + stream_name = stream_name_from(stream_name_or_object) + payloads = broadcasts(stream_name) + + payloads.flat_map do |payload| + html = ActiveSupport::JSON.decode(payload) + document = Nokogiri::HTML5.parse(html) + + document.at("body").element_children + end + end end end end