diff --git a/test/music_library_web/live/artist_live/show_test.exs b/test/music_library_web/live/artist_live/show_test.exs new file mode 100644 index 00000000..d8a3145e --- /dev/null +++ b/test/music_library_web/live/artist_live/show_test.exs @@ -0,0 +1,38 @@ +defmodule MusicLibraryWeb.ArtistLive.ShowTest do + use MusicLibraryWeb.ConnCase + + import Phoenix.LiveViewTest + import MusicLibrary.RecordsFixtures + import LastFm.Fixtures + import Mox + + alias LastFm.{Artist, APIBehaviourMock} + + setup :verify_on_exit! + + describe "Show artist" do + test "it shows records from the collection", %{conn: conn} do + current_time = DateTime.utc_now() + + collection_record = + record_fixture_with_artist("Steven Wilson", %{purchased_at: current_time}) + + _wishlist_record = record_fixture_with_artist("Steven Wilson", %{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 + + expect(APIBehaviourMock, :get_artist_info, fn ^artist_musicbrainz_id, _config -> + {:ok, + artist_get_info() + |> Map.get("artist") + |> Artist.from_api_response()} + end) + + {:ok, show_live, _html} = live(conn, ~p"/artists/#{artist_musicbrainz_id}") + + assert render_async(show_live) =~ "Steven Wilson" + end + end +end diff --git a/test/support/fixtures/last_fm_fixtures.ex b/test/support/fixtures/last_fm_fixtures.ex new file mode 100644 index 00000000..53527aa5 --- /dev/null +++ b/test/support/fixtures/last_fm_fixtures.ex @@ -0,0 +1,9 @@ +defmodule LastFm.Fixtures do + @fixtures_folder Path.join([File.cwd!(), "test/support/fixtures"]) + + def artist_get_info do + Path.join([@fixtures_folder, "artist.getinfo.json"]) + |> File.read!() + |> Jason.decode!() + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index f22d9fbf..e3df04dd 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,5 +1,10 @@ Mox.defmock(MusicBrainz.APIBehaviourMock, for: MusicBrainz.APIBehaviour) Application.put_env(:music_library, :musicbrainz, MusicBrainz.APIBehaviourMock) +Mox.defmock(LastFm.APIBehaviourMock, for: LastFm.APIBehaviour) +last_fm_config = Application.get_env(:music_library, LastFm) +new_last_fm_config = Keyword.put(last_fm_config, :api, LastFm.APIBehaviourMock) +Application.put_env(:music_library, LastFm, new_last_fm_config) + ExUnit.start() Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.Repo, :manual)