# LiveView isolated Component Tests?

**URL:** <https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893>\
**Category:** Questions\
**Tags:** phoenix, testing, liveview\
**Created:** [March 28, 2023, 5:14pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893 "2023-03-28T17:14:53Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![coladarci](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/coladarci/32/8394_2.png) [@coladarci](https://forum.elixirforum.com/u/coladarci)\
**Post date:** [March 28, 2023, 5:14pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/1 "2023-03-28T17:14:53Z")

</div>

Hello, all!

I’m curious if anyone has ideas/experience around the best ways to go about testing live view components. Obviously you can write a test for the `live route` that loads up the full page, start clicking things, etc, but for a complicated setup with many stateful components, it’s extremely difficult to reach good coverage from testing that high up.

In `ember` (and I’m sure other front-end frameworks) there are testing practices where you can render a component on its own (the equivalent of an ephemeral live route?) and interact with it asserting along the way that the right things happen. This helps assert strong contracts; i.e what happens if you embed a component with a specific field set to `nil`; does the component handle it correctly.

Is there are a way to do this? Does this sound like a good path for the framework to go down? A (terrible?) hack would be a `/testing` scope in the router that is used to just embed a single component on the page and we’d manually add one for each component, but that might give an idea of roughly what we’d want to automate in a testing routine..

---

<div class="post-metadata">

**Author:** ![mcrumm](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mcrumm/32/23032_2.png) [@mcrumm](https://forum.elixirforum.com/u/mcrumm)\
**Post date:** [March 28, 2023, 5:23pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/2 "2023-03-28T17:23:55Z")

</div>

You can use [`live_isolated/3`](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#live_isolated/3) to test a LiveView in isolation, but it’s recommended to test through the router (i.e. using the [`live/2`](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#live/2) test helper) as that is required to test live navigation.

Note there is also [`render_component/3`](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#render_component/3) for testing function and stateful components in isolation.

---

<div class="post-metadata">

**Author:** ![coladarci](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/coladarci/32/8394_2.png) [@coladarci](https://forum.elixirforum.com/u/coladarci)\
**Post date:** [March 28, 2023, 5:27pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/3 "2023-03-28T17:27:55Z")

</div>

giggity! Thanks @mcrumm…

---

<div class="post-metadata">

**Author:** ![APB9785](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/apb9785/32/24101_2.png) [@APB9785](https://forum.elixirforum.com/u/APB9785)\
**Post date:** [March 28, 2023, 7:03pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/4 "2023-03-28T19:03:25Z")

</div>

Maybe [this library](https://github.com/Serabe/live_isolated_component) could be helpful also?

---

<div class="post-metadata">

**Author:** ![coladarci](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/coladarci/32/8394_2.png) [@coladarci](https://forum.elixirforum.com/u/coladarci)\
**Post date:** [March 28, 2023, 10:37pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/5 "2023-03-28T22:37:17Z")

</div>

This is very helpful - so @mcrumm it sounds like `render_component` is what I want except that it only renders it; it doesn’t let you interact with it and test the result. @APB9785 's link to `live_isolated` appears to do what I’m after - is there no way to do that w/out a library at this point, to know your knowledge?

---

<div class="post-metadata">

**Author:** ![mcrumm](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mcrumm/32/23032_2.png) [@mcrumm](https://forum.elixirforum.com/u/mcrumm)\
**Post date:** [March 30, 2023, 8:04pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/6 "2023-03-30T20:04:57Z")

</div>

With a little bit of setup you can definitely drive a LiveComponent under test without a library.

I will share an example and an explanation of how I got there, but it should be noted that I don’t use LiveComponent very often so this may not meet your every need 🙂

### Explanation

A common pattern used in the LiveView tests themselves is to create a LiveView that you can drive under test, for example:

```elixir
defmodule MyLiveTest do

  use MyApp.ConnCase, async: true
 import Phoenix.LiveViewTest

  defmodule DemoLive do
    use Phoenix.LiveView

    def render(assigns) do
    ~H"""
    <p>the answer: <%= @the_answer %></p>
    """

    def mount(_, _, socket) do
      {:ok, assign(socket, :the_answer, nil)}
    end

    def handle_call({:run, func}, _, socket) when is_function(func, 1) do
      func.(socket)
    end

    ## Test Helpers

    def run(lv, func) do
      GenServer.call(lv.pid, {:run, func})
    end
  end

  test "test assign/3", %{conn: conn} do
    {:ok, lv, _html} = live_isolated(conn, DemoLive)

    DemoLive.run(lv, fn socket ->
      {:reply, :ok, assign(socket, :the_answer, 42)}
    end)

    assert render(lv) =~ "the answer: 42"
  end
end

```

We can use the `run/2` function to execute code _on the LiveView process_, so it’s capable of doing a lot of heavy lifting.

> Note: Adding a run function/callback as in the example above is an **anti-pattern for testing real LiveViews**. Avoid reaching into the LiveView process state in your tests.

If we pair this with LiveView lifecycle events, we can intercept messages to the LiveView under test as well:

```elixir
test "intercepts a message", %{conn: conn} do
  {:ok, lv, _html} = live_isolated(conn, DemoLive)

  test_pid = self()

  DemoLive.run(lv, fn socket ->
    socket =
      Phoenix.LiveView.attach_hook(socket, :interceptor, :handle_info, fn
        {:intercept_me, term}, socket ->
          send(test_pid, {:intercepted, term})

          # :halt prevents handle_info/2 from being invoked on the LiveView for this message.
          {:halt, socket}

        _other, socket ->
          # :cont allows handle_info/2 to be invoked on the LiveView for this message.
          {:cont, socket}
      end)

    {:reply, :ok, socket}
  end)

  ref = make_ref()

  send(lv.pid, {:intercept_me, ref})

  assert_receive {:intercepted, ^ref}
end

```

So if we can construct a custom LiveView that renders a dynamic LiveComponent, then we can drive the isolated LiveComponent under test 🙂

### Example

This gist has a complete example: [Testing Phoenix.LiveComponent in Isolation · GitHub](https://gist.github.com/mcrumm/8e6b0a98196dd74a841d850c70805f50)

The `LiveComponentTest` support module defined in the gist exposes three functions:

- `live_component_isolated/3` - Starts a LiveView process dedicated to driving the LiveComponent under test. You pass in the conn, the LiveComponent module, and any attributes.

- `live_component_intercept/2` - Attaches a lifecycle hook to the `:handle_info` stage of the driver process. Use this to intercept messages from the LiveComponent. You can send the results back to the test pid to assert on them. Returns an opaque ref that can be used to remove the intercept if needed.

- `live_component_remove_intercept/1` - Removes a previously added intercept.

> <https://gist.github.com/mcrumm/8e6b0a98196dd74a841d850c70805f50#file-live_component_test-ex>

Overall, I think this approach provides a lot of flexibility. With some additional work under-the-hood I think instead of intercept it could expose macros like `assert_lv_receive`, but I will leave that for someone else to explore for now 🙂

I hope that helps!

---

<div class="post-metadata">

**Author:** ![coladarci](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/coladarci/32/8394_2.png) [@coladarci](https://forum.elixirforum.com/u/coladarci)\
**Post date:** [April 19, 2023, 1:54pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/7 "2023-04-19T13:54:09Z")

</div>

Just want to thank you for this extremely thorough response and example - this is exceptional. In my experience outside of live view, _this is the way_ to think about testing a complicated live view app; your components are like functions and you want to write them to be easily testable in isolation to cover all the edge cases they have to handle. It’s wonderful to know it’s possible, and _with your macros,_ straight-forward 🙂

Once again, appreciate all you do!

---

<div class="post-metadata">

**Author:** ![coladarci](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/coladarci/32/8394_2.png) [@coladarci](https://forum.elixirforum.com/u/coladarci)\
**Post date:** [April 19, 2023, 2:04pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/8 "2023-04-19T14:04:11Z")

</div>

UPDATE: I got it but leaving here for posterity. Just some confusing meta programing warnings that go away when I completed the test that uses `live_component_intercept`.. Thanks, again!

As an FYI, the code you provided seems to work great locally, though the macro is throwing a warning on this line: [Testing Phoenix.LiveComponent in Isolation · GitHub](https://gist.github.com/mcrumm/8e6b0a98196dd74a841d850c70805f50#file-example_component_test-exs-L9)

```elixir
warning: this check/guard will always yield the same result

```

I’m having a hard time figuring out why but will keep digging!

---

<div class="post-metadata">

**Author:** ![mcrumm](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mcrumm/32/23032_2.png) [@mcrumm](https://forum.elixirforum.com/u/mcrumm)\
**Post date:** [April 19, 2023, 6:08pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/9 "2023-04-19T18:08:47Z")

</div>

Ah, it was the default `attrs` on the macro. I updated the gist to use an empty list instead of `nil` and that seems to have resolved it. 🙂 Thanks for pointing that out!

---

<div class="post-metadata">

**Author:** ![coladarci](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/coladarci/32/8394_2.png) [@coladarci](https://forum.elixirforum.com/u/coladarci)\
**Post date:** [October 7, 2023, 5:57pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/10 "2023-10-07T17:57:37Z")

</div>

Just want to followup here and say this is working quite nicely for us. The only thing that is a bit difficult is debugging with `open_browser/1` since it’s not rendering the root template; we lose the styling. Is there an easy way in your mind to change

```elixir
    def render(assigns) do
      ~H"""
      <.live_component :if={@lc_module} module={@lc_module} {@lc_attrs} />
      """
    end

```

in the driver to render this inside the root template? 😁

---

<div class="post-metadata">

**Author:** ![mwhitworth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mwhitworth/32/33362_2.png) [@mwhitworth](https://forum.elixirforum.com/u/mwhitworth)\
**Post date:** [November 17, 2023, 8:58am UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/11 "2023-11-17T08:58:28Z")

</div>

Thanks for this - I can confirm [live\_isolated\_component](https://github.com/Serabe/live_isolated_component) does the trick and works great for this use case!

---

<div class="post-metadata">

**Author:** ![geofflane](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/geofflane/32/4489_2.png) [@geofflane](https://forum.elixirforum.com/u/geofflane)\
**Post date:** [January 26, 2024, 1:40pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/12 "2024-01-26T13:40:28Z")

</div>

The live\_isolated\_component is exactly what I wanted!

The caveat is that I’m just starting to look at LiveView, but this seems like a big missing piece unless I’m doing something wildly wrong (which is possible I admit): I am using `assign_async` to do some data loading in a component and there’s not an obvious way to test that in isolation of a view. `render_async` only works with views and so won’t work with the seemingly only way to test a component which is `render_component`. That’s a pretty big limitation and I would expect something like `live_isolated_component` to be included in the library.

Thanks for pointing that out.

---

<div class="post-metadata">

**Author:** ![nicocirio](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nicocirio/32/26386_2.png) [@nicocirio](https://forum.elixirforum.com/u/nicocirio)\
**Post date:** [March 13, 2024, 1:31pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/13 "2024-03-13T13:31:00Z")

</div>

Hi! Thanks for this great solution! In my case, I also needed to intercept events that were targeted to the parent LiveView (in cases where the LiveComponent does not define `phx-target={@myself}` for a specific event). Adding the following function to `live_component_tests.ex` helped me test that behavior:

```elixir
@doc """
  Intercepts events on the LiveComponentTest LiveView.
  Use this function to intercept events sent by the LiveComponent to the LiveView (when phx-target={@myself} is NOT defined).
  ## Examples
      {:ok, lcd, _html} = LiveComponentTest.live_component_isolated(conn, MyLiveComponent)
      test_pid = self()
      live_component_event_intercept(lv, fn
          "some_event_name" = event, %{"some" => "example_params"} = params, socket ->
            send(test_pid, {:handle_event_intercepted, event, params})
            {:halt, socket}
          _other, _params, socket ->
            {:cont, socket}
      end)
      assert_received {:handle_event_intercepted, "some_event_name", %{"some" => "example_params"}}
  """
  def live_component_event_intercept(lv, func) when is_function(func) do
    Driver.run(lv, fn socket ->
      name = :"lv_event_intercept_#{System.unique_integer([:positive, :monotonic])}"
      ref = {:event_intercept, lv, name, :handle_event}
      {:reply, ref, Phoenix.LiveView.attach_hook(socket, name, :handle_event, func)}
    end)
  end

```

Hope this function is helpful for others! 🙂

---

<div class="post-metadata">

**Author:** ![kamaroly](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kamaroly/32/29371_2.png) [@kamaroly](https://forum.elixirforum.com/u/kamaroly)\
**Post date:** [November 7, 2024, 8:33pm UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/14 "2024-11-07T20:33:07Z")

</div>

I wish the `LiveComponentTest` was included out of the box in Phoenix. It looks like what I have been looking for.

---

<div class="post-metadata">

**Author:** ![kamaroly](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kamaroly/32/29371_2.png) [@kamaroly](https://forum.elixirforum.com/u/kamaroly)\
**Post date:** [November 10, 2024, 5:30am UTC](https://forum.elixirforum.com/t/liveview-isolated-component-tests/54893/15 "2024-11-10T05:30:20Z")

</div>

For those who are still looking for a solution,  
I found the following package helpful: [Overview — live\_isolated\_component v0.10.0](https://hexdocs.pm/live_isolated_component/readme.html)
