instance method
capture_turbo_stream_broadcasts
Ruby on Rails 1.5.0
Since v1.5.0 Last seen in v2.0.23Available in: v1.5.0 v2.0.23
Signature
capture_turbo_stream_broadcasts(stream_name_or_object, &block)
Captures any ‘<turbo-stream>` elements that were broadcast over Action Cable
Arguments
-
stream_name_or_objectthe objects used to generate the channel Action Cable name, or the name itself -
&blockoptional block to capture broadcasts during execution
Returns any ‘<turbo-stream>` elements that have been broadcast as an Array of Nokogiri::XML::Element instances
message = Message.find(1) message.broadcast_append_to "messages" message.broadcast_prepend_to "messages" turbo_streams = capture_turbo_stream_broadcasts "messages" assert_equal "append", turbo_streams.first["action"] assert_equal "prepend", turbo_streams.second["action"]
You can pass a block to limit the scope of the broadcasts being captured:
message = Message.find(1) turbo_streams = capture_turbo_stream_broadcasts "messages" do message.broadcast_append_to "messages" end assert_equal "append", turbo_streams.first["action"]
In addition to a String, the helper also accepts an Object or Array to determine the name of the channel the elements are broadcast to:
message = Message.find(1) replace, remove = capture_turbo_stream_broadcasts message do message.broadcast_replace message.broadcast_remove end assert_equal "replace", replace["action"] assert_equal "replace", remove["action"]
Parameters
-
stream_name_or_objectreq -
blockblock
Source
# File lib/turbo/broadcastable/test_helper.rb, line 157
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
Defined in lib/turbo/broadcastable/test_helper.rb line 157
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in Turbo::Broadcastable::TestHelper