LiveIsolatedComponent (live_isolated_component v0.3.0)

Functions for testing LiveView stateful components in isolation easily.

Link to this section Summary

Functions

Asserts that a given handle event has been received.

Asserts the return value of a handle_event

Asserts that a given handle_info event has been received.

Updates the assigns of the component.

Updates the key in assigns of the component.

Renders the given component in isolation and live so you can tested like you would test any LiveView.

Link to this section Functions

Link to this macro

assert_handle_event(view, event \\ nil, params \\ nil, timeout \\ 100)

(macro)

Asserts that a given handle event has been received.

Depending on the number of parameters, different parts are checked:

  • With no parameters, just that a handle_event message has been received.
  • With one parameter, just the event name is checked.
  • With two parameters, both event name and the parameters are checked.
  • The optional last argument is the timeout, defaults to 100 milliseconds

If you just want to check the parameters without checking the event name, pass nil as the event name.

Link to this macro

assert_handle_event_return(view, return_value)

(macro)

Asserts the return value of a handle_event

Link to this macro

assert_handle_info(view, event \\ nil, timeout \\ 100)

(macro)

Asserts that a given handle_info event has been received.

If only the view is passed, only that a handle_info is received is checked. With an optional event name, we check that too. The third argument is an optional timeout, defaults to 100 milliseconds.

Link to this function

live_assign(view, keyword_or_map)

Updates the assigns of the component.

{:ok, view, _html} = live_isolated_component(SomeComponent, assigns: %{description: "blue"})

live_assign(view, %{description: "red"})
Link to this function

live_assign(view, key, value)

Updates the key in assigns of the component.

{:ok, view, _html} = live_isolated_component(SomeComponent, assigns: %{description: "blue"})

live_assign(view, :description, "red")
Link to this macro

live_isolated_component(component, opts \\ %{})

(macro)

Renders the given component in isolation and live so you can tested like you would test any LiveView.

It accepts the following options:

  • :assigns accepts a map of assigns for the component.
  • :handle_event accepts a handler for the handle_event callback in the LiveView.
  • :handle_info accepts a handler for the handle_info callback in the LiveView.
  • :slots accepts different slot descriptors.