stepchud

stepchud

Testing a live_component click event inside a live_view that uses send(self())

I have a problem testing my LiveView that is similar to this post, except that I’m not broadcasting messages via PubSub. In my case, I have a :live_component that renders a button and handles the phx-click event. The handle_event looks like this:

  def handle_event("next-step", _params, socket) do
    IO.puts("clicked next")
    send(self(), {__MODULE__, :go_to_step, 1})
    {:noreply, socket}
  end

the handle_info(:go_to_step, 1) is defined in the parent :live_view of the live component:

  def handle_info({_component, :go_to_step, step_index}, socket) do
    IO.puts("handle go_to_step")
    socket = socket
    |> assign(:current_step, Enum.at(socket.assigns.wizard.steps, step_index))
    {:noreply, socket}
  end

I want to test that clicking the button renders the next step. the IO.puts show my test is properly clicking the button and the handle_info function is reached, so the send() is being received (also the feature works in the browser). However my test assertion fails because the view still shows the original :live_component content and not the one that should be rendered after handle_info finishes. The LiveViewTest:

describe "starts first step" do
  test "clicking Begin button goes to first step", %{conn: conn} do
    {:ok, view, _html} = live(conn, "/wizard")
    assert view
      |> element("button", "Begin")
      |> render_click() =~ "<h1>The Aim</h1>"
  end
end

Any idea how I can get the test process to assert the rendered state at the end of that handle_info()?

Marked As Solved

JonRowe

JonRowe

Given you send a message to yourself during the event handler, the result of “that” render doesn’t change, as you’ve observed, you can always request a render from the view in tests with render(view), which in this case is probably the go.

There is a chance of a race condition in such a rest (click → render then requiring a sleep) but I don’t think in this case it will cause an issue, as the view should process the handle_event message, then the handle_info message, before being free to handle any others (and I believe render(view) requires a message on the view).

Also Liked

stepchud

stepchud

That fixed it, and the test is pretty simple still. Thanks again for the help!

  test "clicking Begin button goes to the first step", %{conn: conn} do
    {:ok, view, _html} = live(conn, "/wizard")
    view
    |> element("button", "Begin")
    |> render_click()
    assert render(view) =~ "<h1>First Step</h1>"
  end

Last Post!

stepchud

stepchud

That fixed it, and the test is pretty simple still. Thanks again for the help!

  test "clicking Begin button goes to the first step", %{conn: conn} do
    {:ok, view, _html} = live(conn, "/wizard")
    view
    |> element("button", "Begin")
    |> render_click()
    assert render(view) =~ "<h1>First Step</h1>"
  end

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
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
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement