Skip to content

Commit 92d9c3b

Browse files
committed
guard tests
1 parent eb03e9d commit 92d9c3b

1 file changed

Lines changed: 70 additions & 68 deletions

File tree

test/phoenix_live_view/integrations/assert_will_receive_test.exs

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,91 @@
1-
defmodule Phoenix.LiveView.AssertWillReceiveTest do
2-
use ExUnit.Case, async: true
1+
if Code.ensure_loaded?(:trace) and function_exported?(:trace, :session_create, 3) do
2+
defmodule Phoenix.LiveView.AssertWillReceiveTest do
3+
use ExUnit.Case, async: true
34

4-
import Phoenix.LiveViewTest
5-
alias Phoenix.LiveViewTest.Support.Endpoint
5+
import Phoenix.LiveViewTest
6+
alias Phoenix.LiveViewTest.Support.Endpoint
67

7-
@endpoint Endpoint
8+
@endpoint Endpoint
89

9-
setup do
10-
{:ok, conn: Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), %{})}
11-
end
10+
setup do
11+
{:ok, conn: Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), %{})}
12+
end
1213

13-
defmodule AssertWillReceiveLive do
14-
use Phoenix.LiveView
14+
defmodule AssertWillReceiveLive do
15+
use Phoenix.LiveView
1516

16-
def mount(_params, _session, socket) do
17-
{:ok, socket}
18-
end
17+
def mount(_params, _session, socket) do
18+
{:ok, socket}
19+
end
1920

20-
def handle_info({:test_message, _num}, socket) do
21-
{:noreply, socket}
22-
end
21+
def handle_info({:test_message, _num}, socket) do
22+
{:noreply, socket}
23+
end
2324

24-
def render(assigns) do
25-
~H""
25+
def render(assigns) do
26+
~H""
27+
end
2628
end
27-
end
2829

29-
describe "assert_will_receive" do
30-
test "asserts the LiveView process will receive a message", %{conn: conn} do
31-
{:ok, view, _} = live_isolated(conn, AssertWillReceiveLive)
30+
describe "assert_will_receive" do
31+
test "asserts the LiveView process will receive a message", %{conn: conn} do
32+
{:ok, view, _} = live_isolated(conn, AssertWillReceiveLive)
3233

33-
Task.start(fn ->
34-
Process.sleep(25)
35-
send(view.pid, {:test_message, 1})
36-
end)
34+
Task.start(fn ->
35+
Process.sleep(25)
36+
send(view.pid, {:test_message, 1})
37+
end)
3738

38-
assert_will_receive(view, {:test_message, num})
39-
assert num == 1
40-
end
39+
assert_will_receive(view, {:test_message, num})
40+
assert num == 1
41+
end
4142

42-
test "runs setup function after tracing is set up", %{conn: conn} do
43-
{:ok, view, _} = live_isolated(conn, AssertWillReceiveLive)
43+
test "runs setup function after tracing is set up", %{conn: conn} do
44+
{:ok, view, _} = live_isolated(conn, AssertWillReceiveLive)
4445

45-
assert_will_receive(view, {:test_message, num}, fn ->
46-
send(view.pid, {:test_message, 1})
47-
end)
46+
assert_will_receive(view, {:test_message, num}, fn ->
47+
send(view.pid, {:test_message, 1})
48+
end)
4849

49-
assert num == 1
50-
end
50+
assert num == 1
51+
end
5152

52-
test "supports guards", %{conn: conn} do
53-
{:ok, view, _} = live_isolated(conn, AssertWillReceiveLive)
53+
test "supports guards", %{conn: conn} do
54+
{:ok, view, _} = live_isolated(conn, AssertWillReceiveLive)
5455

55-
assert_will_receive(
56-
view,
57-
{:test_message, num} when num > 1,
58-
fn ->
59-
send(view.pid, {:test_message, 1})
60-
send(view.pid, {:test_message, 2})
61-
end,
62-
1000
63-
)
56+
assert_will_receive(
57+
view,
58+
{:test_message, num} when num > 1,
59+
fn ->
60+
send(view.pid, {:test_message, 1})
61+
send(view.pid, {:test_message, 2})
62+
end,
63+
1000
64+
)
6465

65-
assert num == 2
66-
end
66+
assert num == 2
67+
end
6768

68-
test "stops tracing after the assertion", %{conn: conn} do
69-
{:ok, view, _} = live_isolated(conn, AssertWillReceiveLive)
70-
71-
assert_will_receive(
72-
view,
73-
{:test_message, :during},
74-
fn ->
75-
send(view.pid, {:test_message, :during})
76-
end,
77-
1000
78-
)
79-
80-
send(view.pid, {:test_message, :after})
81-
82-
receive do
83-
{ref, {:test_message, :after}} when is_reference(ref) ->
84-
flunk("expected assert_will_receive to stop tracing")
85-
after
86-
50 -> :ok
69+
test "stops tracing after the assertion", %{conn: conn} do
70+
{:ok, view, _} = live_isolated(conn, AssertWillReceiveLive)
71+
72+
assert_will_receive(
73+
view,
74+
{:test_message, :during},
75+
fn ->
76+
send(view.pid, {:test_message, :during})
77+
end,
78+
1000
79+
)
80+
81+
send(view.pid, {:test_message, :after})
82+
83+
receive do
84+
{ref, {:test_message, :after}} when is_reference(ref) ->
85+
flunk("expected assert_will_receive to stop tracing")
86+
after
87+
50 -> :ok
88+
end
8789
end
8890
end
8991
end

0 commit comments

Comments
 (0)