From 35aec967fe75349b6962f5d6d86b3dd75001ce55 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Wed, 22 Oct 2025 10:43:10 +0100 Subject: [PATCH] Simplify tests using PhoenixTest --- .../universal_search_live/index.html.heex | 1 + .../live/universal_search_live/index_test.exs | 186 +++++------------- 2 files changed, 48 insertions(+), 139 deletions(-) diff --git a/lib/music_library_web/live/universal_search_live/index.html.heex b/lib/music_library_web/live/universal_search_live/index.html.heex index 14893d7a..02279c9b 100644 --- a/lib/music_library_web/live/universal_search_live/index.html.heex +++ b/lib/music_library_web/live/universal_search_live/index.html.heex @@ -11,6 +11,7 @@ name="hero-magnifying-glass" class="absolute left-3 top-1/2 transform -translate-y-1/2 h-5 w-5 text-zinc-400" /> + <.input name="query" id="universal-search-input" diff --git a/test/music_library_web/live/universal_search_live/index_test.exs b/test/music_library_web/live/universal_search_live/index_test.exs index acf6f7ea..4f499b92 100644 --- a/test/music_library_web/live/universal_search_live/index_test.exs +++ b/test/music_library_web/live/universal_search_live/index_test.exs @@ -1,29 +1,16 @@ defmodule MusicLibraryWeb.UniversalSearchLive.IndexTest do use MusicLibraryWeb.ConnCase - import Phoenix.LiveViewTest import MusicLibrary.Fixtures.Records setup do - # Create test data: collection and wishlist records collection_record = record(%{title: "Dark Side of the Moon"}) - wishlist_record = - record(%{ - title: "Wish You Were Here", - purchased_at: nil - }) + wishlist_record = record(%{title: "Wish You Were Here", purchased_at: nil}) - artist_record = - record_with_artist("Steven Wilson", %{ - title: "Hand. Cannot. Erase." - }) + artist_record = 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, @@ -34,114 +21,66 @@ defmodule MusicLibraryWeb.UniversalSearchLive.IndexTest do describe "Modal visibility" do test "modal is hidden by default", %{conn: conn} do - session = visit(conn, ~p"/collection") - - refute_has(session, "#universal-search-root") + conn + |> visit(~p"/collection") + |> refute_has("#universal-search-root") end test "modal opens when search button is clicked", %{conn: conn} do - {:ok, view, _html} = live(conn, ~p"/collection") - - # Open the modal by sending event to the live component - view - |> element("#universal-search-button") - |> render_click() - - html = render(view) - - assert html =~ "universal-search-root" - assert html =~ "universal-search-input" + conn + |> visit(~p"/collection") + |> click_button("Search (Cmd/Ctrl+K)") + |> assert_has("#universal-search-root") + |> assert_has("#universal-search-input") end test "modal shows empty state initially", %{conn: conn} do - {:ok, view, _html} = live(conn, ~p"/collection") - - view - |> element("#universal-search-button") - |> render_click() - - html = render(view) - - assert html =~ "to open this search" + conn + |> visit(~p"/collection") + |> click_button("Search (Cmd/Ctrl+K)") + |> assert_has("p", text: "to open this search") end end describe "Search functionality" do test "shows no results message when query has no matches", %{conn: conn} do - {:ok, view, _html} = live(conn, ~p"/collection") - - view - |> element("#universal-search-button") - |> render_click() - - # Target the universal search input specifically - html = - view - |> form("#universal-search form", %{query: "nonexistent query xyz"}) - |> render_change() - - assert html =~ "No results found for 'nonexistent query xyz'" - assert html =~ "Add a record instead" + conn + |> visit(~p"/collection") + |> click_button("Search (Cmd/Ctrl+K)") + |> fill_in("Universal Search", with: "nonexistent query xyz") + |> assert_has("p", text: "No results found for 'nonexistent query xyz'") + |> assert_has("a", text: "Add a record instead") end test "resets results when query is cleared", %{conn: conn} do - {:ok, view, _html} = live(conn, ~p"/collection") - - view - |> element("#universal-search-button") - |> render_click() - - # 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" + conn + |> visit(~p"/collection") + |> click_button("Search (Cmd/Ctrl+K)") + |> fill_in("Universal Search", with: "test query") + |> fill_in("Universal Search", with: "") + |> assert_has("p", text: "to open this search") end test "searches collection records", %{ conn: conn, collection_record: collection_record } do - {:ok, view, _html} = live(conn, ~p"/collection") - - view - |> element("#universal-search-button") - |> render_click() - - html = - view - |> form("#universal-search form", %{query: collection_record.title}) - |> render_change() - - # Should display the collection record title - assert html =~ collection_record.title + conn + |> visit(~p"/collection") + |> click_button("Search (Cmd/Ctrl+K)") + |> fill_in("Universal Search", with: collection_record.title) + |> assert_has("p", text: collection_record.title) end test "searches wishlist records", %{ conn: conn, wishlist_record: wishlist_record } do - {:ok, view, _html} = live(conn, ~p"/collection") - - view - |> element("#universal-search-button") - |> render_click() - - html = - view - |> form("#universal-search form", %{query: wishlist_record.title}) - |> render_change() - - # Should display the wishlist record title - assert html =~ wishlist_record.title + conn + |> visit(~p"/collection") + |> click_button("Search (Cmd/Ctrl+K)") + |> fill_in("Universal Search", with: wishlist_record.title) + |> assert_has("p", text: wishlist_record.title) end test "searches artists", %{ @@ -150,53 +89,22 @@ defmodule MusicLibraryWeb.UniversalSearchLive.IndexTest do } do artist = List.first(artist_record.artists) - {:ok, view, _html} = live(conn, ~p"/collection") - - view - |> element("#universal-search-button") - |> render_click() - - html = - view - |> form("#universal-search form", %{query: artist.name}) - |> render_change() - - # Should display the artist name - assert html =~ artist.name + conn + |> visit(~p"/collection") + |> click_button("Search (Cmd/Ctrl+K)") + |> fill_in("Universal Search", with: artist.name) + |> assert_has("p", text: artist.name) end test "displays results count in footer", %{ conn: conn, collection_record: collection_record } do - {:ok, view, _html} = live(conn, ~p"/collection") - - view - |> element("#universal-search-button") - |> render_click() - - 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" + conn + |> visit(~p"/collection") + |> click_button("Search (Cmd/Ctrl+K)") + |> fill_in("Universal Search", with: collection_record.title) + |> assert_has("div", text: "1 result") end end end