diff --git a/lib/pinchflat_web/components/core_components.ex b/lib/pinchflat_web/components/core_components.ex
index 0aeb34f..8f5ff75 100644
--- a/lib/pinchflat_web/components/core_components.ex
+++ b/lib/pinchflat_web/components/core_components.ex
@@ -340,7 +340,7 @@ defmodule PinchflatWeb.CoreComponents do
end)
~H"""
-
+
<.label :if={@label} for={@id}>
<%= @label %>
<%= @label_suffix %>
diff --git a/lib/pinchflat_web/controllers/sources/source_html/index_table_live.ex b/lib/pinchflat_web/controllers/sources/source_html/index_table_live.ex
index bba907b..f64d746 100644
--- a/lib/pinchflat_web/controllers/sources/source_html/index_table_live.ex
+++ b/lib/pinchflat_web/controllers/sources/source_html/index_table_live.ex
@@ -8,7 +8,6 @@ defmodule PinchflatWeb.Sources.IndexTableLive do
alias Pinchflat.Sources.Source
alias Pinchflat.Media.MediaItem
- # TODO: test (and maybe remove existing index tests)
def render(assigns) do
~H"""
<.table rows={@sources} table_class="text-white">
@@ -67,15 +66,15 @@ defmodule PinchflatWeb.Sources.IndexTableLive do
source = Sources.get_source!(params["id"])
should_enable = params["value"] == "true"
- {:ok, new_source} = 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)
+ {:ok, _} = Sources.update_source(source, %{enabled: should_enable})
- {: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
defp get_sources do
- source_query =
+ query =
from s in Source,
as: :source,
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
diff --git a/test/pinchflat_web/controllers/source_controller_test.exs b/test/pinchflat_web/controllers/source_controller_test.exs
index 5a6e0e7..42b574d 100644
--- a/test/pinchflat_web/controllers/source_controller_test.exs
+++ b/test/pinchflat_web/controllers/source_controller_test.exs
@@ -34,27 +34,10 @@ defmodule PinchflatWeb.SourceControllerTest do
end
describe "index" do
- test "lists all sources", %{conn: conn} do
- source = source_fixture()
+ # Most of the tests are in `index_table_list_test.exs`
+ test "returns 200", %{conn: conn} do
conn = get(conn, ~p"/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
diff --git a/test/pinchflat_web/controllers/sources/index_table_live_test.exs b/test/pinchflat_web/controllers/sources/index_table_live_test.exs
new file mode 100644
index 0000000..659bd04
--- /dev/null
+++ b/test/pinchflat_web/controllers/sources/index_table_live_test.exs
@@ -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