byoungdale

byoungdale

Using render_change for testing input element changes - is this the correct way?

I am trying to write a test for a phx-change event I have on a input element inside a form. The phx-change happens only on the input element, not a change on the form.

 <%= url_input f, :url, phx_debounce: "2000", phx_change: "validate_url" %>

Everything works functionally, but I wrote this test for the element:

  test "successful URLs", %{conn: conn} do
    {:ok, view, _html} = live(conn, "/submit")

    assert view
           |> element("#submit-item-form_url")
           |> render_change(:validate_url, %{url: "https://elixir-lang.org/learning.html"}) =~
             "value=\"Learning resources - The Elixir programming language\""

But, when I run the test, I get this error:

  1) test successful URLs (CommunWeb.Live.SubmitItemLiveTest)
     test/commun_web/live/submit_item_live_test.exs:15
     ** (FunctionClauseError) no function clause matching in Phoenix.LiveViewTest.render_event/4

     The following arguments were given to Phoenix.LiveViewTest.render_event/4:
     
         # 1
         #Phoenix.LiveViewTest.Element<selector: "#submit-item-form_url", text_filter: nil, ...>
     
         # 2
         :change
     
         # 3
         :validate_url
     
         # 4
         %{url: "https://elixir-lang.org/learning.html"}
     
     Attempted function clauses (showing 1 out of 1):
     
         defp render_event(%Phoenix.LiveViewTest.View{} = view, type, event, value) when is_map(value) or is_list(value)
     
     code: |> render_change(:validate_url, %{url: "https://elixir-lang.org/learning.html"}) =~
     stacktrace:
       (phoenix_live_view 0.17.10) lib/phoenix_live_view/test/live_view_test.ex:887: Phoenix.LiveViewTest.render_event/4
       test/commun_web/live/submit_item_live_test.exs:20: (test)

It is complaining that the private function render_event is expecting a %Phoenix.LiveViewTest.View{} type, but the render_change function expects an Phoenix.LiveViewTest.Element type, which is what i am passing.

I think I am missing something or is this not the correct way to test a render_change event on an input element?

First Post!

byoungdale

byoungdale

I think the issue is that when a Element is passed to render_change, it does not allow for a customer event to be passed:

  def render_change(%Element{} = element, value), do: render_event(element, :change, value)

for reference see source code here

So, when I change my test to this:

    assert view
           |> element("#submit-item-form_url")
           |> render_change(%{url: "https://elixir-lang.org/learning.html"}) =~
             "value=\"Learning resources - The Elixir programming language\""

My input’s phx-change event validate_url doesn’t get triggered. Just a generic :change event which means my view doesn’t get the url input added to it.

I think adding another match like this would work:

def render_change(%Element{} = element, event, value), do: render_event(element, event, value)

Last Post!

byoungdale

byoungdale

Thank you. The solution was actually not adding the target. The issue was passing in a map instead of a key: map which is what my liveview event handler was expecting.

Here is what ended up working:

assert view
       |> element("#submit-item-form_url")
       |> render_change(item: %{url: "https://elixir-lang.org/learning.html"}) =~
             "value=\"Learning resources - The Elixir programming language\""

So, I had two problems. First, I was manually passing in the render_change event name when I did not need to, which caused the render_event pattern matching to fail in live_view_test.ex, and then without manually passing in the event name, I was not passing the value correctly for my handle_event function.

I only caught that because of the example you posted. Thank you for your help!

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

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
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
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
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 42716 114
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement