Skip to content

Commit

Permalink
Refactor: Add current_path to 'Driver' protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ftes committed Jun 17, 2024
1 parent 34b73b0 commit 4f3ae09
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 27 deletions.
8 changes: 4 additions & 4 deletions lib/phoenix_test/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ defmodule PhoenixTest.Assertions do
end

def assert_path(session, path) do
uri = URI.parse(session.current_path)
uri = URI.parse(PhoenixTest.Driver.current_path(session))

if uri.path == path do
assert true
Expand All @@ -215,7 +215,7 @@ defmodule PhoenixTest.Assertions do
defp assert_query_params(session, params) do
params = Utils.stringify_keys_and_values(params)

uri = URI.parse(session.current_path)
uri = URI.parse(PhoenixTest.Driver.current_path(session))
query_params = uri.query && URI.decode_query(uri.query)

if query_params == params do
Expand All @@ -234,7 +234,7 @@ defmodule PhoenixTest.Assertions do
end

def refute_path(session, path) do
uri = URI.parse(session.current_path)
uri = URI.parse(PhoenixTest.Driver.current_path(session))

if uri.path == path do
msg = """
Expand All @@ -258,7 +258,7 @@ defmodule PhoenixTest.Assertions do
defp refute_query_params(session, params) do
params = Utils.stringify_keys_and_values(params)

uri = URI.parse(session.current_path)
uri = URI.parse(PhoenixTest.Driver.current_path(session))
query_params = uri.query && URI.decode_query(uri.query)

if query_params == params do
Expand Down
1 change: 1 addition & 0 deletions lib/phoenix_test/driver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ defprotocol PhoenixTest.Driver do
def unwrap(session, fun)
def open_browser(session)
def open_browser(session, open_fun)
def current_path(session)
end
3 changes: 3 additions & 0 deletions lib/phoenix_test/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule PhoenixTest.Live do
%__MODULE__{view: view, conn: conn, current_path: current_path}
end

def current_path(session), do: session.current_path

defp append_query_string(path, ""), do: path
defp append_query_string(path, query), do: path <> "?" <> query

Expand Down Expand Up @@ -318,4 +320,5 @@ 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 current_path(session), to: Live
end
3 changes: 3 additions & 0 deletions lib/phoenix_test/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ defmodule PhoenixTest.Static do
%__MODULE__{conn: conn, current_path: current_path}
end

def current_path(session), do: session.current_path

defp append_query_string(path, ""), do: path
defp append_query_string(path, query), do: path <> "?" <> query

Expand Down Expand Up @@ -287,4 +289,5 @@ 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 current_path(session), to: Static
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
16 changes: 8 additions & 8 deletions test/phoenix_test/live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -682,17 +682,17 @@ defmodule PhoenixTest.LiveTest do
end
end

describe "session.current_path" do
describe "current_path" do
test "it is set on visit", %{conn: conn} do
session = visit(conn, "/live/index")

assert session.current_path == "/live/index"
assert PhoenixTest.Driver.current_path(session) == "/live/index"
end

test "it is set on visit with query string", %{conn: conn} do
session = visit(conn, "/live/index?foo=bar")

assert session.current_path == "/live/index?foo=bar"
assert PhoenixTest.Driver.current_path(session) == "/live/index?foo=bar"
end

test "it is updated on href navigation", %{conn: conn} do
Expand All @@ -701,7 +701,7 @@ defmodule PhoenixTest.LiveTest do
|> visit("/live/index")
|> click_link("Navigate to non-liveview")

assert session.current_path == "/page/index?details=true&foo=bar"
assert PhoenixTest.Driver.current_path(session) == "/page/index?details=true&foo=bar"
end

test "it is updated on live navigation", %{conn: conn} do
Expand All @@ -710,7 +710,7 @@ defmodule PhoenixTest.LiveTest do
|> visit("/live/index")
|> click_link("Navigate link")

assert session.current_path == "/live/page_2?details=true&foo=bar"
assert PhoenixTest.Driver.current_path(session) == "/live/page_2?details=true&foo=bar"
end

test "it is updated on live patching", %{conn: conn} do
Expand All @@ -719,7 +719,7 @@ defmodule PhoenixTest.LiveTest do
|> visit("/live/index")
|> click_link("Patch link")

assert session.current_path == "/live/index?details=true&foo=bar"
assert PhoenixTest.Driver.current_path(session) == "/live/index?details=true&foo=bar"
end

test "it is updated on push navigation", %{conn: conn} do
Expand All @@ -728,7 +728,7 @@ defmodule PhoenixTest.LiveTest do
|> visit("/live/index")
|> click_button("Button with push navigation")

assert session.current_path == "/live/page_2?foo=bar"
assert PhoenixTest.Driver.current_path(session) == "/live/page_2?foo=bar"
end

test "it is updated on push patch", %{conn: conn} do
Expand All @@ -737,7 +737,7 @@ defmodule PhoenixTest.LiveTest do
|> visit("/live/index")
|> click_button("Button with push patch")

assert session.current_path == "/live/index?foo=bar"
assert PhoenixTest.Driver.current_path(session) == "/live/index?foo=bar"
end
end
end
10 changes: 5 additions & 5 deletions test/phoenix_test/static_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -695,17 +695,17 @@ defmodule PhoenixTest.StaticTest do
end
end

describe "session.current_path" do
describe "current_path" do
test "it is set on visit", %{conn: conn} do
session = visit(conn, "/page/index")

assert session.current_path == "/page/index"
assert PhoenixTest.Driver.current_path(session) == "/page/index"
end

test "it includes query string if available", %{conn: conn} do
session = visit(conn, "/page/index?foo=bar")

assert session.current_path == "/page/index?foo=bar"
assert PhoenixTest.Driver.current_path(session) == "/page/index?foo=bar"
end

test "it is updated on href navigation", %{conn: conn} do
Expand All @@ -714,7 +714,7 @@ defmodule PhoenixTest.StaticTest do
|> visit("/page/index")
|> click_link("Page 2")

assert session.current_path == "/page/page_2?foo=bar"
assert PhoenixTest.Driver.current_path(session) == "/page/page_2?foo=bar"
end

test "it is updated on redirects", %{conn: conn} do
Expand All @@ -723,7 +723,7 @@ defmodule PhoenixTest.StaticTest do
|> visit("/page/index")
|> click_link("Navigate away and redirect back")

assert session.current_path == "/page/index"
assert PhoenixTest.Driver.current_path(session) == "/page/index"
end
end
end

0 comments on commit 4f3ae09

Please sign in to comment.