ML-182.4: convert tests to phoenix_test

This commit is contained in:
Claudio Ortolina
2026-05-16 16:30:42 +01:00
parent 07d65fe78f
commit 9f5a26198f
3 changed files with 68 additions and 47 deletions
@@ -1,9 +1,10 @@
--- ---
id: ML-182.4 id: ML-182.4
title: "Wave 4: Convert mixed-usage files (artist_live/show, stats_live/top_albums)" title: "Wave 4: Convert mixed-usage files (artist_live/show, stats_live/top_albums)"
status: To Do status: Done
assignee: [] assignee: []
created_date: "2026-05-14 22:14" created_date: "2026-05-14 22:14"
updated_date: "2026-05-16 15:30"
labels: labels:
- testing - testing
- refactoring - refactoring
@@ -2,6 +2,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
use MusicLibraryWeb.ConnCase use MusicLibraryWeb.ConnCase
import MusicLibrary.Fixtures.Records import MusicLibrary.Fixtures.Records
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
alias LastFm.Fixtures alias LastFm.Fixtures
@@ -181,14 +182,14 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
Test.json(conn, BraveSearch.Fixtures.search_images_response()) Test.json(conn, BraveSearch.Fixtures.search_images_response())
end) end)
{:ok, view, _html} = live(conn, ~p"/artists/#{musicbrainz_id}/edit") session =
conn
view |> visit(~p"/artists/#{musicbrainz_id}/edit")
|> element("#image-search-button") |> unwrap(&render_async/1)
|> render_click() |> click_button("#image-search-button", "Search")
|> unwrap(&render_async/1)
html = render_async(view)
html = Phoenix.LiveViewTest.render(session.view)
assert html =~ "https://thumbnails.example.com/raven-thumb.jpg" assert html =~ "https://thumbnails.example.com/raven-thumb.jpg"
end end
@@ -201,14 +202,14 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
Test.transport_error(conn, :timeout) Test.transport_error(conn, :timeout)
end) end)
{:ok, view, _html} = live(conn, ~p"/artists/#{musicbrainz_id}/edit") session =
conn
view |> visit(~p"/artists/#{musicbrainz_id}/edit")
|> element("#image-search-button") |> unwrap(&render_async/1)
|> render_click() |> click_button("#image-search-button", "Search")
|> unwrap(&render_async/1)
html = render_async(view)
html = Phoenix.LiveViewTest.render(session.view)
assert html =~ "Search failed" assert html =~ "Search failed"
end end
@@ -217,8 +218,10 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
artist_musicbrainz_id: musicbrainz_id, artist_musicbrainz_id: musicbrainz_id,
artist_info: artist_info artist_info: artist_info
} do } do
{:ok, view, _html} = live(conn, ~p"/artists/#{musicbrainz_id}/edit") conn
|> visit(~p"/artists/#{musicbrainz_id}/edit")
|> unwrap(&render_async/1)
|> unwrap(fn view ->
image = image =
file_input(view, "#artist-info-form", :image_data, [ file_input(view, "#artist-info-form", :image_data, [
%{name: "raven.jpg", content: raven_cover_data(), type: "image/jpeg"} %{name: "raven.jpg", content: raven_cover_data(), type: "image/jpeg"}
@@ -229,6 +232,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
view view
|> form("#artist-info-form") |> form("#artist-info-form")
|> render_submit() |> render_submit()
end)
updated = Artists.get_artist_info!(artist_info.id) updated = Artists.get_artist_info!(artist_info.id)
assert updated.image_data_hash != artist_info.image_data_hash assert updated.image_data_hash != artist_info.image_data_hash
@@ -254,18 +258,19 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
end end
end) end)
{:ok, view, _html} = live(conn, ~p"/artists/#{musicbrainz_id}/edit") conn
|> visit(~p"/artists/#{musicbrainz_id}/edit")
view |> element("#image-search-button") |> render_click() |> unwrap(&render_async/1)
render_async(view, 300) |> click_button("#image-search-button", "Search")
|> unwrap(&render_async/1)
|> unwrap(fn view ->
view view
|> element( |> element(
"button[phx-click='select_image'][phx-value-url='https://images.example.com/raven-cover.jpg']" "button[phx-click='select_image'][phx-value-url='https://images.example.com/raven-cover.jpg']"
) )
|> render_click() |> render_click()
end)
render_async(view, 300) |> unwrap(&render_async/1)
updated = Artists.get_artist_info!(artist_info.id) updated = Artists.get_artist_info!(artist_info.id)
assert updated.image_data_hash != artist_info.image_data_hash assert updated.image_data_hash != artist_info.image_data_hash
@@ -3,7 +3,6 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
import MusicLibrary.Fixtures.Records import MusicLibrary.Fixtures.Records
import MusicLibrary.ScrobbledTracksFixtures import MusicLibrary.ScrobbledTracksFixtures
import Phoenix.LiveViewTest
# Any release_id from the marbles release-group fixture. Records created by # Any release_id from the marbles release-group fixture. Records created by
# `record/0` include `musicbrainz_data: ReleaseGroup.release_group(:marbles)`, # `record/0` include `musicbrainz_data: ReleaseGroup.release_group(:marbles)`,
@@ -74,8 +73,12 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
%{conn: conn} do %{conn: conn} do
_record = record(%{purchased_at: DateTime.utc_now()}) _record = record(%{purchased_at: DateTime.utc_now()})
{:ok, view, _html} = live(conn, ~p"/") session =
html = render_async(view) conn
|> visit("/")
|> unwrap(&render_async/1)
html = Phoenix.LiveViewTest.render(session.view)
assert html =~ ~r|<img[^>]+src="/assets/[^"]+"[^>]+alt="Marbles"| assert html =~ ~r|<img[^>]+src="/assets/[^"]+"[^>]+alt="Marbles"|
refute html =~ ~s|src="https://example.com/cover.jpg"| refute html =~ ~s|src="https://example.com/cover.jpg"|
@@ -88,8 +91,12 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
test "single collected record: success badge and navigation to collection show", %{conn: conn} do test "single collected record: success badge and navigation to collection show", %{conn: conn} do
collected = record(%{purchased_at: DateTime.utc_now()}) collected = record(%{purchased_at: DateTime.utc_now()})
{:ok, view, _html} = live(conn, ~p"/") session =
html = render_async(view) conn
|> visit("/")
|> unwrap(&render_async/1)
html = Phoenix.LiveViewTest.render(session.view)
# The whole row is clickable, navigating to the collection show route. # The whole row is clickable, navigating to the collection show route.
assert html =~ ~s|/collection/#{collected.id}| assert html =~ ~s|/collection/#{collected.id}|
@@ -103,8 +110,12 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
%{conn: conn} do %{conn: conn} do
wishlisted = record(%{purchased_at: nil}) wishlisted = record(%{purchased_at: nil})
{:ok, view, _html} = live(conn, ~p"/") session =
html = render_async(view) conn
|> visit("/")
|> unwrap(&render_async/1)
html = Phoenix.LiveViewTest.render(session.view)
assert html =~ ~s|/wishlist/#{wishlisted.id}| assert html =~ ~s|/wishlist/#{wishlisted.id}|
assert html =~ "cursor-pointer" assert html =~ "cursor-pointer"
@@ -145,7 +156,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
|> refute_has("#top-albums div.cursor-pointer") |> refute_has("#top-albums div.cursor-pointer")
end end
test "no matching record: renders a plain badge and row is not navigable", %{conn: _conn} do test "no matching record: renders a plain badge and row is not navigable", %{conn: conn} do
# Replace the Marbles scrobbles with an orphan album that has no record. # Replace the Marbles scrobbles with an orphan album that has no record.
# The base setup created Marbles scrobbles — add one that will outrank # The base setup created Marbles scrobbles — add one that will outrank
# them in play_count so it appears first. # them in play_count so it appears first.
@@ -162,8 +173,12 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
}) })
end end
{:ok, view, _html} = live(build_conn() |> init_test_session(%{logged_in: true}), ~p"/") session =
html = render_async(view) conn
|> visit("/")
|> unwrap(&render_async/1)
html = Phoenix.LiveViewTest.render(session.view)
assert html =~ "Orphan Album" assert html =~ "Orphan Album"
# Plain badge is primary/surface — no success/warning color classes on # Plain badge is primary/surface — no success/warning color classes on