I have a nested LiveView in which different kinds of children views can come and go dynamically. I want to unit-test these children views individually without setting up the more complicated parent.
My thought was to setup a dummy LiveView that renders the component to be tested. Then I want to use live_isolated/3
to trigger the test.
Something like that.
defmodule MyDummyLiveView do
use MyAppWeb, :live_view
# imports aliases and stuff
def mount(_params, _session, socket) do
%{my_params: my_params} = get_connect_params(socket)
{:ok, assign(socket, %{params: my_params})}
end
def render(%{params: my_params}) do
live_component(MySubComponent, params: my_params)
end
end
Then the unit test would look like
test "some test", %{conn: conn} do
my_params = %{foo: 1, bar: 2}
{:ok, view, _html} =
conn
|> put_connect_params(%{my_params: my_params})
|> live_isolated(MyDummyLiveView)
html = view
|> element("#some_element")
|> render_click()
# assertion on html
end
This doesn’t work because the socket in the mount/3
function seems to be disconnected. get_connectparam/1
returns nil
.
How would I make sure that mount/3
gets a connected socket?