module Turbo::Broadcastable::TestHelper

def assert_no_turbo_stream_broadcasts(stream_name_or_object, &block)


end
# do something other than broadcast to "message_1"
assert_no_turbo_stream_broadcasts message do

message = Message.find(1)

determine the name of the channel the elements are broadcast to:
In addition to a String, the helper also accepts an Object or Array to

end
# do something other than broadcast to "messages"
assert_no_turbo_stream_broadcasts "messages" do

message = Message.find(1)

You can pass a block to run before the assertion:

assert_no_turbo_stream_broadcasts "messages" # fails with MiniTest::Assertion error

message.broadcast_replace_to "messages"
message = Message.find(1)

Asserts that no `` elements were broadcast:

assertion
* &block optional block executed before the
channel Action Cable name, or the name itself
* stream_name_or_object the objects used to generate the

==== Arguments

Asserts that no `` elements were broadcast over Action Cable
def assert_no_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.empty?, "Expected no broadcasts on #{stream_name.inspect}, but there were #{payloads.count}"
end

def assert_turbo_stream_broadcasts(stream_name_or_object, count: nil, &block)


end
message.broadcast_replace
assert_turbo_stream_broadcasts message do

message = Message.find(1)

determine the name of the channel the elements are broadcast to:
In addition to a String, the helper also accepts an Object or Array to

end
message.broadcast_append_to "messages"
assert_turbo_stream_broadcasts "messages" do

message = Message.find(1)

You can pass a block to run before the assertion:

assert_turbo_stream_broadcasts "messages", count: 2

message.broadcast_remove_to "messages"
message.broadcast_replace_to "messages"
message = Message.find(1)

Asserts that two `` elements were broadcast:

assert_turbo_stream_broadcasts "messages"

message.broadcast_replace_to "messages"
message = Message.find(1)

Asserts `` elements were broadcast:

expected to be broadcast
* count: the number of `` elements that are

==== Options

assertion
* &block optional block executed before the
channel Action Cable name, or the name itself
* stream_name_or_object the objects used to generate the

==== Arguments

Asserts that `` elements were broadcast over Action Cable
def assert_turbo_stream_broadcasts(stream_name_or_object, count: nil, &block)
  payloads = capture_turbo_stream_broadcasts(stream_name_or_object, &block)
  stream_name = stream_name_from(stream_name_or_object)
  if count.nil?
    assert_not_empty payloads, "Expected at least one broadcast on #{stream_name.inspect}, but there were none"
  else
    broadcasts = "Turbo Stream broadcast".pluralize(count)
    assert count == payloads.count, "Expected #{count} #{broadcasts} on #{stream_name.inspect}, but there were #{payloads.count}"
  end
end

def capture_turbo_stream_broadcasts(stream_name_or_object, &block)


assert_equal "replace", remove["action"]
assert_equal "replace", replace["action"]

end
message.broadcast_remove
message.broadcast_replace
replace, remove = capture_turbo_stream_broadcasts message do

message = Message.find(1)

determine the name of the channel the elements are broadcast to:
In addition to a String, the helper also accepts an Object or Array to

assert_equal "append", turbo_streams.first["action"]

end
message.broadcast_append_to "messages"
turbo_streams = capture_turbo_stream_broadcasts "messages" do

message = Message.find(1)

You can pass a block to limit the scope of the broadcasts being captured:

assert_equal "prepend", turbo_streams.second["action"]
assert_equal "append", turbo_streams.first["action"]

turbo_streams = capture_turbo_stream_broadcasts "messages"

message.broadcast_prepend_to "messages"
message.broadcast_append_to "messages"
message = Message.find(1)

Array of Nokogiri::XML::Element instances
Returns any `` elements that have been broadcast as an

* &block optional block to capture broadcasts during execution
channel Action Cable name, or the name itself
* stream_name_or_object the objects used to generate the

==== Arguments

Captures any `` elements that were broadcast over Action Cable
def capture_turbo_stream_broadcasts(stream_name_or_object, &block)
  block&.call
  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