Skip to content

Commit

Permalink
Phoenix.LiveViewTest.refute_push_event/4 (#3490)
Browse files Browse the repository at this point in the history
* Phoenix.LiveViewTest.refute_push_event/4

Adds the `refute_receive` equivalent to `assert_push_event`.

Errors look like:

```elixir
  1) test it can refute events
     test/example_test.exs
     Unexpectedly received event "scores"

     Payload:
     %{points: 100, user: "Dave"}

     (which matched %{points: _, user: "josé"})

     code: refute_push_event(live, "scores", %{points: _, user: "josé"})
     stacktrace:
       test/example_test.exs:273: (test)
```
---------

Co-authored-by: Chris McCord <chris@chrismccord.com>
  • Loading branch information
davydog187 and chrismccord authored Nov 7, 2024
1 parent cb85f02 commit 0cec128
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/phoenix_live_view/test/live_view_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,41 @@ defmodule Phoenix.LiveViewTest do
end
end

@doc """
Refutes an event will be pushed within timeout.
The default `timeout` is [ExUnit](https://hexdocs.pm/ex_unit/ExUnit.html#configure/1)'s
`refute_receive_timeout` (100 ms).
## Examples
refute_push_event view, "scores", %{points: _, user: "josé"}
"""
defmacro refute_push_event(
view,
event,
payload,
timeout \\ Application.fetch_env!(:ex_unit, :refute_receive_timeout)
) do
quote do
%{proxy: {ref, _topic, _}} = unquote(view)

receive do
{^ref, {:push_event, unquote(event), unquote(payload) = data}} ->
flunk("""
Unexpectedly received event "#{unquote(event)}"
Payload:
#{inspect(data, pretty: true)}
""")
after
unquote(timeout) ->
false
end
end
end

@doc """
Asserts a hook reply was returned from a `handle_event` callback.
Expand Down
9 changes: 9 additions & 0 deletions test/phoenix_live_view/integrations/event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,18 @@ defmodule Phoenix.LiveView.EventTest do
)

assert_push_event(view, "my-event", %{two: 2})

assert render(view) =~ "count: 0"
end

test "sends no events if none are pushed", %{conn: conn} do
{:ok, view, _html} = live(conn, "/events")

GenServer.call(view.pid, {:run, fn socket -> {:reply, :ok, socket} end})

refute_push_event(view, "my-event", _)
end

test "sends updates in root and child mounts", %{conn: conn} do
{:ok, view, _html} = live(conn, "/events-in-mount")

Expand Down
4 changes: 4 additions & 0 deletions test/support/live_views/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ defmodule Phoenix.LiveViewTest.Support.EventsLive do
{:reply, reply, socket}
end

def handle_event("dont-reply", _, socket) do
{:noreply, socket}
end

def handle_call({:run, func}, _, socket), do: func.(socket)

def handle_info({:run, func}, socket), do: func.(socket)
Expand Down

0 comments on commit 0cec128

Please sign in to comment.