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