Skip to content

Commit

Permalink
Fix: support links with only query parameters
Browse files Browse the repository at this point in the history
This commit resolves an issue where links containing only query
parameters without a specified path would incorrectly direct to the root
path instead of the current path, unlike standard browser behavior.

Example of a link without a path:

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

Previously, `click_link/2` would direct to root path. In this commit the
link correctly retains the current page while applying the query
parameters.
  • Loading branch information
jounimakela committed Jul 10, 2024
1 parent 41c70e0 commit 5854969
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/phoenix_test/link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ defmodule PhoenixTest.Link do
|> Html.attribute("data-method")
|> Utils.present?()
end

def maybe_append_path(link, path) do
link.href
|> URI.parse()
|> Map.replace_lazy(:path, fn
nil -> path
current_path -> current_path
end)
|> URI.to_string()
end
end
4 changes: 3 additions & 1 deletion lib/phoenix_test/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ defmodule PhoenixTest.Static do
|> dispatch(@endpoint, form.method, form.action, form.data)
|> maybe_redirect(session)
else
PhoenixTest.visit(session.conn, link.href)
path = Link.maybe_append_path(link, session.conn.request_path)

PhoenixTest.visit(session.conn, path)
end
end

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/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

0 comments on commit 5854969

Please sign in to comment.