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

114 lines
3.3 KiB
Elixir
Raw Permalink Normal View History

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