From daa4dcac35fb467cd4aa91e463986abfd9ef268c Mon Sep 17 00:00:00 2001 From: German Velasco Date: Sat, 3 Feb 2024 12:53:36 -0500 Subject: [PATCH] Raise when multiple links found Static pages What changed? ============= We update our `Query.find/3` result to differentiate between finding one or many elements. Since we now do that, we can improve our assertions. But we also bring the static and LiveView behavior up to par in a place where we had a difference. LiveView raises when we find multiple elements that have the same selector and text. So, it requires users to find a unique element. Our Static implementation would simply follow the first link (if many were found) instead of raising. We now update the code to raise if we find many -- matching the LiveView behavior. --- lib/phoenix_test/assertions.ex | 7 +++++ lib/phoenix_test/query.ex | 38 +++++++++++++++++---------- test/phoenix_test/assertions_test.exs | 4 +-- test/phoenix_test/static_test.exs | 11 ++++---- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/phoenix_test/assertions.ex b/lib/phoenix_test/assertions.ex index bc371e16..fdc722e4 100644 --- a/lib/phoenix_test/assertions.ex +++ b/lib/phoenix_test/assertions.ex @@ -47,6 +47,13 @@ defmodule PhoenixTest.Assertions do #{format_found_element(element)} """ + + {:found_many, elements} -> + raise """ + Expected not to find an element. + + But found #{Enum.count(elements)} elements with selector #{inspect(selector)} and text #{inspect(text)}: + """ end end diff --git a/lib/phoenix_test/query.ex b/lib/phoenix_test/query.ex index 4e69502b..1624e0cf 100644 --- a/lib/phoenix_test/query.ex +++ b/lib/phoenix_test/query.ex @@ -38,6 +38,14 @@ defmodule PhoenixTest.Query do {:found, element} -> element + + {:found_many, _elements} -> + msg = + """ + Found more than one element with selector #{inspect(selector)} and text #{inspect(text)}. + """ + + raise ArgumentError, msg end end @@ -81,21 +89,10 @@ defmodule PhoenixTest.Query do {:found, found_element} -> found_element - end - end - defp find_with_text(html, selector, text) do - elements_matched_selector = - html - |> Html.all(selector) - - Enum.find(elements_matched_selector, :not_found, fn element -> - Html.text(element) =~ text - end) - |> then(fn - :not_found -> {:not_found, elements_matched_selector} - found -> {:found, found} - end) + {:found_many, [found_element | _]} -> + found_element + end end defp find_one_of(html, elements) do @@ -110,9 +107,22 @@ defmodule PhoenixTest.Query do |> Enum.find({:not_found, elements}, fn {:not_found, _} -> false {:found, _} -> true + {:found_many, _} -> true end) end + defp find_with_text(html, selector, text) do + elements_matched_selector = Html.all(html, selector) + + elements_matched_selector + |> Enum.filter(fn element -> Html.text(element) =~ text end) + |> case do + [] -> {:not_found, elements_matched_selector} + [found] -> {:found, found} + [_ | _] = found_many -> {:found_many, found_many} + end + end + defp first(html, selector) do html |> Html.all(selector) diff --git a/test/phoenix_test/assertions_test.exs b/test/phoenix_test/assertions_test.exs index c2a21cf7..c443babb 100644 --- a/test/phoenix_test/assertions_test.exs +++ b/test/phoenix_test/assertions_test.exs @@ -118,9 +118,7 @@ defmodule PhoenixTest.AssertionsTest do msg = """ Expected not to find an element. - But found an element with selector ".multiple_links" and text "Multiple links": - - with content "Multiple links" + But found 2 elements with selector ".multiple_links" and text "Multiple links": """ assert_raise RuntimeError, msg, fn -> diff --git a/test/phoenix_test/static_test.exs b/test/phoenix_test/static_test.exs index fb146b63..70879657 100644 --- a/test/phoenix_test/static_test.exs +++ b/test/phoenix_test/static_test.exs @@ -37,11 +37,12 @@ defmodule PhoenixTest.StaticTest do |> assert_has("h1", "Page 2") end - test "follows first link when there are multiple links with same text", %{conn: conn} do - conn - |> visit("/page/index") - |> click_link("Multiple links") - |> assert_has("h1", "Page 3") + test "raises error when there are multiple links with same text", %{conn: conn} do + assert_raise ArgumentError, ~r/Found more than one element with selector/, fn -> + conn + |> visit("/page/index") + |> click_link("Multiple links") + end end test "handles navigation to a LiveView", %{conn: conn} do