Skip to content

Commit

Permalink
file_input/4 can set an entry's relative path (#2632)
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch authored May 16, 2023
1 parent 7db0072 commit 69a1a04
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/phoenix_live_view/test/live_view_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ defmodule Phoenix.LiveViewTest do
* `:content` - the binary content of the file
* `:size` - the byte size of the content
* `:type` - the MIME type of the file
* `:relative_path` - for simulating webkitdirectory metadata
## Examples
Expand Down
3 changes: 3 additions & 0 deletions lib/phoenix_live_view/test/structs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ defmodule Phoenix.LiveViewTest.Upload do
Map.get(entry, :content) ||
raise ArgumentError, "the :content of the binary entry file data is required."

relative_path = Map.get(entry, :relative_path)

%{
"name" => name,
"content" => content,
"relative_path" => relative_path,
"ref" => to_string(System.unique_integer([:positive])),
"size" => entry[:size] || byte_size(content),
"type" => entry[:type] || MIME.from_path(name)
Expand Down
41 changes: 41 additions & 0 deletions test/phoenix_live_view/upload/directory_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Phoenix.LiveView.DirectoryTest do
use ExUnit.Case, async: false

@endpoint Phoenix.LiveViewTest.Endpoint

import Phoenix.LiveViewTest

alias Phoenix.LiveViewTest.UploadLive

def mount_lv(setup) when is_function(setup, 1) do
conn = Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), %{})
{:ok, lv, _} = live_isolated(conn, UploadLive, session: %{})
:ok = GenServer.call(lv.pid, {:setup, setup})
{:ok, lv}
end

test "can set relative path from file_input/4 helper" do
{:ok, lv} =
mount_lv(fn socket ->
Phoenix.LiveView.allow_upload(socket, :avatar,
max_entries: 2,
chunk_size: 20,
accept: :any,
external: fn _entry, socket ->
{:ok, %{uploader: "S3"}, socket}
end
)
end)

avatar =
file_input(lv, "form", :avatar, [
%{
name: "foo1.jpeg",
content: String.duplicate("ok", 100),
relative_path: "some/path/to/foo1.jpeg"
}
])

assert render_upload(avatar, "foo1.jpeg", 1) =~ "relative path:some/path/to/foo1.jpeg"
end
end
1 change: 1 addition & 0 deletions test/support/live_views/upload_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Phoenix.LiveViewTest.UploadLive do
<%= for msg <- upload_errors(@uploads.avatar, entry) do %>
error:<%= inspect(msg) %>
<% end %>
relative path:<%= entry.client_relative_path %>
<% end %>
<.live_file_input upload={@uploads.avatar} />
<button type="submit">save</button>
Expand Down

0 comments on commit 69a1a04

Please sign in to comment.