-
Notifications
You must be signed in to change notification settings - Fork 96
/
room_channel_test.exs
40 lines (32 loc) · 1.3 KB
/
room_channel_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
defmodule ChatWeb.RoomChannelTest do
use ChatWeb.ChannelCase
setup do
{:ok, _, socket} =
ChatWeb.UserSocket
|> socket("user_id", %{some: :assign})
|> subscribe_and_join(ChatWeb.RoomChannel, "room:lobby")
%{socket: socket}
end
test "ping replies with status ok", %{socket: socket} do
ref = push(socket, "ping", %{"hello" => "there"})
assert_reply ref, :ok, %{"hello" => "there"}
end
test "shout broadcasts to room:lobby a message with username", %{socket: socket} do
push(socket, "shout", %{"name" => "test_username", "message" => "hey all"})
assert_broadcast "shout", %{"name" => "test_username", "message" => "hey all"}
end
test "broadcasts are pushed to the client", %{socket: socket} do
broadcast_from!(socket, "broadcast", %{"some" => "data"})
assert_push "broadcast", %{"some" => "data"}
end
test ":after_join sends all existing messages", %{socket: socket} do
# insert a new message to send in the :after_join
payload = %{name: "Alex", message: "test"}
Chat.Message.changeset(%Chat.Message{}, payload) |> Chat.Repo.insert()
{:ok, _, socket2} =
ChatWeb.UserSocket
|> socket("user_id", %{some: :assign})
|> subscribe_and_join(ChatWeb.RoomChannel, "room:lobby")
assert socket2.join_ref != socket.join_ref
end
end