|
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 |
3 | 4 |
|
4 | | - import Phoenix.LiveViewTest |
5 | | - alias Phoenix.LiveViewTest.Support.Endpoint |
| 5 | + import Phoenix.LiveViewTest |
| 6 | + alias Phoenix.LiveViewTest.Support.Endpoint |
6 | 7 |
|
7 | | - @endpoint Endpoint |
| 8 | + @endpoint Endpoint |
8 | 9 |
|
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 |
12 | 13 |
|
13 | | - defmodule AssertWillReceiveLive do |
14 | | - use Phoenix.LiveView |
| 14 | + defmodule AssertWillReceiveLive do |
| 15 | + use Phoenix.LiveView |
15 | 16 |
|
16 | | - def mount(_params, _session, socket) do |
17 | | - {:ok, socket} |
18 | | - end |
| 17 | + def mount(_params, _session, socket) do |
| 18 | + {:ok, socket} |
| 19 | + end |
19 | 20 |
|
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 |
23 | 24 |
|
24 | | - def render(assigns) do |
25 | | - ~H"" |
| 25 | + def render(assigns) do |
| 26 | + ~H"" |
| 27 | + end |
26 | 28 | end |
27 | | - end |
28 | 29 |
|
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) |
32 | 33 |
|
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) |
37 | 38 |
|
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 |
41 | 42 |
|
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) |
44 | 45 |
|
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) |
48 | 49 |
|
49 | | - assert num == 1 |
50 | | - end |
| 50 | + assert num == 1 |
| 51 | + end |
51 | 52 |
|
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) |
54 | 55 |
|
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 | + ) |
64 | 65 |
|
65 | | - assert num == 2 |
66 | | - end |
| 66 | + assert num == 2 |
| 67 | + end |
67 | 68 |
|
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 |
87 | 89 | end |
88 | 90 | end |
89 | 91 | end |
|
0 commit comments