cannery/test/cannery_web/live/container_live_test.exs

336 lines
12 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.ContainerLiveTest do
2022-02-17 20:01:47 -05:00
@moduledoc """
Tests the containers liveviews
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb.ConnCase
import Phoenix.LiveViewTest
2022-02-08 22:10:48 -05:00
import CanneryWeb.Gettext
alias Cannery.{Containers, Repo}
2022-02-08 22:10:48 -05:00
2022-02-17 20:19:35 -05:00
@moduletag :container_live_test
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"
}
@ammo_type_attrs %{
"bullet_type" => "some bullet_type",
"case_material" => "some case_material",
"desc" => "some desc",
"manufacturer" => "some manufacturer",
"name" => "some name",
"grains" => 120
}
@ammo_group_attrs %{
"notes" => "some ammo group",
"count" => 20
}
@shot_group_attrs %{
"notes" => "some shot group",
"count" => 20
}
2021-09-02 23:31:14 -04:00
2022-02-17 20:01:47 -05:00
# @invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
2021-09-02 23:31:14 -04:00
2022-02-17 20:01:47 -05:00
defp create_container(%{current_user: current_user}) do
2022-02-17 20:19:35 -05:00
container = container_fixture(@create_attrs, current_user)
2021-09-02 23:31:14 -04:00
%{container: container}
end
defp create_ammo_group(%{container: container, current_user: current_user}) do
ammo_type = ammo_type_fixture(@ammo_type_attrs, current_user)
{1, [ammo_group]} = ammo_group_fixture(@ammo_group_attrs, ammo_type, container, current_user)
%{ammo_type: ammo_type, ammo_group: ammo_group}
end
defp create_empty_ammo_group(%{container: container, current_user: current_user}) do
ammo_type = ammo_type_fixture(@ammo_type_attrs, current_user)
{1, [ammo_group]} = ammo_group_fixture(@ammo_group_attrs, ammo_type, container, current_user)
shot_group = shot_group_fixture(@shot_group_attrs, current_user, ammo_group)
ammo_group = ammo_group |> Repo.reload!()
%{ammo_type: ammo_type, ammo_group: ammo_group, shot_group: shot_group}
end
2022-12-03 20:07:51 -05:00
describe "Index" do
2022-02-17 20:19:35 -05:00
setup [:register_and_log_in_user, :create_container]
2021-09-02 23:31:14 -04:00
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")
2022-02-17 20:19:35 -05:00
assert html =~ container.location
2021-09-02 23:31:14 -04:00
end
2022-12-03 20:07:51 -05:00
test "lists all containers in table mode", %{conn: conn, container: container} do
2022-11-10 19:02:35 -05:00
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
html =
index_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_table-label"}]/)
|> render_click()
2022-11-10 19:02:35 -05:00
2022-12-03 20:07:51 -05:00
assert html =~ gettext("Containers")
assert html =~ container.location
2022-11-10 19:02:35 -05:00
end
2022-12-03 20:07:51 -05:00
test "can search for containers", %{conn: conn, container: container} do
{:ok, index_live, html} = live(conn, Routes.container_index_path(conn, :index))
2022-11-10 19:02:35 -05:00
2022-12-03 20:07:51 -05:00
assert html =~ container.location
2022-11-10 19:02:35 -05:00
2022-12-03 20:07:51 -05:00
assert index_live
2023-03-15 00:45:08 -04:00
|> form(~s/form[phx-change="search"]/,
2022-12-03 20:07:51 -05:00
search: %{search_term: container.location}
)
|> render_change() =~ container.location
2022-11-10 19:02:35 -05:00
2022-12-03 20:07:51 -05:00
assert_patch(index_live, Routes.container_index_path(conn, :search, container.location))
2021-09-02 23:31:14 -04:00
2022-12-03 20:07:51 -05:00
refute index_live
2023-03-15 00:45:08 -04:00
|> form(~s/form[phx-change="search"]/, search: %{search_term: "something_else"})
2022-12-03 20:07:51 -05:00
|> render_change() =~ container.location
2022-11-10 18:51:24 -05:00
2022-12-03 20:07:51 -05:00
assert_patch(index_live, Routes.container_index_path(conn, :search, "something_else"))
2022-11-10 18:51:24 -05:00
2022-12-03 20:07:51 -05:00
assert index_live
2023-03-15 00:45:08 -04:00
|> form(~s/form[phx-change="search"]/, search: %{search_term: ""})
2022-12-03 20:07:51 -05:00
|> render_change() =~ container.location
2022-11-10 18:51:24 -05:00
2022-12-03 20:07:51 -05:00
assert_patch(index_live, Routes.container_index_path(conn, :index))
2022-11-10 18:51:24 -05:00
end
test "saves new container", %{conn: conn, container: container} do
2022-12-03 20:07:51 -05:00
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
2022-11-10 18:51:24 -05:00
assert index_live |> element("a", dgettext("actions", "New Container")) |> render_click() =~
gettext("New Container")
assert_patch(index_live, Routes.container_index_path(conn, :new))
# assert index_live
# |> form("#container-form", container: @invalid_attrs)
# |> render_change() =~ dgettext("errors", "can't be blank")
{:ok, _view, html} =
index_live
|> form("#container-form", container: @create_attrs)
|> render_submit()
2022-12-03 20:07:51 -05:00
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
2022-11-10 18:51:24 -05:00
assert html =~ dgettext("prompts", "%{name} created successfully", name: container.name)
assert html =~ "some location"
end
test "updates container in listing", %{
conn: conn,
current_user: current_user,
container: container
} do
2022-12-03 20:07:51 -05:00
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
2022-11-10 18:51:24 -05:00
2023-03-15 00:45:08 -04:00
assert index_live |> element(~s/a[aria-label="Edit #{container.name}"]/) |> render_click() =~
2022-11-10 18:51:24 -05:00
gettext("Edit %{name}", name: container.name)
assert_patch(index_live, Routes.container_index_path(conn, :edit, container))
# assert index_live
# |> form("#container-form", container: @invalid_attrs)
# |> render_change() =~ dgettext("errors", "can't be blank")
{:ok, _view, html} =
index_live
|> form("#container-form", container: @update_attrs)
|> render_submit()
2022-12-03 20:07:51 -05:00
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
2022-11-10 18:51:24 -05:00
container = container.id |> Containers.get_container!(current_user)
assert html =~ dgettext("prompts", "%{name} updated successfully", name: container.name)
assert html =~ "some updated location"
end
2022-11-10 19:02:35 -05:00
test "clones container in listing", %{
conn: conn,
current_user: current_user,
container: container
} do
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
html = index_live |> element(~s/a[aria-label="Clone #{container.name}"]/) |> render_click()
2022-11-10 19:02:35 -05:00
assert html =~ gettext("New Container")
assert html =~ "some location"
assert_patch(index_live, Routes.container_index_path(conn, :clone, container))
# assert index_live
# |> form("#container-form", container: @invalid_attrs)
# |> render_change() =~ dgettext("errors", "can't be blank")
{:ok, _view, html} =
index_live
|> form("#container-form", container: @create_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
container = container.id |> Containers.get_container!(current_user)
assert html =~ dgettext("prompts", "%{name} created successfully", name: container.name)
assert html =~ "some location"
end
test "clones container in listing with updates", %{
conn: conn,
current_user: current_user,
container: container
} do
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
assert index_live |> element(~s/a[aria-label="Clone #{container.name}"]/) |> render_click() =~
2022-11-10 19:02:35 -05:00
gettext("New Container")
assert_patch(index_live, Routes.container_index_path(conn, :clone, container))
# assert index_live
# |> form("#container-form", container: @invalid_attrs)
# |> render_change() =~ dgettext("errors", "can't be blank")
{:ok, _view, html} =
index_live
|> form("#container-form",
container: Map.merge(@create_attrs, %{location: "some updated location"})
)
|> render_submit()
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
container = container.id |> Containers.get_container!(current_user)
assert html =~ dgettext("prompts", "%{name} created successfully", name: container.name)
assert html =~ "some updated location"
end
2022-11-10 18:51:24 -05:00
test "deletes container in listing", %{conn: conn, container: container} do
2022-12-03 20:07:51 -05:00
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
2022-11-10 18:51:24 -05:00
2023-03-15 00:45:08 -04:00
assert index_live |> element(~s/a[aria-label="Delete #{container.name}"]/) |> render_click()
2021-09-02 23:31:14 -04:00
refute has_element?(index_live, "#container-#{container.id}")
end
end
describe "Show" do
2022-02-17 20:19:35 -05:00
setup [:register_and_log_in_user, :create_container]
2021-09-02 23:31:14 -04:00
test "displays container", %{
conn: conn,
container: %{name: name, location: location} = container
} do
2021-09-02 23:31:14 -04:00
{:ok, _show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
assert html =~ name
assert html =~ location
2021-09-02 23:31:14 -04:00
end
2022-02-17 20:19:35 -05:00
test "updates container within modal", %{
conn: conn,
current_user: current_user,
container: container
} do
2021-09-02 23:31:14 -04:00
{:ok, show_live, _html} = live(conn, Routes.container_show_path(conn, :show, container))
2023-03-15 00:45:08 -04:00
assert show_live |> element(~s/a[aria-label="Edit #{container.name}"]/) |> render_click() =~
2022-02-19 00:31:17 -05:00
gettext("Edit %{name}", name: container.name)
2021-09-02 23:31:14 -04:00
assert_patch(show_live, Routes.container_show_path(conn, :edit, container))
2022-02-17 20:01:47 -05:00
# assert show_live
# |> form("#container-form", container: @invalid_attrs)
# |> render_change() =~ dgettext("errors", "can't be blank")
2021-09-02 23:31:14 -04:00
2022-03-28 23:05:12 -04:00
{:ok, _view, html} =
2021-09-02 23:31:14 -04:00
show_live
|> form("#container-form", container: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.container_show_path(conn, :show, container))
2022-02-17 20:19:35 -05:00
container = container.id |> Containers.get_container!(current_user)
assert html =~ dgettext("prompts", "%{name} updated successfully", name: container.name)
assert html =~ "some updated location"
2021-09-02 23:31:14 -04:00
end
end
describe "Show with ammo group" do
setup [:register_and_log_in_user, :create_container, :create_ammo_group]
test "displays ammo group",
%{conn: conn, ammo_type: %{name: ammo_type_name}, container: container} do
{:ok, _show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
assert html =~ ammo_type_name
assert html =~ "some ammo group"
end
test "displays ammo group in table",
%{conn: conn, ammo_type: %{name: ammo_type_name}, container: container} do
{:ok, show_live, _html} = live(conn, Routes.container_show_path(conn, :show, container))
2023-03-15 00:45:08 -04:00
html =
show_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_table-label"}]/)
|> render_click()
assert html =~ ammo_type_name
assert html =~ "some ammo group"
end
end
describe "Show with empty ammo group" do
setup [:register_and_log_in_user, :create_container, :create_empty_ammo_group]
test "hides empty ammo groups by default",
%{conn: conn, ammo_type: %{name: ammo_type_name}, container: container} do
{:ok, show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
2022-11-09 18:45:28 -05:00
assert html =~ dgettext("actions", "Show used")
refute html =~ "some ammo group"
2023-03-15 00:45:08 -04:00
html =
show_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/)
|> render_click()
assert html =~ ammo_type_name
assert html =~ "some ammo group"
assert html =~ "Empty"
end
test "displays empty ammo groups in table on toggle",
%{conn: conn, ammo_type: %{name: ammo_type_name}, container: container} do
{:ok, show_live, _html} = live(conn, Routes.container_show_path(conn, :show, container))
2023-03-15 00:45:08 -04:00
html =
show_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_table-label"}]/)
|> render_click()
assert html =~ dgettext("actions", "Show used")
refute html =~ "some ammo group"
2023-03-15 00:45:08 -04:00
html =
show_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/)
|> render_click()
assert html =~ ammo_type_name
assert html =~ "some ammo group"
assert html =~ "Empty"
end
end
2021-09-02 23:31:14 -04:00
end