Class: LiveCable::Testing::TestChannel
- Inherits:
-
Object
- Object
- LiveCable::Testing::TestChannel
- Defined in:
- lib/live_cable/testing/test_channel.rb
Overview
Stand-in for an ActionCable channel. Records streams started via stream_from so tests can trigger their callbacks with receive_stream, and payloads sent by components.
Instance Attribute Summary collapse
- #connection ⇒ LiveCable::Testing::TestCableConnection readonly
-
#streams ⇒ Hash<String, Hash>
readonly
Stream name => { coder:, callback: }.
-
#transmissions ⇒ Array<Hash>
readonly
Payloads sent by components through this channel.
Instance Method Summary collapse
- #broadcast(data) ⇒ Object
-
#broadcast_to(name, payload) ⇒ Object
Simulate an external broadcast arriving on a stream.
-
#initialize(identifiers = {}) ⇒ TestChannel
constructor
A new instance of TestChannel.
- #stop_stream_from(name) ⇒ Object
- #stream_from(name, coder: nil, &block) ⇒ Object
Constructor Details
#initialize(identifiers = {}) ⇒ TestChannel
Returns a new instance of TestChannel.
18 19 20 21 22 |
# File 'lib/live_cable/testing/test_channel.rb', line 18 def initialize(identifiers = {}) @streams = {} @transmissions = [] @connection = TestCableConnection.new(identifiers) end |
Instance Attribute Details
#connection ⇒ LiveCable::Testing::TestCableConnection (readonly)
16 17 18 |
# File 'lib/live_cable/testing/test_channel.rb', line 16 def connection @connection end |
#streams ⇒ Hash<String, Hash> (readonly)
Returns Stream name => { coder:, callback: }.
10 11 12 |
# File 'lib/live_cable/testing/test_channel.rb', line 10 def streams @streams end |
#transmissions ⇒ Array<Hash> (readonly)
Returns Payloads sent by components through this channel.
13 14 15 |
# File 'lib/live_cable/testing/test_channel.rb', line 13 def transmissions @transmissions end |
Instance Method Details
#broadcast(data) ⇒ Object
24 25 26 |
# File 'lib/live_cable/testing/test_channel.rb', line 24 def broadcast(data) @transmissions << data end |
#broadcast_to(name, payload) ⇒ Object
Simulate an external broadcast arriving on a stream. The payload goes through the stream's coder round trip, so a Hash payload arrives with string keys just like a production broadcast.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/live_cable/testing/test_channel.rb', line 42 def broadcast_to(name, payload) stream = @streams.fetch(name) do raise LiveCable::Error, "Component is not streaming from #{name.inspect} " \ "(active streams: #{@streams.keys.inspect})" end callback = stream[:callback] raise LiveCable::Error, "Stream #{name.inspect} has no callback" unless callback coder = stream[:coder] payload = coder.decode(coder.encode(payload)) if coder callback.call(payload) end |
#stop_stream_from(name) ⇒ Object
32 33 34 |
# File 'lib/live_cable/testing/test_channel.rb', line 32 def stop_stream_from(name) @streams.delete(name) end |
#stream_from(name, coder: nil, &block) ⇒ Object
28 29 30 |
# File 'lib/live_cable/testing/test_channel.rb', line 28 def stream_from(name, coder: nil, &block) @streams[name] = { coder:, callback: block } end |