Skip to content

Commit

Permalink
Introduce and re-use turbo_stream_broadcasts helper
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpdoyle committed Jul 5, 2023
1 parent 4d3d59c commit a21f55f
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/turbo/broadcastable/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit a21f55f

Please sign in to comment.