From 767af30c83a6d1eca3cfcfff3d622f0cf915ed7f Mon Sep 17 00:00:00 2001 From: sorax Date: Tue, 3 Sep 2024 20:14:55 +0200 Subject: [PATCH] test: split in seperatefiles --- test/radiator_web/live/episode_live_test.exs | 158 ++----------------- test/radiator_web/live/outline_live_test.exs | 148 +++++++++++++++++ 2 files changed, 165 insertions(+), 141 deletions(-) create mode 100644 test/radiator_web/live/outline_live_test.exs diff --git a/test/radiator_web/live/episode_live_test.exs b/test/radiator_web/live/episode_live_test.exs index ee842b08..85464bc6 100644 --- a/test/radiator_web/live/episode_live_test.exs +++ b/test/radiator_web/live/episode_live_test.exs @@ -4,12 +4,6 @@ defmodule RadiatorWeb.EpisodeLiveTest do import Phoenix.LiveViewTest import Radiator.AccountsFixtures import Radiator.PodcastFixtures - import Radiator.OutlineFixtures - - alias Radiator.Outline - alias Radiator.Outline.NodeRepository - - @additional_keep_alive 2000 describe "Episode page is restricted" do setup do @@ -36,14 +30,24 @@ defmodule RadiatorWeb.EpisodeLiveTest do setup %{conn: conn} do user = user_fixture() show = show_fixture() + episode = episode_fixture(%{show_id: show.id}) - %{conn: log_in_user(conn, user), show: show} + %{conn: log_in_user(conn, user), show: show, episode: episode} end - test "has the title of the episode", %{conn: conn, show: show} do + test "has the title of the show and list of episodes", %{ + conn: conn, + show: show, + episode: episode + } do {:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show.id}") assert page_title(live) =~ show.title + + assert live + |> has_element?("aside a", episode.title) + + # assert page_title(live) =~ show.title end test "create new episode will create node as well", %{conn: conn, show: show} do @@ -54,141 +58,13 @@ defmodule RadiatorWeb.EpisodeLiveTest do |> render_click() =~ "New Episode" assert_patch(live, ~p"/admin/podcast/#{show.id}/new") - end - end - - describe "Episode outline nodes" do - setup %{conn: conn} do - user = user_fixture() - %{id: show_id} = show_fixture() - %{id: episode_id} = episode_fixture(%{show_id: show_id}) - - node_1 = - node_fixture( - episode_id: episode_id, - parent_id: nil, - prev_id: nil, - content: "node_1" - ) - - node_2 = - node_fixture( - episode_id: episode_id, - parent_id: nil, - prev_id: node_1.uuid, - content: "node_2" - ) - - node_3 = - node_fixture( - episode_id: episode_id, - parent_id: nil, - prev_id: node_2.uuid, - content: "node_3" - ) - - %{conn: log_in_user(conn, user), show_id: show_id, nodes: [node_1, node_2, node_3]} - end - test "lists all nodes", %{conn: conn, show_id: show_id, nodes: [node_1, node_2, node_3]} do - {:ok, _live, html} = live(conn, ~p"/admin/podcast/#{show_id}") + # assert live + # |> form("#episode-form", episode: %{number: 1, title: "some episode"}) + # |> render_submit() - assert html =~ node_1.content - assert html =~ node_2.content - assert html =~ node_3.content + # assert live + # |> has_element?("#outline form input") end - - test "update node content", %{conn: conn, show_id: show_id, nodes: [node_1 | _]} do - {:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") - {:ok, other_live, _other_html} = live(conn, ~p"/admin/podcast/#{show_id}") - - assert live - |> form("#form-#{node_1.uuid}", node: %{content: "node_1_updated"}) - |> render_change() - - keep_liveview_alive() - - updated_node = NodeRepository.get_node!(node_1.uuid) - assert updated_node.uuid == node_1.uuid - assert updated_node.parent_id == node_1.parent_id - assert updated_node.prev_id == node_1.prev_id - assert updated_node.content == "node_1_updated" - - assert other_live - |> has_element?("#form-#{node_1.uuid} input[value=node_1_updated]") - end - - test "insert a new node", %{conn: conn, show_id: show_id, nodes: [_, node_2 | _]} do - {:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") - {:ok, other_live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") - - assert live - |> form("#form-#{node_2.uuid}", node: %{content: "node_2_updated"}) - |> render_submit() - - keep_liveview_alive() - - siblings = Outline.get_all_siblings(nil) - node_2_1 = Enum.find(siblings, &(&1.prev_id == node_2.uuid)) - - assert node_2_1.parent_id == node_2.parent_id - assert node_2_1.prev_id == node_2.uuid - assert node_2_1.content == nil - - assert other_live - |> has_element?("#form-#{node_2_1.uuid}") - end - - # test "move node", %{conn: conn, show: show, episode: episode, nodes: [node_1 | _]} do - # %{uuid: uuid1, parent_id: parent_id1} = node_1 - - # uuid2 = Ecto.UUID.generate() - - # node2 = - # node_fixture(%{ - # uuid: uuid2, - # episode_id: episode.id, - # parent_id: parent_id1, - # prev_id: uuid1 - # }) - - # {:ok, _live, _html} = live(conn, ~p"/admin/podcast/#{show.id}") - # {:ok, _other_live, _html} = live(conn, ~p"/admin/podcast/#{show.id}") - - # _params = node2 |> Map.merge(%{parent_id: uuid1, prev_id: nil}) |> Map.from_struct() - - # # assert live |> render_hook(:move_node, params) - - # # keep_liveview_alive() - - # # assert %Node{parent_id: ^uuid1} = NodeRepository.get_node!(uuid2) - - # # assert_push_event(live, "clean", %{node: %{uuid: ^uuid2}}) - - # # assert_push_event(other_live, "move", %{ - # # node: %{uuid: ^uuid2, parent_id: ^uuid1, prev_id: nil} - # # }) - # end - - test "delete node", %{conn: conn, show_id: show_id, nodes: [_, _, node_3]} do - {:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") - {:ok, other_live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") - - assert live - |> element("#form-#{node_3.uuid} input") - |> render_keydown(%{"key" => "Delete", "value" => ""}) - - keep_liveview_alive() - - siblings = Outline.get_all_siblings(nil) - assert Enum.find(siblings, &(&1.prev_id == node_3.uuid)) == nil - - refute other_live - |> has_element?("#form-#{node_3.uuid}") - end - end - - defp keep_liveview_alive do - :timer.sleep(@additional_keep_alive) end end diff --git a/test/radiator_web/live/outline_live_test.exs b/test/radiator_web/live/outline_live_test.exs new file mode 100644 index 00000000..d4a6c0a7 --- /dev/null +++ b/test/radiator_web/live/outline_live_test.exs @@ -0,0 +1,148 @@ +defmodule RadiatorWeb.OutlineLiveTest do + use RadiatorWeb.ConnCase + + import Phoenix.LiveViewTest + import Radiator.AccountsFixtures + import Radiator.PodcastFixtures + import Radiator.OutlineFixtures + + alias Radiator.Outline + alias Radiator.Outline.NodeRepository + + @additional_keep_alive 2000 + + describe "Episode outline nodes" do + setup %{conn: conn} do + user = user_fixture() + %{id: show_id} = show_fixture() + %{id: episode_id} = episode_fixture(%{show_id: show_id}) + + node_1 = + node_fixture( + episode_id: episode_id, + parent_id: nil, + prev_id: nil, + content: "node_1" + ) + + node_2 = + node_fixture( + episode_id: episode_id, + parent_id: nil, + prev_id: node_1.uuid, + content: "node_2" + ) + + node_3 = + node_fixture( + episode_id: episode_id, + parent_id: nil, + prev_id: node_2.uuid, + content: "node_3" + ) + + %{conn: log_in_user(conn, user), show_id: show_id, nodes: [node_1, node_2, node_3]} + end + + test "lists all nodes", %{conn: conn, show_id: show_id, nodes: [node_1, node_2, node_3]} do + {:ok, _live, html} = live(conn, ~p"/admin/podcast/#{show_id}") + + assert html =~ node_1.content + assert html =~ node_2.content + assert html =~ node_3.content + end + + test "update node content", %{conn: conn, show_id: show_id, nodes: [node_1 | _]} do + {:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") + {:ok, other_live, _other_html} = live(conn, ~p"/admin/podcast/#{show_id}") + + assert live + |> form("#form-#{node_1.uuid}", node: %{content: "node_1_updated"}) + |> render_change() + + keep_liveview_alive() + + updated_node = NodeRepository.get_node!(node_1.uuid) + assert updated_node.uuid == node_1.uuid + assert updated_node.parent_id == node_1.parent_id + assert updated_node.prev_id == node_1.prev_id + assert updated_node.content == "node_1_updated" + + assert other_live + |> has_element?("#form-#{node_1.uuid} input[value=node_1_updated]") + end + + test "insert a new node", %{conn: conn, show_id: show_id, nodes: [_, node_2 | _]} do + {:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") + {:ok, other_live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") + + assert live + |> form("#form-#{node_2.uuid}", node: %{content: "node_2_updated"}) + |> render_submit() + + keep_liveview_alive() + + siblings = Outline.get_all_siblings(nil) + node_2_1 = Enum.find(siblings, &(&1.prev_id == node_2.uuid)) + + assert node_2_1.parent_id == node_2.parent_id + assert node_2_1.prev_id == node_2.uuid + assert node_2_1.content == nil + + assert other_live + |> has_element?("#form-#{node_2_1.uuid}") + end + + # test "move node", %{conn: conn, show: show, episode: episode, nodes: [node_1 | _]} do + # %{uuid: uuid1, parent_id: parent_id1} = node_1 + + # uuid2 = Ecto.UUID.generate() + + # node2 = + # node_fixture(%{ + # uuid: uuid2, + # episode_id: episode.id, + # parent_id: parent_id1, + # prev_id: uuid1 + # }) + + # {:ok, _live, _html} = live(conn, ~p"/admin/podcast/#{show.id}") + # {:ok, _other_live, _html} = live(conn, ~p"/admin/podcast/#{show.id}") + + # _params = node2 |> Map.merge(%{parent_id: uuid1, prev_id: nil}) |> Map.from_struct() + + # # assert live |> render_hook(:move_node, params) + + # # keep_liveview_alive() + + # # assert %Node{parent_id: ^uuid1} = NodeRepository.get_node!(uuid2) + + # # assert_push_event(live, "clean", %{node: %{uuid: ^uuid2}}) + + # # assert_push_event(other_live, "move", %{ + # # node: %{uuid: ^uuid2, parent_id: ^uuid1, prev_id: nil} + # # }) + # end + + test "delete node", %{conn: conn, show_id: show_id, nodes: [_, _, node_3]} do + {:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") + {:ok, other_live, _html} = live(conn, ~p"/admin/podcast/#{show_id}") + + assert live + |> element("#form-#{node_3.uuid} input") + |> render_keydown(%{"key" => "Delete", "value" => ""}) + + keep_liveview_alive() + + siblings = Outline.get_all_siblings(nil) + assert Enum.find(siblings, &(&1.prev_id == node_3.uuid)) == nil + + refute other_live + |> has_element?("#form-#{node_3.uuid}") + end + end + + defp keep_liveview_alive do + :timer.sleep(@additional_keep_alive) + end +end