1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Add first few channel tests

This commit is contained in:
Daniel Barber 2018-08-15 15:41:17 -04:00
parent b500ac5068
commit eb28da621e
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 54 additions and 1 deletions

View File

@ -0,0 +1,43 @@
defmodule ChessWeb.GameChannelTest do
use ChessWeb.ChannelCase
alias ChessWeb.UserSocket
import Chess.Factory
# import Chess.AuthenticationHelpers
test "assigns game_id to the socket after join" do
user = insert(:user)
game = insert(:game, %{
user_id: user.id,
opponent_id: insert(:opponent).id
})
token = Phoenix.Token.sign(@endpoint, "game socket", user.id)
{:ok, socket} = connect(UserSocket, %{"token" => token})
{:ok, _, socket} = subscribe_and_join(socket, "game:#{game.id}", %{})
assert socket.assigns.game_id == Integer.to_string(game.id)
end
test "returns the game state after join" do
user = insert(:user)
opponent = insert(:opponent, %{name: "Daruk"})
game = insert(:game, %{
user_id: user.id,
opponent_id: opponent.id
})
token = Phoenix.Token.sign(@endpoint, "game socket", user.id)
{:ok, socket} = connect(UserSocket, %{"token" => token})
{:ok, _, _} = subscribe_and_join(socket, "game:#{game.id}", %{})
assert_push "game:update", %{
player: "white",
opponent: "Daruk",
turn: "white",
}
end
end

View File

@ -11,7 +11,17 @@ defmodule Chess.Factory do
%User{
name: "Zelda",
email: "zelda@hyrule.com",
password: "ilovelink"
password: "ganonsucks"
}
|> User.changeset(params)
|> Repo.insert!
end
def insert(:opponent, params) do
%User{
name: "Link",
email: "link@hyrule.com",
password: "ilovezelda"
}
|> User.changeset(params)
|> Repo.insert!