Simplify tests using PhoenixTest

This commit is contained in:
Claudio Ortolina
2025-10-22 10:43:10 +01:00
parent 40143df96d
commit 35aec967fe
2 changed files with 48 additions and 139 deletions
@@ -11,6 +11,7 @@
name="hero-magnifying-glass" name="hero-magnifying-glass"
class="absolute left-3 top-1/2 transform -translate-y-1/2 h-5 w-5 text-zinc-400" class="absolute left-3 top-1/2 transform -translate-y-1/2 h-5 w-5 text-zinc-400"
/> />
<label for="universal-search-input" class="sr-only">Universal Search</label>
<.input <.input
name="query" name="query"
id="universal-search-input" id="universal-search-input"
@@ -1,29 +1,16 @@
defmodule MusicLibraryWeb.UniversalSearchLive.IndexTest do defmodule MusicLibraryWeb.UniversalSearchLive.IndexTest do
use MusicLibraryWeb.ConnCase use MusicLibraryWeb.ConnCase
import Phoenix.LiveViewTest
import MusicLibrary.Fixtures.Records import MusicLibrary.Fixtures.Records
setup do setup do
# Create test data: collection and wishlist records
collection_record = record(%{title: "Dark Side of the Moon"}) collection_record = record(%{title: "Dark Side of the Moon"})
wishlist_record = wishlist_record = record(%{title: "Wish You Were Here", purchased_at: nil})
record(%{
title: "Wish You Were Here",
purchased_at: nil
})
artist_record = artist_record = record_with_artist("Steven Wilson", %{title: "Hand. Cannot. Erase."})
record_with_artist("Steven Wilson", %{
title: "Hand. Cannot. Erase."
})
# Create an artist info for searchable artist artist_info(artist_record.artists |> List.first() |> Map.get(:musicbrainz_id), %{})
artist_info(
artist_record.artists |> List.first() |> Map.get(:musicbrainz_id),
%{}
)
%{ %{
collection_record: collection_record, collection_record: collection_record,
@@ -34,114 +21,66 @@ defmodule MusicLibraryWeb.UniversalSearchLive.IndexTest do
describe "Modal visibility" do describe "Modal visibility" do
test "modal is hidden by default", %{conn: conn} do test "modal is hidden by default", %{conn: conn} do
session = visit(conn, ~p"/collection") conn
|> visit(~p"/collection")
refute_has(session, "#universal-search-root") |> refute_has("#universal-search-root")
end end
test "modal opens when search button is clicked", %{conn: conn} do test "modal opens when search button is clicked", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collection") conn
|> visit(~p"/collection")
# Open the modal by sending event to the live component |> click_button("Search (Cmd/Ctrl+K)")
view |> assert_has("#universal-search-root")
|> element("#universal-search-button") |> assert_has("#universal-search-input")
|> render_click()
html = render(view)
assert html =~ "universal-search-root"
assert html =~ "universal-search-input"
end end
test "modal shows empty state initially", %{conn: conn} do test "modal shows empty state initially", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collection") conn
|> visit(~p"/collection")
view |> click_button("Search (Cmd/Ctrl+K)")
|> element("#universal-search-button") |> assert_has("p", text: "to open this search")
|> render_click()
html = render(view)
assert html =~ "to open this search"
end end
end end
describe "Search functionality" do describe "Search functionality" do
test "shows no results message when query has no matches", %{conn: conn} do test "shows no results message when query has no matches", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collection") conn
|> visit(~p"/collection")
view |> click_button("Search (Cmd/Ctrl+K)")
|> element("#universal-search-button") |> fill_in("Universal Search", with: "nonexistent query xyz")
|> render_click() |> assert_has("p", text: "No results found for 'nonexistent query xyz'")
|> assert_has("a", text: "Add a record instead")
# Target the universal search input specifically
html =
view
|> form("#universal-search form", %{query: "nonexistent query xyz"})
|> render_change()
assert html =~ "No results found for &#39;nonexistent query xyz&#39;"
assert html =~ "Add a record instead"
end end
test "resets results when query is cleared", %{conn: conn} do test "resets results when query is cleared", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collection") conn
|> visit(~p"/collection")
view |> click_button("Search (Cmd/Ctrl+K)")
|> element("#universal-search-button") |> fill_in("Universal Search", with: "test query")
|> render_click() |> fill_in("Universal Search", with: "")
|> assert_has("p", text: "to open this search")
# First search for something
view
|> form("#universal-search form", %{query: "test query"})
|> render_change()
# Then clear the search
html =
view
|> form("#universal-search form", %{query: ""})
|> render_change()
assert html =~ "to open this search"
refute html =~ "No results found"
end end
test "searches collection records", %{ test "searches collection records", %{
conn: conn, conn: conn,
collection_record: collection_record collection_record: collection_record
} do } do
{:ok, view, _html} = live(conn, ~p"/collection") conn
|> visit(~p"/collection")
view |> click_button("Search (Cmd/Ctrl+K)")
|> element("#universal-search-button") |> fill_in("Universal Search", with: collection_record.title)
|> render_click() |> assert_has("p", text: collection_record.title)
html =
view
|> form("#universal-search form", %{query: collection_record.title})
|> render_change()
# Should display the collection record title
assert html =~ collection_record.title
end end
test "searches wishlist records", %{ test "searches wishlist records", %{
conn: conn, conn: conn,
wishlist_record: wishlist_record wishlist_record: wishlist_record
} do } do
{:ok, view, _html} = live(conn, ~p"/collection") conn
|> visit(~p"/collection")
view |> click_button("Search (Cmd/Ctrl+K)")
|> element("#universal-search-button") |> fill_in("Universal Search", with: wishlist_record.title)
|> render_click() |> assert_has("p", text: wishlist_record.title)
html =
view
|> form("#universal-search form", %{query: wishlist_record.title})
|> render_change()
# Should display the wishlist record title
assert html =~ wishlist_record.title
end end
test "searches artists", %{ test "searches artists", %{
@@ -150,53 +89,22 @@ defmodule MusicLibraryWeb.UniversalSearchLive.IndexTest do
} do } do
artist = List.first(artist_record.artists) artist = List.first(artist_record.artists)
{:ok, view, _html} = live(conn, ~p"/collection") conn
|> visit(~p"/collection")
view |> click_button("Search (Cmd/Ctrl+K)")
|> element("#universal-search-button") |> fill_in("Universal Search", with: artist.name)
|> render_click() |> assert_has("p", text: artist.name)
html =
view
|> form("#universal-search form", %{query: artist.name})
|> render_change()
# Should display the artist name
assert html =~ artist.name
end end
test "displays results count in footer", %{ test "displays results count in footer", %{
conn: conn, conn: conn,
collection_record: collection_record collection_record: collection_record
} do } do
{:ok, view, _html} = live(conn, ~p"/collection") conn
|> visit(~p"/collection")
view |> click_button("Search (Cmd/Ctrl+K)")
|> element("#universal-search-button") |> fill_in("Universal Search", with: collection_record.title)
|> render_click() |> assert_has("div", text: "1 result")
html =
view
|> form("#universal-search form", %{query: collection_record.title})
|> render_change()
# Should display results count
assert html =~ "result"
end
end
describe "Display and formatting" do
test "displays keyboard shortcut hints in empty state", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collection")
view
|> element("#universal-search-button")
|> render_click()
html = render(view)
# Should show keyboard shortcut info
assert html =~ "Cmd/Ctrl+K"
end end
end end
end end