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.

Examples:

RSpec

RSpec.describe Live::Counter do
  include LiveCable::Testing

  it 'increments by the step size' do
    counter = live_mount('counter', step: 2)

    counter.perform(:increment)

    expect(counter.count).to eq(2)
    expect(counter.rendered).to have_css('[data-testid="counter-value"]', text: '2')
  end
end

Defined Under Namespace

Classes: RenderState, TestCableConnection, TestChannel, TestComponent

Instance Method Summary collapse

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.

Parameters:

  • component (String, Class, LiveCable::Component)

    Component name (e.g. 'counter' or 'chat/room'), component class, or instance

  • id (String) (defaults to: 'test')

    The component id (defaults to 'test')

  • connection (LiveCable::Connection, nil) (defaults to: nil)

    Mount onto an existing test connection (from another mounted component) to share state between components

  • identifiers (Hash) (defaults to: {})

    ActionCable connection identifiers made available to the component (e.g. current_user: user)

  • raise_errors (Boolean) (defaults to: true)

    Raise errors from actions and rendering instead of broadcasting an _error like production does (default true)

  • defaults (Hash)

    Default values for reactive variables

Returns:



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