Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Move assertions into 'Driver' protocol #88

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions lib/phoenix_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ defmodule PhoenixTest do

import Phoenix.ConnTest

alias PhoenixTest.Assertions
alias PhoenixTest.Driver

@endpoint Application.compile_env(:phoenix_test, :endpoint)
Expand Down Expand Up @@ -691,7 +690,7 @@ defmodule PhoenixTest do
assert_has(session, "#user")
```
"""
defdelegate assert_has(session, selector), to: Assertions
defdelegate assert_has(session, selector), to: Driver

@doc """
Assert helper to ensure an element with given CSS selector and options.
Expand Down Expand Up @@ -734,7 +733,7 @@ defmodule PhoenixTest do
assert_has(session, ".posts", at: 2, text: "Hello")
```
"""
defdelegate assert_has(session, selector, opts), to: Assertions
defdelegate assert_has(session, selector, opts), to: Driver

@doc """
Opposite of `assert_has/2` helper. Verifies that element with
Expand All @@ -754,7 +753,7 @@ defmodule PhoenixTest do
refute_has(session, "#user")
```
"""
defdelegate refute_has(session, selector), to: Assertions
defdelegate refute_has(session, selector), to: Driver

@doc """
Opposite of `assert_has/3` helper. Verifies that element with
Expand Down Expand Up @@ -795,7 +794,7 @@ defmodule PhoenixTest do
refute_has(session, ".posts", at: 2, text: "Hello")
```
"""
defdelegate refute_has(session, selector, opts), to: Assertions
defdelegate refute_has(session, selector, opts), to: Driver

@doc """
Assert helper to verify current request path. Takes an optional `query_params`
Expand All @@ -822,12 +821,12 @@ defmodule PhoenixTest do
|> assert_path("/users", query_params: %{name: "frodo"})
```
"""
defdelegate assert_path(session, path), to: Assertions
defdelegate assert_path(session, path), to: Driver

@doc """
Same as `assert_path/2` but takes an optional `query_params` map.
"""
defdelegate assert_path(session, path, opts), to: Assertions
defdelegate assert_path(session, path, opts), to: Driver

@doc """
Verifies current request path is NOT the one provided. Takes an optional
Expand All @@ -854,11 +853,11 @@ defmodule PhoenixTest do
|> refute_path("/users", query_params: %{name: "frodo"})
```
"""
defdelegate refute_path(session, path), to: Assertions
defdelegate refute_path(session, path), to: Driver

@doc """
Same as `refute_path/2` but takes an optional `query_params` for more specific
refutation.
"""
defdelegate refute_path(session, path, opts), to: Assertions
defdelegate refute_path(session, path, opts), to: Driver
end
9 changes: 9 additions & 0 deletions lib/phoenix_test/driver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ defprotocol PhoenixTest.Driver do
def unwrap(session, fun)
def open_browser(session)
def open_browser(session, open_fun)

def assert_has(session, selector)
def assert_has(session, selector, opts)
def refute_has(session, selector)
def refute_has(session, selector, opts)
def assert_path(session, path)
def assert_path(session, path, opts)
def refute_path(session, path)
def refute_path(session, path, opts)
end
10 changes: 10 additions & 0 deletions lib/phoenix_test/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ defmodule PhoenixTest.Live do
end

defimpl PhoenixTest.Driver, for: PhoenixTest.Live do
alias PhoenixTest.Assertions
alias PhoenixTest.Live

defdelegate render_page_title(session), to: Live
Expand All @@ -318,4 +319,13 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Live do
defdelegate open_browser(session), to: Live
defdelegate open_browser(session, open_fun), to: Live
defdelegate unwrap(session, fun), to: Live

defdelegate assert_has(session, selector), to: Assertions
defdelegate assert_has(session, selector, opts), to: Assertions
defdelegate refute_has(session, selector), to: Assertions
defdelegate refute_has(session, selector, opts), to: Assertions
defdelegate assert_path(session, path), to: Assertions
defdelegate assert_path(session, path, opts), to: Assertions
defdelegate refute_path(session, path), to: Assertions
defdelegate refute_path(session, path, opts), to: Assertions
end
10 changes: 10 additions & 0 deletions lib/phoenix_test/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ defmodule PhoenixTest.Static do
end

defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
alias PhoenixTest.Assertions
alias PhoenixTest.Static

defdelegate render_page_title(session), to: Static
Expand All @@ -287,4 +288,13 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
defdelegate open_browser(session), to: Static
defdelegate open_browser(session, open_fun), to: Static
defdelegate unwrap(session, fun), to: Static

defdelegate assert_has(session, selector), to: Assertions
defdelegate assert_has(session, selector, opts), to: Assertions
defdelegate refute_has(session, selector), to: Assertions
defdelegate refute_has(session, selector, opts), to: Assertions
defdelegate assert_path(session, path), to: Assertions
defdelegate assert_path(session, path, opts), to: Assertions
defdelegate refute_path(session, path), to: Assertions
defdelegate refute_path(session, path, opts), to: Assertions
end
21 changes: 11 additions & 10 deletions test/phoenix_test/assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule PhoenixTest.AssertionsTest do
import PhoenixTest.TestHelpers

alias ExUnit.AssertionError
alias PhoenixTest.Live

setup do
%{conn: Phoenix.ConnTest.build_conn()}
Expand Down Expand Up @@ -568,19 +569,19 @@ defmodule PhoenixTest.AssertionsTest do

describe "assert_path" do
test "asserts the session's current path" do
session = %{current_path: "/page/index"}
session = %Live{current_path: "/page/index"}

assert_path(session, "/page/index")
end

test "asserts query params are the same" do
session = %{current_path: "/page/index?hello=world"}
session = %Live{current_path: "/page/index?hello=world"}

assert_path(session, "/page/index", query_params: %{"hello" => "world"})
end

test "order of query params does not matter" do
session = %{current_path: "/page/index?hello=world&foo=bar"}
session = %Live{current_path: "/page/index?hello=world&foo=bar"}

assert_path(session, "/page/index", query_params: %{"foo" => "bar", "hello" => "world"})
end
Expand All @@ -592,7 +593,7 @@ defmodule PhoenixTest.AssertionsTest do
""")

assert_raise AssertionError, msg, fn ->
session = %{current_path: "/page/index"}
session = %Live{current_path: "/page/index"}

assert_path(session, "/page/not-index")
end
Expand All @@ -605,7 +606,7 @@ defmodule PhoenixTest.AssertionsTest do
""")

assert_raise AssertionError, msg, fn ->
session = %{current_path: "/page/index"}
session = %Live{current_path: "/page/index"}

assert_path(session, "/page/index", query_params: %{foo: "bar", details: true})
end
Expand All @@ -618,7 +619,7 @@ defmodule PhoenixTest.AssertionsTest do
""")

assert_raise AssertionError, msg, fn ->
session = %{current_path: "/page/index?hello=world&hi=bye"}
session = %Live{current_path: "/page/index?hello=world&hi=bye"}

assert_path(session, "/page/index", query_params: %{"goodbye" => "world", "hi" => "bye"})
end
Expand All @@ -627,13 +628,13 @@ defmodule PhoenixTest.AssertionsTest do

describe "refute_path" do
test "refute the given path is the current path" do
session = %{current_path: "/page/index"}
session = %Live{current_path: "/page/index"}

refute_path(session, "/page/page_2")
end

test "refutes query params are the same" do
session = %{current_path: "/page/index?hello=world"}
session = %Live{current_path: "/page/index?hello=world"}

refute_path(session, "/page/index", query_params: %{"hello" => "not-world"})
end
Expand All @@ -645,7 +646,7 @@ defmodule PhoenixTest.AssertionsTest do
""")

assert_raise AssertionError, msg, fn ->
session = %{current_path: "/page/index"}
session = %Live{current_path: "/page/index"}

refute_path(session, "/page/index")
end
Expand All @@ -658,7 +659,7 @@ defmodule PhoenixTest.AssertionsTest do
""")

assert_raise AssertionError, msg, fn ->
session = %{current_path: "/page/index?hello=world&hi=bye"}
session = %Live{current_path: "/page/index?hello=world&hi=bye"}

refute_path(session, "/page/index", query_params: %{"hello" => "world", "hi" => "bye"})
end
Expand Down
Loading