Module: LiveCable::Testing
- Defined in:
- lib/live_cable/testing.rb,
lib/live_cable/testing/render_state.rb,
lib/live_cable/testing/test_channel.rb,
lib/live_cable/testing/test_component.rb,
lib/live_cable/testing/test_cable_connection.rb
Overview
Test helpers for unit testing LiveCable components without a browser or a real ActionCable connection.
Include the module in your specs and use live_mount to mount a
component. Actions and reactive updates are dispatched through the real
message pipeline, so action whitelisting, parameter parsing, writability
checks, change tracking, and re-rendering are all exercised exactly as
they are in production.
Defined Under Namespace
Classes: RenderState, TestCableConnection, TestChannel, TestComponent
Instance Method Summary collapse
-
#live_mount(component, id: 'test', connection: nil, identifiers: {}, raise_errors: true, **defaults) ⇒ LiveCable::Testing::TestComponent
Mount a component for testing.
Instance Method Details
#live_mount(component, id: 'test', connection: nil, identifiers: {}, raise_errors: true, **defaults) ⇒ LiveCable::Testing::TestComponent
Mount a component for testing.
Mirrors what LiveChannel#subscribed does in production: the component
is registered on a connection, defaults are applied, lifecycle connect
callbacks run, and the initial render is broadcast.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/live_cable/testing.rb', line 45 def live_mount(component, id: 'test', connection: nil, identifiers: {}, raise_errors: true, **defaults) connection ||= build_test_connection(raise_errors:) instance = case component when LiveCable::Component then component when Class then component.new(id) else LiveCable.instance_from_string(component.to_s, id) end test_component = TestComponent.new(instance, connection, TestChannel.new(identifiers)) connection.add_component(instance) instance.defaults = defaults instance.apply_defaults instance.connect(test_component.channel) instance.broadcast_render test_component end |