homeman-elixir/test/homeman_web/live/todo_live_test.exs

114 lines
3.3 KiB
Elixir
Raw Permalink Normal View History

2024-01-21 23:48:10 -06:00
defmodule HomemanWeb.TodoLiveTest do
use HomemanWeb.ConnCase
import Phoenix.LiveViewTest
import Homeman.AccountsFixtures
@create_attrs %{description: "some description", emoji: "some emoji", completed_at: "2024-01-21T05:40:00"}
@update_attrs %{description: "some updated description", emoji: "some updated emoji", completed_at: "2024-01-22T05:40:00"}
@invalid_attrs %{description: nil, emoji: nil, completed_at: nil}
defp create_todo(_) do
todo = todo_fixture()
%{todo: todo}
end
describe "Index" do
setup [:create_todo]
test "lists all todos", %{conn: conn, todo: todo} do
{:ok, _index_live, html} = live(conn, ~p"/todos")
assert html =~ "Listing Todos"
assert html =~ todo.description
end
test "saves new todo", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/todos")
assert index_live |> element("a", "New Todo") |> render_click() =~
"New Todo"
assert_patch(index_live, ~p"/todos/new")
assert index_live
|> form("#todo-form", todo: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert index_live
|> form("#todo-form", todo: @create_attrs)
|> render_submit()
assert_patch(index_live, ~p"/todos")
html = render(index_live)
assert html =~ "Todo created successfully"
assert html =~ "some description"
end
test "updates todo in listing", %{conn: conn, todo: todo} do
{:ok, index_live, _html} = live(conn, ~p"/todos")
assert index_live |> element("#todos-#{todo.id} a", "Edit") |> render_click() =~
"Edit Todo"
assert_patch(index_live, ~p"/todos/#{todo}/edit")
assert index_live
|> form("#todo-form", todo: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert index_live
|> form("#todo-form", todo: @update_attrs)
|> render_submit()
assert_patch(index_live, ~p"/todos")
html = render(index_live)
assert html =~ "Todo updated successfully"
assert html =~ "some updated description"
end
test "deletes todo in listing", %{conn: conn, todo: todo} do
{:ok, index_live, _html} = live(conn, ~p"/todos")
assert index_live |> element("#todos-#{todo.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#todos-#{todo.id}")
end
end
describe "Show" do
setup [:create_todo]
test "displays todo", %{conn: conn, todo: todo} do
{:ok, _show_live, html} = live(conn, ~p"/todos/#{todo}")
assert html =~ "Show Todo"
assert html =~ todo.description
end
test "updates todo within modal", %{conn: conn, todo: todo} do
{:ok, show_live, _html} = live(conn, ~p"/todos/#{todo}")
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Todo"
assert_patch(show_live, ~p"/todos/#{todo}/show/edit")
assert show_live
|> form("#todo-form", todo: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert show_live
|> form("#todo-form", todo: @update_attrs)
|> render_submit()
assert_patch(show_live, ~p"/todos/#{todo}")
html = render(show_live)
assert html =~ "Todo updated successfully"
assert html =~ "some updated description"
end
end
end