memEx/test/memex_web/live/context_live_test.exs

126 lines
3.8 KiB
Elixir
Raw Normal View History

2022-07-25 20:11:08 -04:00
defmodule MemexWeb.ContextLiveTest do
use MemexWeb.ConnCase
import Phoenix.LiveViewTest
import Memex.ContextsFixtures
2022-11-17 22:38:52 -05:00
@create_attrs %{
"content" => "some content",
"tags_string" => "tag1",
"title" => "some title",
"visibility" => :public
}
2022-07-25 22:05:54 -04:00
@update_attrs %{
2022-11-17 22:38:52 -05:00
"content" => "some updated content",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"visibility" => :private
}
@invalid_attrs %{
"content" => nil,
"tags_string" => "",
"title" => nil,
"visibility" => nil
2022-07-25 22:05:54 -04:00
}
2022-07-25 20:11:08 -04:00
defp create_context(_) do
context = context_fixture()
%{context: context}
end
describe "Index" do
setup [:create_context]
test "lists all contexts", %{conn: conn, context: context} do
{:ok, _index_live, html} = live(conn, Routes.context_index_path(conn, :index))
2022-11-17 22:30:01 -05:00
assert html =~ "listing contexts"
2022-07-25 20:11:08 -04:00
assert html =~ context.content
end
test "saves new context", %{conn: conn} do
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
2022-11-17 22:30:01 -05:00
assert index_live |> element("a", "new context") |> render_click() =~
"new context"
2022-07-25 20:11:08 -04:00
assert_patch(index_live, Routes.context_index_path(conn, :new))
assert index_live
|> form("#context-form", context: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
index_live
|> form("#context-form", context: @create_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
2022-11-17 22:30:01 -05:00
assert html =~ "context created successfully"
2022-07-25 20:11:08 -04:00
assert html =~ "some content"
end
test "updates context in listing", %{conn: conn, context: context} do
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
2022-11-17 22:30:01 -05:00
assert index_live |> element("#context-#{context.id} a", "edit") |> render_click() =~
"edit context"
2022-07-25 20:11:08 -04:00
assert_patch(index_live, Routes.context_index_path(conn, :edit, context))
assert index_live
|> form("#context-form", context: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
index_live
|> form("#context-form", context: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
2022-11-17 22:30:01 -05:00
assert html =~ "context updated successfully"
2022-07-25 20:11:08 -04:00
assert html =~ "some updated content"
end
test "deletes context in listing", %{conn: conn, context: context} do
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
2022-11-17 22:30:01 -05:00
assert index_live |> element("#context-#{context.id} a", "delete") |> render_click()
2022-07-25 20:11:08 -04:00
refute has_element?(index_live, "#context-#{context.id}")
end
end
2022-11-17 22:30:01 -05:00
describe "show" do
2022-07-25 20:11:08 -04:00
setup [:create_context]
test "displays context", %{conn: conn, context: context} do
{:ok, _show_live, html} = live(conn, Routes.context_show_path(conn, :show, context))
2022-11-17 22:30:01 -05:00
assert html =~ "show context"
2022-07-25 20:11:08 -04:00
assert html =~ context.content
end
test "updates context within modal", %{conn: conn, context: context} do
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context))
2022-11-17 22:30:01 -05:00
assert show_live |> element("a", "edit") |> render_click() =~
"edit context"
2022-07-25 20:11:08 -04:00
assert_patch(show_live, Routes.context_show_path(conn, :edit, context))
assert show_live
|> form("#context-form", context: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
show_live
|> form("#context-form", context: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.context_show_path(conn, :show, context))
2022-11-17 22:30:01 -05:00
assert html =~ "context updated successfully"
2022-07-25 20:11:08 -04:00
assert html =~ "some updated content"
end
end
end