cannery/test/cannery_web/live/container_live_test.exs

130 lines
4.3 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.ContainerLiveTest do
use CanneryWeb.ConnCase
import Phoenix.LiveViewTest
2022-02-08 22:10:48 -05:00
import CanneryWeb.Gettext
2021-09-02 23:31:14 -04:00
alias Cannery.Containers
2022-02-08 22:10:48 -05:00
alias Cannery.{Accounts.User, Containers.Container}
@moduletag :containers_live
2021-09-02 23:31:14 -04:00
2022-01-22 17:21:13 -05:00
@create_attrs %{
"desc" => "some desc",
"location" => "some location",
"name" => "some name",
"type" => "some type"
}
@update_attrs %{
"desc" => "some updated desc",
"location" => "some updated location",
"name" => "some updated name",
"type" => "some updated type"
}
2021-09-02 23:31:14 -04:00
@invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
2022-02-08 22:10:48 -05:00
@spec fixture(:container, User.t()) :: Container.t()
defp fixture(:container, user) do
{:ok, container} = Containers.create_container(@create_attrs, user)
2021-09-02 23:31:14 -04:00
container
end
2022-02-08 22:10:48 -05:00
defp create_container(%{user: user}) do
container = fixture(:container, user)
2021-09-02 23:31:14 -04:00
%{container: container}
end
describe "Index" do
setup [:create_container]
test "lists all containers", %{conn: conn, container: container} do
{:ok, _index_live, html} = live(conn, Routes.container_index_path(conn, :index))
2022-02-15 17:33:45 -05:00
assert html =~ gettext("Containers")
2021-09-02 23:31:14 -04:00
assert html =~ container.desc
end
test "saves new container", %{conn: conn} do
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
2022-02-08 22:10:48 -05:00
assert index_live |> element("a", gettext("New Container")) |> render_click() =~
gettext("New Container")
2021-09-02 23:31:14 -04:00
assert_patch(index_live, Routes.container_index_path(conn, :new))
assert index_live
|> form("#container-form", container: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
index_live
|> form("#container-form", container: @create_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
2022-02-08 22:10:48 -05:00
assert html =~ gettext("Container created successfully")
2021-09-02 23:31:14 -04:00
assert html =~ "some desc"
end
test "updates container in listing", %{conn: conn, container: container} do
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
assert index_live |> element("#container-#{container.id} a", "Edit") |> render_click() =~
2022-02-08 22:10:48 -05:00
gettext("Edit Container")
2021-09-02 23:31:14 -04:00
assert_patch(index_live, Routes.container_index_path(conn, :edit, container))
assert index_live
|> form("#container-form", container: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
index_live
|> form("#container-form", container: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
2022-02-08 22:10:48 -05:00
assert html =~ gettext("Container updated successfully")
2021-09-02 23:31:14 -04:00
assert html =~ "some updated desc"
end
test "deletes container in listing", %{conn: conn, container: container} do
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
assert index_live |> element("#container-#{container.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#container-#{container.id}")
end
end
describe "Show" do
setup [:create_container]
test "displays container", %{conn: conn, container: container} do
{:ok, _show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
2022-02-08 22:10:48 -05:00
assert html =~ gettext("Show Container")
2021-09-02 23:31:14 -04:00
assert html =~ container.desc
end
test "updates container within modal", %{conn: conn, container: container} do
{:ok, show_live, _html} = live(conn, Routes.container_show_path(conn, :show, container))
assert show_live |> element("a", "Edit") |> render_click() =~
2022-02-08 22:10:48 -05:00
gettext("Edit Container")
2021-09-02 23:31:14 -04:00
assert_patch(show_live, Routes.container_show_path(conn, :edit, container))
assert show_live
|> form("#container-form", container: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
show_live
|> form("#container-form", container: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.container_show_path(conn, :show, container))
2022-02-08 22:10:48 -05:00
assert html =~ gettext("Container updated successfully")
2021-09-02 23:31:14 -04:00
assert html =~ "some updated desc"
end
end
end