byoungdale

byoungdale

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?

Showing Posts 1 to 3

byoungdale

byoungdale OP

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)
mcrumm

mcrumm

Phoenix Core Team

Try including the _target: when you invoke render_change:

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

Here’s an example from the LiveView tests:

https://github.com/phoenixframework/phoenix_live_view/blob/05bcd95fd40afc10a6b39f1c7ce9986495b21d5d/test/phoenix_live_view/integrations/elements_test.exs#L396-L399

We might be able to pick up the phx-change attribute from the input element automatically (avoiding the need to set _target manually) if someone wanted to PR that :slight_smile:

byoungdale

byoungdale OP

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!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews