sodapopcan

sodapopcan

Testing LiveView with JS Commands

I’m having trouble figuring out how to test LiveViews that contain JS commands. Is this even possible? My mental model is a bit lacking here in terms of how browserless LiveView tests work. There doesn’t seem to be any mention of testing JS commands in the docs though would be happy to add them if I can figure this out.

My scenario:

def some_button(assigns) do
  ~H"""
  <a href="#"
    data-test-id="button"
    phx-click={Phoenix.LiveView.JS.add_class("some-class", to: "body")}>click me</a>
  """
end

…the test:

test "click it", %{conn: conn} do
  {:ok, view, _html} = live(conn, "/")

assert view
       |> element("[data-test-id=button]")
       |> render_click() =~ "resulting html"  # Right side of the assertion isn't important since it never makes it here
end

I get the following error: (ArgumentError) no push command found within JS commands

Any insight would be much appreciated!

Marked As Solved

trisolaran

trisolaran

My understanding is that, using the functions in Phoenix.LiveViewTest, you can only test the server-side part of the LiveView, i.e. the LiveView process. As far as I know, no JS client is started during the test, and this precludes testing of JS commands.

Also Liked

sodapopcan

sodapopcan

If you want to test actual JS interactions with an actual JS engine running, you have to use Wallaby.

You can, however, still test the functionality of whatever is in modals with LiveViewTest, just not whether they open or close. For example, if you have a modal with a form in it that is hidden by JS, it’s still in the DOM so you can still have a LiveViewTest fill it out and submit it (LiveViewTest doesn’t care if something is truly “visible” to the user or not). Then you can have a few Wallaby tests (which are much heavier to run) to ensure JS stuff is working.

sodapopcan

sodapopcan

I just want to give this a bump I still can’t figure it out and it’s driving me a little nuts. Can anyone at least answer if writing LiveViewTests for LiveViews with JS commands in them is even possible? I can always use Wallaby but I’m trying to hold off on that until I get into heavier system tests.

TIA!

sodapopcan

sodapopcan

Awesome, thanks for your response! It’s what I figured I just couldn’t find anywhere that explicitly stated they wouldn’t work. I’ll see if I can update the docs in some way that makes sense.

Where Next?

Popular in Questions Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
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
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement