karang

karang

Liveview testing render changes before submitting form

Hello I am trying to test my liveview and I want to test the editing of a resource through liveview. So I have something like this:

{:ok, view, _html} = live(conn, ~p"/my_resource/#{id}/edit")

params = %{some_key:  "some value"}

{:ok, view, html} =
    view
    |> form("#my-form")
    |> render_change(params)
    |> render_submit()
    |> follow_redirect(conn, ~p"/my_redirect_route")

My problem is that the return type of render_change is not what render submit is expecting and it errors out on the render_submit() line. This is the error I get:

** (FunctionClauseError) no function clause matching in Phoenix.LiveViewTest.render_event/4
     
The following arguments were given to Phoenix.LiveViewTest.render_event/4:

# 1
"<div>my html string</div>"

# 2
:submit

# 3
%{}
     
# 4 
%{}

I have tried using the element/3 function and splitting this out into 2 steps but that failed also. Is there a way I can render some changes and then submit the form? I am sure there is I am just missing something… Thanks!

Marked As Solved

RudManusachi

RudManusachi

LiveViews are GenServer’s under the hood

source

render_change is modifying the state of the live_view process and returns rendered result as a string to assert on if needed.

Once we got view via

{:ok, view, _html} = live(conn, ~p"/my_resource/#{id}/edit")

we should be able to

# modify the view (this will also return the resulting html so we can assert on its content if needed)
html = 
  view
  |> form("#my-form")
  |> render_change(%{some_key:  "some value"})

# now the state of the `view` is modified, so we can submit the form and try to follow redirect
view
|> form("#my-form")
|> render_submit()
|> follow_redirect(conn, ~p"/my_redirect_route")

Also Liked

karang

karang

Makes total sense! That worked. Thanks!

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement