dwyl / email

✉️ email dashboard to track deliverability and engagement stats in our App 📈
https://dwylmail.herokuapp.com/
GNU General Public License v2.0
41 stars 2 forks source link

How to test handle_info/2 in Phoenix LiveView #26

Closed nelsonic closed 4 years ago

nelsonic commented 4 years ago

The email dashboard #23 works: https://phemail.herokuapp.com image

But sadly we don't have a test for the handle_event/3 function: https://codecov.io/gh/dwyl/email/src/e19ac570bedbcb78720bae60e9215d6a8b449473/lib/app_web/live/dashboard.ex#L13 image

Until we figure out how to test the handle_event/3 function we cannot reach 100% ... codecov.io

I've tried hacking on it for a couple of pomodoros in: /test/app_web/live/dashboard_test.exs and read the docs: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#content 👀 Stumped on how to test this ... 🤦‍♂

If you know how to test this please help by commenting below!

nelsonic commented 4 years ago

This looks promising: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#module-testing-events 👀

nelsonic commented 4 years ago

Got the error:

** (UndefinedFunctionError) function Floki.parse_document/1 is undefined (module Floki is not available)

According to: https://hexdocs.pm/phoenix_live_view/installation.html floki is required to test LiveView ... floki-test-dep

So adding it to mix.exs:

{:floki, ">= 0.0.0", only: :test}
nelsonic commented 4 years ago

Continue: https://stackoverflow.com/questions/ask

How to test handle_event/3 in Phoenix LiveView

Greetings Phoenix LiveView Wizards!

I have a basic LiveView app that renders/updates a table: https://github.com/dwyl/email
The code is very simple: lib/app_web/live/dashboard.ex

defmodule AppWeb.Dashboard do
  use Phoenix.LiveView
  @topic "live"

  def mount(_session, _params, socket) do
    AppWeb.Endpoint.subscribe(@topic) # subscribe to the channel
    sent = App.Ctx.list_sent_with_status()
    {:ok, assign(socket, %{val: 0, sent: sent}),
      layout: {AppWeb.LayoutView, "live.html"}}
  end

  def handle_event("sent", _value, socket) do
    new_state = update(socket, :sent, App.Ctx.list_sent_with_status())
    AppWeb.Endpoint.broadcast_from(self(), @topic, "sent", new_state.assigns)
    {:noreply, new_state}
  end

  def handle_info(_msg, socket) do
    {:noreply, assign(socket, sent: App.Ctx.list_sent_with_status())}
  end

  def render(assigns) do
    AppWeb.PageView.render("dashboard.html", assigns)
  end
end

The code works as expected, see: https://phemail.herokuapp.com
but we have no idea how to test the handle_event/3 function. 🤷‍♂
We have tried invoking it but we just get errors ...

nelsonic commented 4 years ago

Opened question on SO: https://stackoverflow.com/questions/60848600/how-to-test-handle-event-3 Also on ElixirForum: https://elixirforum.com/t/how-to-test-handle-event-3-in-phoenix-liveview/30070

Let's see what the internet says. 🙏 ⏳

nelsonic commented 4 years ago

Got a couple of replies on the questions. 🎉

Progress: counter-not-covered

nelsonic commented 4 years ago

Success! https://codecov.io/gh/dwyl/phoenix-liveview-counter-tutorial/src/master/lib/live_view_counter_web/live/counter.ex image

nelsonic commented 4 years ago

Now I "just" need to figure out how to apply this to our dashboard.ex ... 💭

nelsonic commented 4 years ago

When I attempt to run the following test using the render_click/2 function:

test "handle_event/3", %{conn: conn} do
  {:ok, view, html} = live(conn, "/")
  # IO.inspect(view, label: "view")
  assert view.module == AppWeb.Dashboard
  assert html =~ "Email"
  result = render_click(view, :refresh)
  IO.inspect(result, label: "result")
end

It gives the following error:

15:59:28.896 [error] GenServer #PID<0.487.0> terminating
** (BadFunctionError) expected a function, got: []
    (phoenix_live_view 0.10.0) lib/phoenix_live_view.ex:1503: Phoenix.LiveView.update/3
    (app 1.0.0) lib/app_web/live/dashboard.ex:13: AppWeb.Dashboard.handle_event/3
    (phoenix_live_view 0.10.0) lib/phoenix_live_view/channel.ex:92: Phoenix.LiveView.Channel.handle_info/2
    (stdlib 3.11.2) gen_server.erl:637: :gen_server.try_dispatch/4
    (stdlib 3.11.2) gen_server.erl:711: :gen_server.handle_msg/6
    (stdlib 3.11.2) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Message{event: "event", join_ref: 0, payload: %{"event" => "refresh", "type" => "click", "value" => %{}}, ref: "1", topic: "phx-Ff-XUTOFPPgWfAIj"}
nelsonic commented 4 years ago

What if I just DELETE the handle_event/3 function...? 💭

nelsonic commented 4 years ago

Deleted the handle_event/3 function in https://github.com/dwyl/email/commit/2a5332328a8a25d1f8cdc74b7509865b93af8fd3 and the dashboard still works https://dwylmail.herokuapp.com and we have 100% coverage! https://codecov.io/github/dwyl/email?branch=master