Skip to content

Commit

Permalink
🔧 Fix assert_path live navigation with query params
Browse files Browse the repository at this point in the history
What changed?
=============

We fix a bug where `assert_path` would fail with live navigation because
query params weren't being "carried through" in the current path
  • Loading branch information
germsvel committed May 2, 2024
1 parent e173f5b commit 6c58f27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/phoenix_test/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ defmodule PhoenixTest.Live do

def build(conn) do
{:ok, view, _html} = live(conn)
%__MODULE__{view: view, conn: conn, current_path: conn.request_path}
current_path = conn.request_path <> "?" <> conn.query_string
%__MODULE__{view: view, conn: conn, current_path: current_path}
end

def render_page_title(%{view: view}) do
Expand Down
21 changes: 21 additions & 0 deletions test/phoenix_test/assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,34 @@ defmodule PhoenixTest.AssertionsTest do
|> assert_path("/live/index")
end

test "asserts correct path with regular navigation (from Live)", %{conn: conn} do
conn
|> visit("/live/index")
|> click_link("Navigate to non-liveview")
|> assert_path("/page/index")
end

test "asserts correct query params with regular navigation (from Live)", %{conn: conn} do
conn
|> visit("/live/index")
|> click_link("Navigate to non-liveview")
|> assert_path("/page/index", query_params: %{foo: "bar", details: true})
end

test "asserts correct path with Live navigation", %{conn: conn} do
conn
|> visit("/live/index")
|> click_link("Navigate link")
|> assert_path("/live/page_2")
end

test "asserts correct query params with Live navigation", %{conn: conn} do
conn
|> visit("/live/index")
|> click_link("Navigate link")
|> assert_path("/live/page_2", query_params: %{foo: "bar", details: true})
end

test "asserts correct path with Live patching", %{conn: conn} do
conn
|> visit("/live/index")
Expand Down
4 changes: 2 additions & 2 deletions test/support/index_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ defmodule PhoenixTest.IndexLive do
~H"""
<h1 id="title" class="title" data-role="title">LiveView main page</h1>
<.link navigate="/live/page_2">Navigate link</.link>
<.link navigate="/live/page_2?details=true&foo=bar">Navigate link</.link>
<.link patch="/live/index?details=true&foo=bar">Patch link</.link>
<.link href="/page/index">Navigate to non-liveview</.link>
<.link href="/page/index?details=true&foo=bar">Navigate to non-liveview</.link>
<.link class="multiple_links" href="/live/page_3">Multiple links</.link>
<.link class="multiple_links" href="/live/page_4">Multiple links</.link>
Expand Down

0 comments on commit 6c58f27

Please sign in to comment.