Refactor test to extract data dependencies

This commit is contained in:
Claudio Ortolina
2024-12-14 17:16:55 +03:00
parent 04d60b09a4
commit 2fbe9db016
@@ -16,16 +16,25 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
|> Phoenix.HTML.safe_to_string()
end
defp fill_collection(_config) do
collection_record =
record_fixture_with_artist("Steven Wilson", %{
title: "The Raven that refused to sing",
purchased_at: DateTime.utc_now()
})
[artist] = collection_record.artists
%{collection_record: collection_record, artist_musicbrainz_id: artist.musicbrainz_id}
end
describe "Show artist" do
test "it shows the artist bio and play count", %{conn: conn} do
current_time = DateTime.utc_now()
collection_record =
record_fixture_with_artist("Steven Wilson", %{purchased_at: current_time})
[artist] = collection_record.artists
artist_musicbrainz_id = artist.musicbrainz_id
setup :fill_collection
test "it shows the artist bio and play count", %{
conn: conn,
artist_musicbrainz_id: artist_musicbrainz_id
} do
expect(APIBehaviourMock, :get_artist_info, fn {:musicbrainz_id, ^artist_musicbrainz_id},
_config ->
{:ok,
@@ -46,15 +55,10 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
assert element(show_live, "details")
end
test "it gracefully handles errors in fetching bio and play count", %{conn: conn} do
current_time = DateTime.utc_now()
collection_record =
record_fixture_with_artist("Steven Wilson", %{purchased_at: current_time})
[artist] = collection_record.artists
artist_musicbrainz_id = artist.musicbrainz_id
test "it gracefully handles errors in fetching bio and play count", %{
conn: conn,
artist_musicbrainz_id: artist_musicbrainz_id
} do
expect(APIBehaviourMock, :get_artist_info, fn {:musicbrainz_id, ^artist_musicbrainz_id},
_config ->
{:error, :timeout}
@@ -72,25 +76,19 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
assert has_element?(show_live, "div", "There was an error loading the biography")
end
test "it shows records from the collection and the wishlist", %{conn: conn} do
current_time = DateTime.utc_now()
collection_record =
record_fixture_with_artist("Steven Wilson", %{
title: "The Raven that refused to sing",
purchased_at: current_time
})
test "it shows records from the collection and the wishlist", %{
conn: conn,
collection_record: collection_record,
artist_musicbrainz_id: artist_musicbrainz_id
} do
wishlist_record =
record_fixture_with_artist("Steven Wilson", %{
title: "Grace for drowning",
purchased_at: nil
})
other_record = record_fixture_with_artist("Porcupine Tree", %{purchased_at: current_time})
[artist] = collection_record.artists
artist_musicbrainz_id = artist.musicbrainz_id
other_collection_record =
record_fixture_with_artist("Porcupine Tree", %{purchased_at: DateTime.utc_now()})
{:ok, show_live, _html} = live(conn, ~p"/artists/#{artist_musicbrainz_id}")
@@ -101,8 +99,8 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
assert has_element?(show_live, "#wishlist p", escape(wishlist_record.title))
# other records
refute has_element?(show_live, "#collection p", escape(other_record.title))
refute has_element?(show_live, "#wishlist p", escape(other_record.title))
refute has_element?(show_live, "#collection p", escape(other_collection_record.title))
refute has_element?(show_live, "#wishlist p", escape(other_collection_record.title))
end
end
end