Skip to content

Commit

Permalink
test: split in seperatefiles
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Sep 3, 2024
1 parent cb12454 commit 767af30
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 141 deletions.
158 changes: 17 additions & 141 deletions test/radiator_web/live/episode_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
148 changes: 148 additions & 0 deletions test/radiator_web/live/outline_live_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 767af30

Please sign in to comment.