Filling inputs_for inputs in LiveView tests

I have a LiveView form that allows editing of associations via inputs_for, but when I try to write tests for that behavior, I haven’t been able to get the test code to fill the appropriate field. I get an error that reads could not find non-disabled input, select or textarea with name "list[tasks][]" within and then all of the HTML. The name of the field itself is list[tasks][0][description].

    test "test name", %{list: list} do
      task = task_fixture(%{list_id: list.id})

      {:ok, show_live, _html} = live(conn, ~p"/lists/#{list}")

      assert show_live
             |> form("#list-form", list: %{tasks: [%{description: "some description"}]})
             |> render_submit()
<.inputs_for :let={task_form} field={@form[:tasks]}>
    <.input field={task_form[:description]} type="textarea" />
</.inputs_for>

I’m assuming this is an issue with the arguments I’m passing to form, but I haven’t been able to figure out how to get it right.

Figured a solution out! A working call is:

form("#list-form", list: %{tasks: %{"0": %{description: "some description"}}})
1 Like

Can you explain why this solution worked? I assume this renders task_form[0][:description]?

Nvm. I see that this is for nested inputs!

(ArgumentError) could not find non-disabled input, select or textarea with name "account[users][]" within:

         <input type="hidden" name="account[users][0][_persistent_id]" value="0"/>
         <input type="hidden" name="account[users][0][id]" value="usr_00b9b8d493914e56a8d"/>
         <input type="text" name="account[users][0][full_name]" id="account_users_0_full_name" value="Miss Cleta McKenzie" class="pc-text-input"/>

This is an effect of how forms submit data. Say you got the following:

account[users][][id]=1&
account[users][][full_name]=John&
account[users][][id]=2&
account[users][][full_name]=Jane

There would be no foolprove way to know which ID belongs to which fullname. Therefore inputs_for generates indexed fields.