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

Fix: support links with only query parameters #80

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions lib/phoenix_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ defmodule PhoenixTest do
you're visiting.
"""
def visit(conn, path) do
path = maybe_append_path(conn, path)

case get(conn, path) do
%{assigns: %{live_module: _}} = conn ->
PhoenixTest.Live.build(conn)
Expand All @@ -224,6 +226,16 @@ defmodule PhoenixTest do
end
end

defp maybe_append_path(%Plug.Conn{request_path: request_path}, path) do
path
|> URI.parse()
|> Map.replace_lazy(:path, fn
nil -> request_path
uri_path -> uri_path
end)
|> URI.to_string()
end

@doc """
Clicks a link with given text and performs the action.

Expand Down
7 changes: 7 additions & 0 deletions test/phoenix_test/live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ defmodule PhoenixTest.LiveTest do
|> assert_has("h1", text: "Main page")
end

test "handles links with query parameters and no path", %{conn: conn} do
conn
|> visit("/live/index")
|> click_link("English")
|> assert_has("h1", text: "LiveView main page")
end

test "raises error when there are multiple links with same text", %{conn: conn} do
assert_raise ArgumentError, ~r/2 of them matched the text filter/, fn ->
conn
Expand Down
7 changes: 7 additions & 0 deletions test/phoenix_test/static_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ defmodule PhoenixTest.StaticTest do
|> assert_has("h1", text: "LiveView main page")
end

test "handles links with query parameters and no path", %{conn: conn} do
conn
|> visit("/page/index")
|> click_link("English")
|> assert_has("h1", text: "Main page")
end

test "handles form submission via `data-method` & `data-to` attributes", %{conn: conn} do
conn
|> visit("/page/index")
Expand Down
2 changes: 2 additions & 0 deletions test/support/index_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule PhoenixTest.IndexLive do

<.link navigate="/live/page_2?redirect_to=/live/index">Navigate (and redirect back) link</.link>

<.link href="?lang=en">English</.link>

<h2 :if={@details}>LiveView main page details</h2>

<button phx-click="push-navigate">Button with push navigation</button>
Expand Down
2 changes: 2 additions & 0 deletions test/support/page_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ defmodule PhoenixTest.PageView do

<a href="/live/index">To LiveView!</a>

<a href="?lang=en">English</a>

<ul id="multiple-items">
<li>Aragorn</li>
<li>Legolas</li>
Expand Down