cannery/test/cannery_web/live/ammo_type_live_test.exs

401 lines
14 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.AmmoTypeLiveTest do
2022-02-17 20:21:26 -05:00
@moduledoc """
Tests the ammo type liveview
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb.ConnCase
import Phoenix.LiveViewTest
alias Cannery.{Ammo, Repo}
2021-09-02 23:31:14 -04:00
2022-02-17 20:21:26 -05:00
@moduletag :ammo_type_live_test
2022-01-22 17:21:13 -05:00
@create_attrs %{
2023-03-28 21:57:29 -04:00
bullet_type: "some bullet_type",
case_material: "some case_material",
desc: "some desc",
manufacturer: "some manufacturer",
name: "some name",
grains: 120
2022-01-22 17:21:13 -05:00
}
@update_attrs %{
2023-03-28 21:57:29 -04:00
bullet_type: "some updated bullet_type",
case_material: "some updated case_material",
desc: "some updated desc",
manufacturer: "some updated manufacturer",
name: "some updated name",
grains: 456
}
@invalid_attrs %{
bullet_type: nil,
case_material: nil,
desc: nil,
manufacturer: nil,
name: nil,
grains: nil
2022-01-22 17:21:13 -05:00
}
2023-03-29 22:54:55 -04:00
@pack_attrs %{
2023-03-29 23:49:45 -04:00
notes: "some pack",
2023-03-28 21:57:29 -04:00
count: 20
}
@shot_group_attrs %{
2023-03-28 21:57:29 -04:00
notes: "some shot group",
count: 20
}
2021-09-02 23:31:14 -04:00
2022-02-17 20:21:26 -05:00
defp create_ammo_type(%{current_user: current_user}) do
2023-03-28 21:57:29 -04:00
[ammo_type: ammo_type_fixture(@create_attrs, current_user)]
2021-09-02 23:31:14 -04:00
end
2023-03-29 22:54:55 -04:00
defp create_pack(%{ammo_type: ammo_type, current_user: current_user}) do
container = container_fixture(current_user)
2023-03-29 22:54:55 -04:00
{1, [pack]} = pack_fixture(@pack_attrs, ammo_type, container, current_user)
[pack: pack, container: container]
end
2023-03-29 22:54:55 -04:00
defp create_empty_pack(%{ammo_type: ammo_type, current_user: current_user}) do
container = container_fixture(current_user)
2023-03-29 22:54:55 -04:00
{1, [pack]} = pack_fixture(@pack_attrs, ammo_type, container, current_user)
shot_group = shot_group_fixture(@shot_group_attrs, current_user, pack)
pack = pack |> Repo.reload!()
[pack: pack, container: container, shot_group: shot_group]
end
2021-09-02 23:31:14 -04:00
describe "Index" do
2022-02-17 20:21:26 -05:00
setup [:register_and_log_in_user, :create_ammo_type]
2021-09-02 23:31:14 -04:00
test "lists all ammo_types", %{conn: conn, ammo_type: ammo_type} do
{:ok, _index_live, html} = live(conn, Routes.ammo_type_index_path(conn, :index))
2023-03-28 21:57:29 -04:00
assert html =~ "Catalog"
2021-09-02 23:31:14 -04:00
assert html =~ ammo_type.bullet_type
end
2023-03-28 23:08:40 -04:00
test "can sort by class", %{conn: conn, current_user: current_user} do
rifle_type = ammo_type_fixture(%{class: :rifle}, current_user)
shotgun_type = ammo_type_fixture(%{class: :shotgun}, current_user)
pistol_type = ammo_type_fixture(%{class: :pistol}, current_user)
2023-03-23 22:07:25 -04:00
{:ok, index_live, html} = live(conn, Routes.ammo_type_index_path(conn, :index))
assert html =~ "All"
assert html =~ rifle_type.name
assert html =~ shotgun_type.name
assert html =~ pistol_type.name
html =
index_live
2023-03-28 23:08:40 -04:00
|> form(~s/form[phx-change="change_class"]/)
|> render_change(ammo_type: %{class: :rifle})
2023-03-23 22:07:25 -04:00
assert html =~ rifle_type.name
refute html =~ shotgun_type.name
refute html =~ pistol_type.name
html =
index_live
2023-03-28 23:08:40 -04:00
|> form(~s/form[phx-change="change_class"]/)
|> render_change(ammo_type: %{class: :shotgun})
2023-03-23 22:07:25 -04:00
refute html =~ rifle_type.name
assert html =~ shotgun_type.name
refute html =~ pistol_type.name
html =
index_live
2023-03-28 23:08:40 -04:00
|> form(~s/form[phx-change="change_class"]/)
|> render_change(ammo_type: %{class: :pistol})
2023-03-23 22:07:25 -04:00
refute html =~ rifle_type.name
refute html =~ shotgun_type.name
assert html =~ pistol_type.name
html =
index_live
2023-03-28 23:08:40 -04:00
|> form(~s/form[phx-change="change_class"]/)
|> render_change(ammo_type: %{class: :all})
2023-03-23 22:07:25 -04:00
assert html =~ rifle_type.name
assert html =~ shotgun_type.name
assert html =~ pistol_type.name
end
2022-12-03 19:30:52 -05:00
test "can search for ammo_type", %{conn: conn, ammo_type: ammo_type} do
{:ok, index_live, html} = live(conn, Routes.ammo_type_index_path(conn, :index))
assert html =~ ammo_type.bullet_type
assert index_live
2023-03-23 22:07:25 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: ammo_type.bullet_type}) =~
ammo_type.bullet_type
2022-12-03 19:30:52 -05:00
assert_patch(index_live, Routes.ammo_type_index_path(conn, :search, ammo_type.bullet_type))
refute index_live
2023-03-23 22:07:25 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: "something_else"}) =~ ammo_type.bullet_type
2022-12-03 19:30:52 -05:00
assert_patch(index_live, Routes.ammo_type_index_path(conn, :search, "something_else"))
2022-12-03 18:22:51 -05:00
assert index_live
2023-03-23 22:07:25 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: ""}) =~ ammo_type.bullet_type
2022-12-03 18:22:51 -05:00
assert_patch(index_live, Routes.ammo_type_index_path(conn, :index))
2022-12-03 19:30:52 -05:00
end
2022-02-17 20:21:26 -05:00
test "saves new ammo_type", %{conn: conn, current_user: current_user, ammo_type: ammo_type} do
2021-09-02 23:31:14 -04:00
{:ok, index_live, _html} = live(conn, Routes.ammo_type_index_path(conn, :index))
2023-03-28 21:57:29 -04:00
assert index_live |> element("a", "New Ammo type") |> render_click() =~ "New Ammo type"
2021-09-02 23:31:14 -04:00
assert_patch(index_live, Routes.ammo_type_index_path(conn, :new))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#ammo_type-form")
|> render_change(ammo_type: @invalid_attrs) =~ "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
index_live
2023-03-23 22:07:25 -04:00
|> form("#ammo_type-form")
|> render_submit(ammo_type: @create_attrs)
2021-09-02 23:31:14 -04:00
|> follow_redirect(conn, Routes.ammo_type_index_path(conn, :index))
2022-02-17 20:21:26 -05:00
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
2023-03-28 21:57:29 -04:00
assert html =~ "#{ammo_type.name} created successfully"
2021-09-02 23:31:14 -04:00
assert html =~ "some bullet_type"
end
2022-02-17 20:21:26 -05:00
test "updates ammo_type in listing",
%{conn: conn, current_user: current_user, ammo_type: ammo_type} do
2021-09-02 23:31:14 -04:00
{:ok, index_live, _html} = live(conn, Routes.ammo_type_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
assert index_live |> element(~s/a[aria-label="Edit #{ammo_type.name}"]/) |> render_click() =~
2023-03-28 21:57:29 -04:00
"Edit #{ammo_type.name}"
2021-09-02 23:31:14 -04:00
assert_patch(index_live, Routes.ammo_type_index_path(conn, :edit, ammo_type))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#ammo_type-form")
|> render_change(ammo_type: @invalid_attrs) =~ "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
index_live
2023-03-23 22:07:25 -04:00
|> form("#ammo_type-form")
|> render_submit(ammo_type: @update_attrs)
2021-09-02 23:31:14 -04:00
|> follow_redirect(conn, Routes.ammo_type_index_path(conn, :index))
2022-02-17 20:21:26 -05:00
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
2023-03-28 21:57:29 -04:00
assert html =~ "#{ammo_type.name} updated successfully"
2021-09-02 23:31:14 -04:00
assert html =~ "some updated bullet_type"
end
2022-11-10 19:10:28 -05:00
test "clones ammo_type in listing",
%{conn: conn, current_user: current_user, ammo_type: ammo_type} do
{:ok, index_live, _html} = live(conn, Routes.ammo_type_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
html = index_live |> element(~s/a[aria-label="Clone #{ammo_type.name}"]/) |> render_click()
2023-03-28 21:57:29 -04:00
assert html =~ "New Ammo type"
2022-11-10 19:10:28 -05:00
assert html =~ "some bullet_type"
assert_patch(index_live, Routes.ammo_type_index_path(conn, :clone, ammo_type))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#ammo_type-form")
|> render_change(ammo_type: @invalid_attrs) =~ "can't be blank"
2022-11-10 19:10:28 -05:00
{:ok, _view, html} =
index_live
2023-03-23 22:07:25 -04:00
|> form("#ammo_type-form")
|> render_submit(ammo_type: @create_attrs)
2022-11-10 19:10:28 -05:00
|> follow_redirect(conn, Routes.ammo_type_index_path(conn, :index))
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
2023-03-28 21:57:29 -04:00
assert html =~ "#{ammo_type.name} created successfully"
2022-11-10 19:10:28 -05:00
assert html =~ "some bullet_type"
end
test "clones ammo_type in listing with updates",
%{conn: conn, current_user: current_user, ammo_type: ammo_type} do
{:ok, index_live, _html} = live(conn, Routes.ammo_type_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
html = index_live |> element(~s/a[aria-label="Clone #{ammo_type.name}"]/) |> render_click()
2023-03-28 21:57:29 -04:00
assert html =~ "New Ammo type"
2022-11-10 19:10:28 -05:00
assert html =~ "some bullet_type"
assert_patch(index_live, Routes.ammo_type_index_path(conn, :clone, ammo_type))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#ammo_type-form")
|> render_change(ammo_type: @invalid_attrs) =~ "can't be blank"
2022-11-10 19:10:28 -05:00
{:ok, _view, html} =
index_live
2023-03-23 22:07:25 -04:00
|> form("#ammo_type-form")
|> render_submit(
2023-03-28 21:57:29 -04:00
ammo_type: Map.merge(@create_attrs, %{bullet_type: "some updated bullet_type"})
2022-11-10 19:10:28 -05:00
)
|> follow_redirect(conn, Routes.ammo_type_index_path(conn, :index))
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
2023-03-28 21:57:29 -04:00
assert html =~ "#{ammo_type.name} created successfully"
2022-11-10 19:10:28 -05:00
assert html =~ "some updated bullet_type"
end
2021-09-02 23:31:14 -04:00
test "deletes ammo_type in listing", %{conn: conn, ammo_type: ammo_type} do
{:ok, index_live, _html} = live(conn, Routes.ammo_type_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
assert index_live |> element(~s/a[aria-label="Delete #{ammo_type.name}"]/) |> render_click()
2021-09-02 23:31:14 -04:00
refute has_element?(index_live, "#ammo_type-#{ammo_type.id}")
end
end
2023-03-29 23:49:45 -04:00
describe "Index with pack" do
2023-03-29 22:54:55 -04:00
setup [:register_and_log_in_user, :create_ammo_type, :create_pack]
2023-03-29 23:49:45 -04:00
test "shows used packs on toggle",
2023-03-29 22:54:55 -04:00
%{conn: conn, pack: pack, current_user: current_user} do
{:ok, index_live, html} = live(conn, Routes.ammo_type_index_path(conn, :index))
2023-03-28 21:57:29 -04:00
assert html =~ "Show used"
refute html =~ "Used rounds"
refute html =~ "Total ever rounds"
refute html =~ "Used packs"
refute html =~ "Total ever packs"
2023-03-15 00:45:08 -04:00
html =
index_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/)
|> render_click()
2023-03-28 21:57:29 -04:00
assert html =~ "Used rounds"
assert html =~ "Total ever rounds"
assert html =~ "Used packs"
assert html =~ "Total ever packs"
assert html =~ "\n20\n"
assert html =~ "\n0\n"
assert html =~ "\n1\n"
2023-03-29 22:54:55 -04:00
shot_group_fixture(%{count: 5}, current_user, pack)
{:ok, index_live, _html} = live(conn, Routes.ammo_type_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
html =
index_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/)
|> render_click()
assert html =~ "\n15\n"
assert html =~ "\n5\n"
end
end
describe "Show ammo type" do
2022-02-17 20:21:26 -05:00
setup [:register_and_log_in_user, :create_ammo_type]
2021-09-02 23:31:14 -04:00
test "displays ammo_type", %{
conn: conn,
ammo_type: %{name: name, bullet_type: bullet_type} = ammo_type
} do
2021-09-02 23:31:14 -04:00
{:ok, _show_live, html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
assert html =~ name
assert html =~ bullet_type
2021-09-02 23:31:14 -04:00
end
2022-02-17 20:21:26 -05:00
test "updates ammo_type within modal",
%{conn: conn, current_user: current_user, ammo_type: %{name: name} = ammo_type} do
2021-09-02 23:31:14 -04:00
{:ok, show_live, _html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
2023-03-15 00:45:08 -04:00
assert show_live |> element(~s/a[aria-label="Edit #{ammo_type.name}"]/) |> render_click() =~
2023-03-28 21:57:29 -04:00
"Edit #{name}"
2021-09-02 23:31:14 -04:00
assert_patch(show_live, Routes.ammo_type_show_path(conn, :edit, ammo_type))
2023-03-28 21:57:29 -04:00
assert show_live
|> form("#ammo_type-form")
|> render_change(ammo_type: @invalid_attrs) =~ "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
2023-03-23 22:07:25 -04:00
|> form("#ammo_type-form")
|> render_submit(ammo_type: @update_attrs)
2021-09-02 23:31:14 -04:00
|> follow_redirect(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
2022-02-17 20:21:26 -05:00
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
2023-03-28 21:57:29 -04:00
assert html =~ "#{ammo_type.name} updated successfully"
2021-09-02 23:31:14 -04:00
assert html =~ "some updated bullet_type"
end
end
2023-03-29 23:49:45 -04:00
describe "Show ammo type with pack" do
2023-03-29 22:54:55 -04:00
setup [:register_and_log_in_user, :create_ammo_type, :create_pack]
2023-03-29 23:49:45 -04:00
test "displays pack", %{
conn: conn,
ammo_type: %{name: ammo_type_name} = ammo_type,
container: %{name: container_name}
} do
{:ok, _show_live, html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
assert html =~ ammo_type_name
assert html =~ "\n20\n"
assert html =~ container_name
end
2023-03-29 23:49:45 -04:00
test "displays pack in table",
%{conn: conn, ammo_type: ammo_type, container: %{name: container_name}} do
{:ok, show_live, _html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
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 =~ "\n20\n"
assert html =~ container_name
end
end
2023-03-29 23:49:45 -04:00
describe "Show ammo type with empty pack" do
2023-03-29 22:54:55 -04:00
setup [:register_and_log_in_user, :create_ammo_type, :create_empty_pack]
2023-03-29 23:49:45 -04:00
test "displays empty packs on toggle",
%{conn: conn, ammo_type: ammo_type, container: %{name: container_name}} do
{:ok, show_live, html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
2023-03-28 21:57:29 -04:00
assert html =~ "Show used"
refute html =~ "\n20\n"
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 =~ "\n20\n"
assert html =~ "Empty"
assert html =~ container_name
end
2023-03-29 23:49:45 -04:00
test "displays empty packs in table on toggle",
%{conn: conn, ammo_type: ammo_type, container: %{name: container_name}} do
{:ok, show_live, _html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
2023-03-15 00:45:08 -04:00
html =
show_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_table-label"}]/)
|> render_click()
2023-03-28 21:57:29 -04:00
assert html =~ "Show used"
refute html =~ "\n20\n"
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 =~ "\n20\n"
assert html =~ "Empty"
assert html =~ container_name
end
end
2021-09-02 23:31:14 -04:00
end