mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Split out move feature tests and add opponent test
This commit is contained in:
parent
4df7dc0dae
commit
67f0d8f45e
@ -4,7 +4,7 @@
|
||||
fn form -> %>
|
||||
<div class="form-group">
|
||||
<%= label form, :opponent_id %>
|
||||
<%= select form, :opponent_id, @opponents %>
|
||||
<%= select form, :opponent_id, @opponents, prompt: "Choose an opponent" %>
|
||||
<%= error_tag form, :opponent_id %>
|
||||
</div>
|
||||
|
||||
|
||||
2
mix.exs
2
mix.exs
@ -26,7 +26,7 @@ defmodule Chess.Mixfile do
|
||||
end
|
||||
|
||||
# Specifies which paths to compile per environment.
|
||||
defp elixirc_paths(:test), do: ["lib", "test/support"]
|
||||
defp elixirc_paths(:test), do: ["lib", "test/support", "test/helpers"]
|
||||
defp elixirc_paths(_), do: ["lib"]
|
||||
|
||||
# Specifies your project dependencies.
|
||||
|
||||
@ -5,6 +5,9 @@ defmodule Chess.GamesTest do
|
||||
|
||||
import Chess.Factory
|
||||
|
||||
import Chess.AuthenticationHelpers
|
||||
import Chess.FormHelpers
|
||||
|
||||
test "visit homepage", %{session: session} do
|
||||
session
|
||||
|> visit("/")
|
||||
@ -30,6 +33,19 @@ defmodule Chess.GamesTest do
|
||||
|> assert_has(css(".board"))
|
||||
end
|
||||
|
||||
test "cannot create a game without an opponent", %{session: session} do
|
||||
session
|
||||
|> create_user_and_login()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> click(button("Create game"))
|
||||
|
||||
session
|
||||
|> assert_has(
|
||||
css(".help-block", text: "can't be blank")
|
||||
)
|
||||
end
|
||||
|
||||
test "can only see own games", %{session: session} do
|
||||
opponent = insert(:user, %{
|
||||
name: "Urbosa",
|
||||
@ -82,146 +98,4 @@ defmodule Chess.GamesTest do
|
||||
session
|
||||
|> assert_has(css("h2", text: "Game with Zelda"))
|
||||
end
|
||||
|
||||
test "can move a piece", %{session: session} do
|
||||
insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
|
||||
session
|
||||
|> create_user_and_login()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "Zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r1"))
|
||||
|> assert_has(square_selected("f4-r1"))
|
||||
|> assert_has(square_containing("f4-r1", "white.pawn"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r3"))
|
||||
|> refute_has(square_containing("f4-r1", "white.pawn"))
|
||||
|> assert_has(square_containing("f4-r3", "white.pawn"))
|
||||
end
|
||||
|
||||
test "cannot move the opponents pieces", %{session: session} do
|
||||
insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
|
||||
session
|
||||
|> create_user_and_login()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "Zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r6"))
|
||||
|> refute_has(square_selected("f4-r5"))
|
||||
end
|
||||
|
||||
test "cannot move pieces when it's the opponents turn", %{session: session} do
|
||||
insert(:user, %{
|
||||
name: "Link",
|
||||
email: "link@hyrule.com",
|
||||
password: "ilovezelda"
|
||||
})
|
||||
insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
|
||||
session
|
||||
|> login("link@hyrule.com", "ilovezelda")
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "Zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
{:ok, session2} = Wallaby.start_session
|
||||
session2
|
||||
|> login("zelda@hyrule.com", "ganonsucks")
|
||||
|> click(link("Game with Link"))
|
||||
|
||||
session2
|
||||
|> click(css("#f4-r6"))
|
||||
|> refute_has(square_selected("f4-r5"))
|
||||
end
|
||||
|
||||
test "move is reflected on opponents screen", %{session: session} do
|
||||
insert(:user, %{
|
||||
name: "Link",
|
||||
email: "link@hyrule.com",
|
||||
password: "ilovezelda"
|
||||
})
|
||||
insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
|
||||
session
|
||||
|> login("link@hyrule.com", "ilovezelda")
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "Zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
{:ok, session2} = Wallaby.start_session
|
||||
session2
|
||||
|> login("zelda@hyrule.com", "ganonsucks")
|
||||
|> click(link("Game with Link"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r1"))
|
||||
|> click(css("#f4-r3"))
|
||||
|
||||
session2
|
||||
|> assert_has(css(".board.black-to-move"))
|
||||
|> refute_has(square_containing("f4-r1", "white.pawn"))
|
||||
|> assert_has(square_containing("f4-r3", "white.pawn"))
|
||||
end
|
||||
|
||||
defp create_user_and_login(session) do
|
||||
insert(:user, %{
|
||||
name: "Link",
|
||||
email: "link@hyrule.com",
|
||||
password: "ilovezelda"
|
||||
})
|
||||
|
||||
session
|
||||
|> login("link@hyrule.com", "ilovezelda")
|
||||
end
|
||||
|
||||
defp login(session, email, password) do
|
||||
session
|
||||
|> visit("/session/new")
|
||||
|> fill_in(text_field("Email"), with: email)
|
||||
|> fill_in(text_field("Password"), with: password)
|
||||
|> click(button("Log in"))
|
||||
end
|
||||
|
||||
def select(session, name, [option: option]) do
|
||||
session
|
||||
|> find(css("[name='#{name}']"))
|
||||
|> click(option(option))
|
||||
|
||||
session
|
||||
end
|
||||
|
||||
defp square_selected(square) do
|
||||
css("##{square}.selected")
|
||||
end
|
||||
|
||||
defp square_containing(square, piece) do
|
||||
css("##{square}.#{piece}")
|
||||
end
|
||||
end
|
||||
|
||||
125
test/features/moves_test.exs
Normal file
125
test/features/moves_test.exs
Normal file
@ -0,0 +1,125 @@
|
||||
defmodule Chess.MovesTest do
|
||||
use ChessWeb.FeatureCase
|
||||
|
||||
import Wallaby.Query
|
||||
|
||||
import Chess.Factory
|
||||
|
||||
import Chess.AuthenticationHelpers
|
||||
import Chess.FormHelpers
|
||||
|
||||
test "can move a piece", %{session: session} do
|
||||
insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
|
||||
session
|
||||
|> create_user_and_login()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "Zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r1"))
|
||||
|> assert_has(square_selected("f4-r1"))
|
||||
|> assert_has(square_containing("f4-r1", "white.pawn"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r3"))
|
||||
|> refute_has(square_containing("f4-r1", "white.pawn"))
|
||||
|> assert_has(square_containing("f4-r3", "white.pawn"))
|
||||
end
|
||||
|
||||
test "cannot move the opponents pieces", %{session: session} do
|
||||
insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
|
||||
session
|
||||
|> create_user_and_login()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "Zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r6"))
|
||||
|> refute_has(square_selected("f4-r5"))
|
||||
end
|
||||
|
||||
test "cannot move pieces when it's the opponents turn", %{session: session} do
|
||||
insert(:user, %{
|
||||
name: "Link",
|
||||
email: "link@hyrule.com",
|
||||
password: "ilovezelda"
|
||||
})
|
||||
insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
|
||||
session
|
||||
|> login("link@hyrule.com", "ilovezelda")
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "Zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
{:ok, session2} = Wallaby.start_session
|
||||
session2
|
||||
|> login("zelda@hyrule.com", "ganonsucks")
|
||||
|> click(link("Game with Link"))
|
||||
|
||||
session2
|
||||
|> click(css("#f4-r6"))
|
||||
|> refute_has(square_selected("f4-r5"))
|
||||
end
|
||||
|
||||
test "move is reflected on opponents screen", %{session: session} do
|
||||
insert(:user, %{
|
||||
name: "Link",
|
||||
email: "link@hyrule.com",
|
||||
password: "ilovezelda"
|
||||
})
|
||||
insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
|
||||
session
|
||||
|> login("link@hyrule.com", "ilovezelda")
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "Zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
{:ok, session2} = Wallaby.start_session
|
||||
session2
|
||||
|> login("zelda@hyrule.com", "ganonsucks")
|
||||
|> click(link("Game with Link"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r1"))
|
||||
|> click(css("#f4-r3"))
|
||||
|
||||
session2
|
||||
|> assert_has(css(".board.black-to-move"))
|
||||
|> refute_has(square_containing("f4-r1", "white.pawn"))
|
||||
|> assert_has(square_containing("f4-r3", "white.pawn"))
|
||||
end
|
||||
|
||||
defp square_selected(square) do
|
||||
css("##{square}.selected")
|
||||
end
|
||||
|
||||
defp square_containing(square, piece) do
|
||||
css("##{square}.#{piece}")
|
||||
end
|
||||
end
|
||||
26
test/helpers/authentication_helpers.ex
Normal file
26
test/helpers/authentication_helpers.ex
Normal file
@ -0,0 +1,26 @@
|
||||
defmodule Chess.AuthenticationHelpers do
|
||||
use Wallaby.DSL
|
||||
|
||||
import Wallaby.Query
|
||||
|
||||
import Chess.Factory
|
||||
|
||||
def create_user_and_login(session) do
|
||||
insert(:user, %{
|
||||
name: "Link",
|
||||
email: "link@hyrule.com",
|
||||
password: "ilovezelda"
|
||||
})
|
||||
|
||||
session
|
||||
|> login("link@hyrule.com", "ilovezelda")
|
||||
end
|
||||
|
||||
def login(session, email, password) do
|
||||
session
|
||||
|> visit("/session/new")
|
||||
|> fill_in(text_field("Email"), with: email)
|
||||
|> fill_in(text_field("Password"), with: password)
|
||||
|> click(button("Log in"))
|
||||
end
|
||||
end
|
||||
13
test/helpers/form_helpers.ex
Normal file
13
test/helpers/form_helpers.ex
Normal file
@ -0,0 +1,13 @@
|
||||
defmodule Chess.FormHelpers do
|
||||
use Wallaby.DSL
|
||||
|
||||
import Wallaby.Query
|
||||
|
||||
def select(session, name, [option: option]) do
|
||||
session
|
||||
|> find(css("[name='#{name}']"))
|
||||
|> click(option(option))
|
||||
|
||||
session
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user