LiveViewTest: How to test a form with "normal" fields and a file upload

Hey,

i´ve a form with a text field and file upload:

Now i want to test the entire form with its logic, but form and file_input functions of the LiveViewTest Module only return events.

Test extract:

      pre_model_update_view
      |> form("#model-upload-form",
        model: model_params
      )
    # |> file_input("#model-upload-form", :uploaded_files, [
    #   %{
    #     last_modified: 1_594_171_879_000,
    #     name: test_if_file_name,
    #     content: test_ifc_file_content,
    #     size: test_ifc_file_stats.size,
    #     type: "application/ifc"
    #   }
    # ])
    # |> render_submit()
    # |> render_upload(test_if_file_name)

How can i test the entire form?

Thx

1 Like

have you tried uploading first and then submitting the form?

file_input = file_input(pre_model_update_view, "#model-upload-form", :uploaded_files, [ ... ])

render_upload(file_input, test_if_file_name)

pre_model_update_view
|> form("#model-upload-form", model: model_params)
|> render_submit()
3 Likes

I guess its working, but i´ve work on my current logic.
Thx.

After fixing my logic, i can confirm your solution @trisolaran is correct.

In case it is helpful for others stumbling across this thread, there is a reasonable example of how to test liveview uploads here: phoenix_starter/profile_component_test.exs at d319e22697874a06963c95c269a03a61cdaf5938 · newaperio/phoenix_starter · GitHub

2 Likes