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:
parent
b500ac5068
commit
eb28da621e
43
test/chess_web/channels/game_channel_test.exs
Normal file
43
test/chess_web/channels/game_channel_test.exs
Normal 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
|
||||||
@ -11,7 +11,17 @@ defmodule Chess.Factory do
|
|||||||
%User{
|
%User{
|
||||||
name: "Zelda",
|
name: "Zelda",
|
||||||
email: "zelda@hyrule.com",
|
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)
|
|> User.changeset(params)
|
||||||
|> Repo.insert!
|
|> Repo.insert!
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user