Extract LiveView async test helper

This commit is contained in:
Claudio Ortolina
2026-05-16 22:27:46 +01:00
parent 4fa05d6ad1
commit 90ac4fa23a
10 changed files with 90 additions and 49 deletions
@@ -2,7 +2,9 @@ defmodule MusicLibraryWeb.Components.ChatTest do
use MusicLibraryWeb.ConnCase, async: false
import MusicLibrary.Fixtures.Records
import Phoenix.LiveViewTest
import Phoenix.LiveViewTest,
only: [element: 2, form: 3, live: 2, render: 1, render_click: 1, render_submit: 1]
alias MusicBrainz.Fixtures.Release, as: ReleaseFixtures
alias MusicBrainz.Fixtures.ReleaseGroup
@@ -12,7 +12,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
import MusicLibrary.Fixtures.Records
import Phoenix.LiveViewTest,
only: [element: 2, render: 1, render_async: 1, render_change: 2, render_click: 1]
only: [element: 2, render: 1, render_change: 2, render_click: 1]
alias MusicBrainz.Fixtures.Release, as: ReleaseFixtures
alias MusicLibrary.Secrets
@@ -83,7 +83,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("div[data-part=inner-prefix]", text: "Finished at")
end
@@ -93,7 +93,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
view
|> element("button[phx-click=scrobble_release][phx-target]")
@@ -116,7 +116,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
view
|> element(@sheet_form)
@@ -136,7 +136,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
view
|> element(@sheet_form)
@@ -172,7 +172,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
view
|> element(@sheet_form)
@@ -197,7 +197,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
view
|> element(@sheet_form)
@@ -221,7 +221,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> refute_has("button", text: "Scrobble selected")
end
@@ -232,7 +232,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
view
|> element(@sheet_form)
@@ -250,7 +250,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
view
|> element(@sheet_form)
@@ -267,7 +267,7 @@ defmodule MusicLibraryWeb.Components.ReleaseTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
view
|> element(@sheet_form)
@@ -323,8 +323,7 @@ end
defmodule MusicLibraryWeb.Components.ReleaseTest.ShowPrintTest do
use MusicLibraryWeb.ConnCase, async: false
import Phoenix.LiveViewTest,
only: [render: 1, render_async: 1]
import Phoenix.LiveViewTest, only: [render: 1]
alias MusicBrainz.Fixtures.Release, as: ReleaseFixtures
alias Req.Test
@@ -3,7 +3,15 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
import MusicLibrary.Fixtures.Records
import Phoenix.LiveViewTest
import Phoenix.LiveViewTest,
only: [
element: 2,
file_input: 4,
form: 2,
render_click: 1,
render_submit: 1,
render_upload: 2
]
alias LastFm.Fixtures
alias MusicLibrary.Artists
@@ -48,7 +56,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
conn
|> visit(~p"/artists/#{artist_musicbrainz_id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("span", "No scrobbles")
|> assert_has("dt", "Biography")
end
@@ -74,7 +82,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
conn
|> visit(~p"/artists/#{artist_musicbrainz_id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("dt", "Biography")
|> assert_has("span", "Wikipedia")
|> assert_has("p", text: "English musician")
@@ -96,7 +104,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
conn
|> visit(~p"/artists/#{artist_musicbrainz_id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("span", "No scrobbles")
|> refute_has("summary", "Biography")
|> assert_has("div", "Error loading biography")
@@ -118,7 +126,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
conn
|> visit(~p"/artists/#{artist_musicbrainz_id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("span", "United Kingdom")
|> assert_has("span", "🇬🇧")
|> assert_has("code", artist_musicbrainz_id)
@@ -150,7 +158,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
conn
|> visit(~p"/artists/#{artist_musicbrainz_id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("#collection p", escape(collection_record.title))
|> assert_has("#wishlist p", escape(wishlist_record.title))
|> refute_has("#collection p", escape(other_collection_record.title))
@@ -168,7 +176,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
} do
conn
|> visit(~p"/artists/#{musicbrainz_id}/edit")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("#artist-info-form")
|> assert_has("label", text: "Search for artist image online")
end
@@ -185,9 +193,9 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
session =
conn
|> visit(~p"/artists/#{musicbrainz_id}/edit")
|> unwrap(&render_async/1)
|> render_async()
|> click_button("#image-search-button", "Search")
|> unwrap(&render_async/1)
|> render_async()
html = Phoenix.LiveViewTest.render(session.view)
assert html =~ "https://thumbnails.example.com/raven-thumb.jpg"
@@ -205,9 +213,9 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
session =
conn
|> visit(~p"/artists/#{musicbrainz_id}/edit")
|> unwrap(&render_async/1)
|> render_async()
|> click_button("#image-search-button", "Search")
|> unwrap(&render_async/1)
|> render_async()
html = Phoenix.LiveViewTest.render(session.view)
assert html =~ "Search failed"
@@ -220,7 +228,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
} do
conn
|> visit(~p"/artists/#{musicbrainz_id}/edit")
|> unwrap(&render_async/1)
|> render_async()
|> unwrap(fn view ->
image =
file_input(view, "#artist-info-form", :image_data, [
@@ -260,9 +268,9 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
conn
|> visit(~p"/artists/#{musicbrainz_id}/edit")
|> unwrap(fn view -> render_async(view, 500) end)
|> render_async(500)
|> click_button("#image-search-button", "Search")
|> unwrap(fn view -> render_async(view, 500) end)
|> render_async(500)
|> unwrap(fn view ->
view
|> element(
@@ -270,7 +278,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
)
|> render_click()
end)
|> unwrap(&render_async/1)
|> render_async()
updated = Artists.get_artist_info!(artist_info.id)
assert updated.image_data_hash != artist_info.image_data_hash
@@ -20,7 +20,7 @@ defmodule MusicLibraryWeb.CollectionLive.ShowTest do
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("a", "Edit")
|> click_link("Edit")
|> assert_path(~p"/collection/#{record}/show/edit")
@@ -43,7 +43,7 @@ defmodule MusicLibraryWeb.CollectionLive.ShowTest do
session =
conn
|> visit(~p"/collection/#{record.id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("h2", escape(record.title))
|> assert_has("p", record.release_date)
|> assert_has("p", format_label(record.format))
@@ -82,7 +82,7 @@ defmodule MusicLibraryWeb.CollectionLive.ShowTest do
conn
|> visit(~p"/collection/#{record.id}")
|> assert_has("button", "Show Tracks")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("a", "Connect Last.fm")
release =
@@ -22,7 +22,7 @@ defmodule MusicLibraryWeb.MaintenanceLive.IndexTest do
test "async status resolves to :not_connected when no session key is stored", %{conn: conn} do
conn
|> visit(~p"/maintenance")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("span", "Not connected")
end
@@ -35,7 +35,7 @@ defmodule MusicLibraryWeb.MaintenanceLive.IndexTest do
conn
|> visit(~p"/maintenance")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("span", "Connected as alice")
end
end
@@ -33,7 +33,7 @@ defmodule MusicLibraryWeb.ScrobbleLive.ReleaseShowTest do
test "renders the scrobble UI for the release", %{conn: conn} do
conn
|> visit(~p"/scrobble/#{@rg_id}/releases/#{@release_id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("h2", text: "Marbles")
end
@@ -46,7 +46,7 @@ defmodule MusicLibraryWeb.ScrobbleLive.ReleaseShowTest do
test "sets the page title once the release loads", %{conn: conn} do
conn
|> visit(~p"/scrobble/#{@rg_id}/releases/#{@release_id}")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("title", text: "Marillion - Marbles")
end
end
@@ -45,7 +45,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
test "renders no album rows when no scrobbles exist", %{conn: conn} do
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
|> refute_has("#top-albums img")
end
end
@@ -56,7 +56,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
test "shows the album title, linked artist, and play count", %{conn: conn} do
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has("#top-albums p", text: "Marbles")
|> assert_has(~s|#top-albums a[href="/artists/#{@marillion_mbid}"]|, text: "Marillion")
|> assert_has("#top-albums span", text: "2")
@@ -65,7 +65,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
test "uses the Last.fm cover URL when no matching record exists", %{conn: conn} do
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
|> assert_has(~s|#top-albums img[src="https://example.com/cover.jpg"]|)
end
@@ -76,7 +76,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
session =
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
html = Phoenix.LiveViewTest.render(session.view)
@@ -94,7 +94,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
session =
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
html = Phoenix.LiveViewTest.render(session.view)
@@ -113,7 +113,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
session =
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
html = Phoenix.LiveViewTest.render(session.view)
@@ -146,7 +146,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
session =
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
session
|> assert_has("##{"top-album-#{@marbles_release_id}"}")
@@ -176,7 +176,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
session =
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
html = Phoenix.LiveViewTest.render(session.view)
@@ -206,7 +206,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
session =
conn
|> visit("/")
|> unwrap(&render_async/1)
|> render_async()
refute_has(session, "#top-albums", text: "Older Album")
@@ -214,7 +214,7 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbumsTest do
|> within("#top-albums", fn s ->
s
|> click_button("30d")
|> unwrap(&render_async/1)
|> render_async()
end)
assert_has(session, "#top-albums", text: "Older Album")
@@ -3,7 +3,8 @@ defmodule MusicLibraryWeb.LiveHelpers.RecordActionsTest do
use Oban.Testing, repo: MusicLibrary.BackgroundRepo
import MusicLibrary.Fixtures.Records
import Phoenix.LiveViewTest
import Phoenix.LiveViewTest, only: [live: 2, render: 1, render_click: 3]
alias MusicBrainz.Fixtures.Release, as: ReleaseFixtures
alias MusicBrainz.Fixtures.ReleaseGroup