More tests

This commit is contained in:
Kieran Eglin 2024-11-21 14:27:49 -08:00
parent a9870fb5d7
commit 8cc4783fde
No known key found for this signature in database
GPG key ID: 193984967FCF432D
4 changed files with 64 additions and 27 deletions

View file

@ -340,7 +340,7 @@ defmodule PinchflatWeb.CoreComponents do
end) end)
~H""" ~H"""
<div x-data={"{ enabled: #{@checked}}"} class=""> <div x-data={"{ enabled: #{@checked} }"} class="" phx-update="ignore" id={"#{@id}-wrapper"}>
<.label :if={@label} for={@id}> <.label :if={@label} for={@id}>
<%= @label %> <%= @label %>
<span :if={@label_suffix} class="text-xs text-bodydark"><%= @label_suffix %></span> <span :if={@label_suffix} class="text-xs text-bodydark"><%= @label_suffix %></span>

View file

@ -8,7 +8,6 @@ defmodule PinchflatWeb.Sources.IndexTableLive do
alias Pinchflat.Sources.Source alias Pinchflat.Sources.Source
alias Pinchflat.Media.MediaItem alias Pinchflat.Media.MediaItem
# TODO: test (and maybe remove existing index tests)
def render(assigns) do def render(assigns) do
~H""" ~H"""
<.table rows={@sources} table_class="text-white"> <.table rows={@sources} table_class="text-white">
@ -67,15 +66,15 @@ defmodule PinchflatWeb.Sources.IndexTableLive do
source = Sources.get_source!(params["id"]) source = Sources.get_source!(params["id"])
should_enable = params["value"] == "true" should_enable = params["value"] == "true"
{:ok, new_source} = Sources.update_source(source, %{enabled: should_enable}) {:ok, _} = Sources.update_source(source, %{enabled: should_enable})
# Trying to be efficient with the update. Let's see if it pays off
updated_sources = Enum.map(socket.assigns.sources, fn s -> if s.id == new_source.id, do: new_source, else: s end)
{:noreply, assign(socket, sources: updated_sources)} # NOTE: I'm not re-rendering the view here since, at least in this case, the UI's state
# tracking mechanisms are good enough
{:noreply, assign(socket, %{sources: get_sources()})}
end end
defp get_sources do defp get_sources do
source_query = query =
from s in Source, from s in Source,
as: :source, as: :source,
inner_join: mp in assoc(s, :media_profile), inner_join: mp in assoc(s, :media_profile),
@ -101,6 +100,6 @@ defmodule PinchflatWeb.Sources.IndexTableLive do
) )
} }
Repo.all(source_query) Repo.all(query)
end end
end end

View file

@ -34,27 +34,10 @@ defmodule PinchflatWeb.SourceControllerTest do
end end
describe "index" do describe "index" do
test "lists all sources", %{conn: conn} do # Most of the tests are in `index_table_list_test.exs`
source = source_fixture() test "returns 200", %{conn: conn} do
conn = get(conn, ~p"/sources") conn = get(conn, ~p"/sources")
assert html_response(conn, 200) =~ "Sources" assert html_response(conn, 200) =~ "Sources"
assert html_response(conn, 200) =~ source.custom_name
end
test "omits sources that have marked_for_deletion_at set", %{conn: conn} do
source = source_fixture(marked_for_deletion_at: DateTime.utc_now())
conn = get(conn, ~p"/sources")
refute html_response(conn, 200) =~ source.custom_name
end
test "omits sources who's media profile has marked_for_deletion_at set", %{conn: conn} do
media_profile = media_profile_fixture(marked_for_deletion_at: DateTime.utc_now())
source = source_fixture(media_profile_id: media_profile.id)
conn = get(conn, ~p"/sources")
refute html_response(conn, 200) =~ source.custom_name
end end
end end

View file

@ -0,0 +1,55 @@
defmodule PinchflatWeb.Sources.IndexTableLiveTest do
use PinchflatWeb.ConnCase
import Phoenix.LiveViewTest
import Pinchflat.SourcesFixtures
import Pinchflat.ProfilesFixtures
alias Pinchflat.Sources.Source
alias PinchflatWeb.Sources.IndexTableLive
describe "initial rendering" do
test "lists all sources", %{conn: conn} do
source = source_fixture()
{:ok, _view, html} = live_isolated(conn, IndexTableLive)
assert html =~ source.custom_name
end
test "omits sources that have marked_for_deletion_at set", %{conn: conn} do
source = source_fixture(marked_for_deletion_at: DateTime.utc_now())
{:ok, _view, html} = live_isolated(conn, IndexTableLive)
refute html =~ source.custom_name
end
test "omits sources who's media profile has marked_for_deletion_at set", %{conn: conn} do
media_profile = media_profile_fixture(marked_for_deletion_at: DateTime.utc_now())
source = source_fixture(media_profile_id: media_profile.id)
{:ok, _view, html} = live_isolated(conn, IndexTableLive)
refute html =~ source.custom_name
end
end
describe "when a source is enabled or disabled" do
test "updates the source's enabled status", %{conn: conn} do
source = source_fixture(enabled: true)
{:ok, view, _html} = live_isolated(conn, IndexTableLive)
params = %{
"event" => "toggle_enabled",
"id" => source.id,
"value" => "false"
}
# Send an event to the server directly
render_change(view, "formless-input", params)
assert %{enabled: false} = Repo.get!(Source, source.id)
end
end
end