travisf

travisf

LiveView file upload test

I’m trying to test a file upload:


    file =
      file_input(view, "#profile_pic_upload", :profile_pic, [
        %{
          last_modified: '1_624_366_053_000',
          name: "profile_test.png",
          content: File.read!("../support/profile_test.png"),
          size: 269_280,
          type: "image/png"
        }
      ])

    assert render_upload(file, "foo.png") =~ "100%"

But I keep getting this error:

** (ArgumentError) expected selector "#profile_pic_upload input[type=\"file\"][name=\"profile_pic\"]" to return a single element, but got none within:

But the HTML returned seems to contain that specific element:

<input accept=".jpg,.jpeg,.png" data-phx-active-refs="" data-phx-done-refs="" data-phx-preflighted-refs="" data-phx-update="ignore" data-phx-upload-ref="phx-Fors3j71WxhLhgGi" id="profile_pic_upload" name="profile_pic" phx-hook="Phoenix.LiveFileUpload" type="file"/>

So why is it unable to find the selector? Is there something I need to do to be more explicit?

For some context, I’m not actually trying to test the upload percentage, I’m just working through the docs. I am trying to populate the field with a file, submit the form to test some logic that my LiveView performs when a profile is updated with a profile picture. If there is another way to submit a form with a file attached, or fake it, I’m all ears!

Marked As Solved

cblavier

cblavier

I think that instead of doing

file_input(view, "#profile_pic_upload", ...)

you should do

file_input(view, "#my_form_containing_profile_pic_upload", ...)

Let us know if it works!

Also Liked

chrismccord

chrismccord

Creator of Phoenix

Note that you do not need to pass size if you are passing the content, as we’ll calculate the correct byte_size for you. You may be passing a mismatched size which we aren’t handling

travisf

travisf

Thanks @cblavier that definitely gets the correct field input.
Now I’m getting an error:

** (exit) exited in: GenServer.call(#PID<0.802.0>, {:chunk, "profile_test.png", 100, #PID<0.800.0>, #Phoenix.LiveViewTest.Element<selector: "#account_form input[type=\"file\"][name=\"profile_pic\"]", text_filter: nil, ...>}, 5000)
 ** (EXIT) shutdown: :closed

But I assume that’s related to something else.

Last Post!

travisf

travisf

Thank you so much that was the exact issue. I wish I could mark two answers as solutions!

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement