I’m trying to test a record creation page which requires authentication. Here is a snippet of live files structure:
- live
- item_live
- form_component.ex
- form_component.html.heex
- index.ex
- index.html.heex
This is a snippet of my test:
test "saves new item", %{conn: conn} = params do
conn = conn |> log_in_user(params.user)
{:ok, view, _html} = live(conn, Routes.item_index_path(conn, :new))
{:ok, _, html} =
view
|> form("#item-form", item: params.item)
|> render_submit()
|> follow_redirect(conn, Routes.item_index_path(conn, :index))
assert html =~ "Item created successfully"
end
When render_submit
is called, it triggers form_component
and it is rendered twice. During the first render, socket
in form_component
has logged in user data. But after the second rendering, socket
doesn’t have the data.
It’s happening only during testing. In the regular web app, the socket
preserving the user data.
My question is why the socket is not preserving user data? How can I pass data from live_view to a live_component?