Module: Turbo::Broadcastable::TestHelper
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/turbo/broadcastable/test_helper.rb
Instance Method Summary collapse
-
#assert_no_turbo_stream_broadcasts(stream_name_or_object, &block) ⇒ Object
Asserts that no ‘<turbo-stream>` elements were broadcast over Action Cable.
-
#assert_turbo_stream_broadcasts(stream_name_or_object, count: nil, &block) ⇒ Object
Asserts that ‘<turbo-stream>` elements were broadcast over Action Cable.
-
#capture_turbo_stream_broadcasts(stream_name_or_object, &block) ⇒ Object
Captures any ‘<turbo-stream>` elements that were broadcast over Action Cable.
Instance Method Details
#assert_no_turbo_stream_broadcasts(stream_name_or_object, &block) ⇒ Object
Asserts that no ‘<turbo-stream>` elements were broadcast over Action Cable
Arguments
-
stream_name_or_object
the objects used to generate the channel Action Cable name, or the name itself -
&block
optional block executed before the assertion
Asserts that no ‘<turbo-stream>` elements were broadcast:
= Message.find(1)
.broadcast_replace_to "messages"
assert_no_turbo_stream_broadcasts "messages" # fails with MiniTest::Assertion error
You can pass a block to run before the assertion:
= Message.find(1)
assert_no_turbo_stream_broadcasts "messages" do
# do something other than broadcast to "messages"
end
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.find(1)
assert_no_turbo_stream_broadcasts do
# do something other than broadcast to "message_1"
end
104 105 106 107 108 109 110 111 112 |
# File 'lib/turbo/broadcastable/test_helper.rb', line 104 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 |
#assert_turbo_stream_broadcasts(stream_name_or_object, count: nil, &block) ⇒ Object
Asserts that ‘<turbo-stream>` elements were broadcast over Action Cable
Arguments
-
stream_name_or_object
the objects used to generate the channel Action Cable name, or the name itself -
&block
optional block executed before the assertion
Options
-
count:
the number of ‘<turbo-stream>` elements that are
expected to be broadcast
Asserts ‘<turbo-stream>` elements were broadcast:
= Message.find(1)
.broadcast_replace_to "messages"
assert_turbo_stream_broadcasts "messages"
Asserts that two ‘<turbo-stream>` elements were broadcast:
= Message.find(1)
.broadcast_replace_to "messages"
.broadcast_remove_to "messages"
assert_turbo_stream_broadcasts "messages", count: 2
You can pass a block to run before the assertion:
= Message.find(1)
assert_turbo_stream_broadcasts "messages" do
.broadcast_append_to "messages"
end
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.find(1)
assert_turbo_stream_broadcasts do
.broadcast_replace
end
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/turbo/broadcastable/test_helper.rb', line 58 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 |
#capture_turbo_stream_broadcasts(stream_name_or_object, &block) ⇒ Object
Captures any ‘<turbo-stream>` elements that were broadcast over Action Cable
Arguments
-
stream_name_or_object
the objects used to generate the channel Action Cable name, or the name itself -
&block
optional block to capture broadcasts during execution
Returns any ‘<turbo-stream>` elements that have been broadcast as an Array of Nokogiri::XML::Element
instances
= Message.find(1)
.broadcast_append_to "messages"
.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.find(1)
turbo_streams = capture_turbo_stream_broadcasts "messages" do
.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.find(1)
replace, remove = capture_turbo_stream_broadcasts do
.broadcast_replace
.broadcast_remove
end
assert_equal "replace", replace["action"]
assert_equal "replace", remove["action"]
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# 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 |