nittin

nittin

How do I write tests for dropdowns?

I have a use case scenario where I need to update my user after a click event from option tag like below!

What I’m doing is that I have a click event in each of the options and when there is a click, the user’a role is updated. While it’s easy to code something like this, how do I write live view tests for this?

Most Liked

codeanpeace

codeanpeace

Just a caveat, while this kind of thing can’t be tested directly with Phoenix.LiveViewTest, you can still test client side interactions in Phoenix with integration testing and browser automation libraries such as Wallaby and Hound.

dfalling

dfalling

Hi, super late response here but I was looking for the same thing and this was the first post that came up. In my case it wasn’t even Javascript- I just wanted to test contents inside a <details> tag.

So this kind of thing can’t be tested in Phoenix (see Testing LiveView with JS Commands - #2 by sodapopcan). All of the helpers (eg render_click) are triggering server-side changes in Liveview. If your change requires some client-side handling, it can’t be tested.

trisolaran

trisolaran

Some time ago I discovered that some support for JS commands has been around for a while now. You can test push events in your JS commands. So this kind of tests works:

heex:

<div :if={@clicked}>Clicked!</div>
<button id="clickez-moi" phx-click={JS.push("clicked")} class="btn">Test</button>

live view:

def handle_event(
        "clicked",
        _,
        socket
      ) do
    {:noreply, assign(socket, clicked: true)}
end

test:

test "test js command", %{view: view} do
      element(view, "#clickez-moi")
      |> render_click()

      assert has_element?(view, "div", "Clicked!")
end

You can’t test other commands that purely operate on the DOM on the client side this way (such as add_class) but you can test events sent to the server. This is very nice!

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
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
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New

We're in Beta

About us Mission Statement