cannery/test/cannery_web/live/type_live_test.exs

401 lines
13 KiB
Elixir
Raw Normal View History

2023-03-30 21:53:52 -04:00
defmodule CanneryWeb.TypeLiveTest do
2022-02-17 20:21:26 -05:00
@moduledoc """
2023-03-30 21:53:52 -04:00
Tests the type liveview
2022-02-17 20:21:26 -05:00
"""
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
2023-03-30 21:53:52 -04:00
@moduletag :type_live_test
2022-02-17 20:21:26 -05:00
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
}
2023-03-30 20:43:30 -04:00
@shot_record_attrs %{
notes: "some shot recorddd",
2023-03-28 21:57:29 -04:00
count: 20
}
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
defp create_type(%{current_user: current_user}) do
[type: type_fixture(@create_attrs, current_user)]
2021-09-02 23:31:14 -04:00
end
2023-03-30 21:53:52 -04:00
defp create_pack(%{type: type, current_user: current_user}) do
container = container_fixture(current_user)
2023-03-30 21:53:52 -04:00
{1, [pack]} = pack_fixture(@pack_attrs, type, container, current_user)
2023-03-29 22:54:55 -04:00
[pack: pack, container: container]
end
2023-03-30 21:53:52 -04:00
defp create_empty_pack(%{type: type, current_user: current_user}) do
container = container_fixture(current_user)
2023-03-30 21:53:52 -04:00
{1, [pack]} = pack_fixture(@pack_attrs, type, container, current_user)
2023-03-30 20:43:30 -04:00
shot_record = shot_record_fixture(@shot_record_attrs, current_user, pack)
2023-03-29 22:54:55 -04:00
pack = pack |> Repo.reload!()
2023-03-30 20:43:30 -04:00
[pack: pack, container: container, shot_record: shot_record]
end
2021-09-02 23:31:14 -04:00
describe "Index" do
2023-03-30 21:53:52 -04:00
setup [:register_and_log_in_user, :create_type]
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
test "lists all types", %{conn: conn, type: type} do
{:ok, _index_live, html} = live(conn, Routes.type_index_path(conn, :index))
2023-03-28 21:57:29 -04:00
assert html =~ "Catalog"
2023-03-30 21:53:52 -04:00
assert html =~ type.bullet_type
2021-09-02 23:31:14 -04:00
end
2023-03-28 23:08:40 -04:00
test "can sort by class", %{conn: conn, current_user: current_user} do
2023-03-30 21:53:52 -04:00
rifle_type = type_fixture(%{class: :rifle}, current_user)
shotgun_type = type_fixture(%{class: :shotgun}, current_user)
pistol_type = type_fixture(%{class: :pistol}, current_user)
2023-03-23 22:07:25 -04:00
2023-03-30 21:53:52 -04:00
{:ok, index_live, html} = live(conn, Routes.type_index_path(conn, :index))
2023-03-23 22:07:25 -04:00
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"]/)
2023-03-30 21:53:52 -04:00
|> render_change(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"]/)
2023-03-30 21:53:52 -04:00
|> render_change(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"]/)
2023-03-30 21:53:52 -04:00
|> render_change(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"]/)
2023-03-30 21:53:52 -04:00
|> render_change(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
2023-03-30 21:53:52 -04:00
test "can search for type", %{conn: conn, type: type} do
{:ok, index_live, html} = live(conn, Routes.type_index_path(conn, :index))
2022-12-03 19:30:52 -05:00
2023-03-30 21:53:52 -04:00
assert html =~ type.bullet_type
2022-12-03 19:30:52 -05:00
assert index_live
2023-03-23 22:07:25 -04:00
|> form(~s/form[phx-change="search"]/)
2023-03-30 21:53:52 -04:00
|> render_change(search: %{search_term: type.bullet_type}) =~
type.bullet_type
2022-12-03 19:30:52 -05:00
2023-03-30 21:53:52 -04:00
assert_patch(index_live, Routes.type_index_path(conn, :search, type.bullet_type))
2022-12-03 19:30:52 -05:00
refute index_live
2023-03-23 22:07:25 -04:00
|> form(~s/form[phx-change="search"]/)
2023-03-30 21:53:52 -04:00
|> render_change(search: %{search_term: "something_else"}) =~ type.bullet_type
2022-12-03 19:30:52 -05:00
2023-03-30 21:53:52 -04:00
assert_patch(index_live, Routes.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"]/)
2023-03-30 21:53:52 -04:00
|> render_change(search: %{search_term: ""}) =~ type.bullet_type
2022-12-03 18:22:51 -05:00
2023-03-30 21:53:52 -04:00
assert_patch(index_live, Routes.type_index_path(conn, :index))
2022-12-03 19:30:52 -05:00
end
2023-03-30 21:53:52 -04:00
test "saves new type", %{conn: conn, current_user: current_user, type: type} do
{:ok, index_live, _html} = live(conn, Routes.type_index_path(conn, :index))
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
assert index_live |> element("a", "New Type") |> render_click() =~ "New Type"
assert_patch(index_live, Routes.type_index_path(conn, :new))
2021-09-02 23:31:14 -04:00
2023-03-28 21:57:29 -04:00
assert index_live
2023-03-30 21:53:52 -04:00
|> form("#type-form")
|> render_change(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-30 21:53:52 -04:00
|> form("#type-form")
|> render_submit(type: @create_attrs)
|> follow_redirect(conn, Routes.type_index_path(conn, :index))
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
type = type.id |> Ammo.get_type!(current_user)
assert html =~ "#{type.name} created successfully"
2021-09-02 23:31:14 -04:00
assert html =~ "some bullet_type"
end
2023-03-30 21:53:52 -04:00
test "updates type in listing",
%{conn: conn, current_user: current_user, type: type} do
{:ok, index_live, _html} = live(conn, Routes.type_index_path(conn, :index))
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
assert index_live |> element(~s/a[aria-label="Edit #{type.name}"]/) |> render_click() =~
"Edit #{type.name}"
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
assert_patch(index_live, Routes.type_index_path(conn, :edit, type))
2021-09-02 23:31:14 -04:00
2023-03-28 21:57:29 -04:00
assert index_live
2023-03-30 21:53:52 -04:00
|> form("#type-form")
|> render_change(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-30 21:53:52 -04:00
|> form("#type-form")
|> render_submit(type: @update_attrs)
|> follow_redirect(conn, Routes.type_index_path(conn, :index))
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
type = type.id |> Ammo.get_type!(current_user)
assert html =~ "#{type.name} updated successfully"
2021-09-02 23:31:14 -04:00
assert html =~ "some updated bullet_type"
end
2023-03-30 21:53:52 -04:00
test "clones type in listing",
%{conn: conn, current_user: current_user, type: type} do
{:ok, index_live, _html} = live(conn, Routes.type_index_path(conn, :index))
2022-11-10 19:10:28 -05:00
2023-03-30 21:53:52 -04:00
html = index_live |> element(~s/a[aria-label="Clone #{type.name}"]/) |> render_click()
assert html =~ "New Type"
2022-11-10 19:10:28 -05:00
assert html =~ "some bullet_type"
2023-03-30 21:53:52 -04:00
assert_patch(index_live, Routes.type_index_path(conn, :clone, type))
2022-11-10 19:10:28 -05:00
2023-03-28 21:57:29 -04:00
assert index_live
2023-03-30 21:53:52 -04:00
|> form("#type-form")
|> render_change(type: @invalid_attrs) =~ "can't be blank"
2022-11-10 19:10:28 -05:00
{:ok, _view, html} =
index_live
2023-03-30 21:53:52 -04:00
|> form("#type-form")
|> render_submit(type: @create_attrs)
|> follow_redirect(conn, Routes.type_index_path(conn, :index))
2022-11-10 19:10:28 -05:00
2023-03-30 21:53:52 -04:00
type = type.id |> Ammo.get_type!(current_user)
assert html =~ "#{type.name} created successfully"
2022-11-10 19:10:28 -05:00
assert html =~ "some bullet_type"
end
2023-03-30 21:53:52 -04:00
test "clones type in listing with updates",
%{conn: conn, current_user: current_user, type: type} do
{:ok, index_live, _html} = live(conn, Routes.type_index_path(conn, :index))
2022-11-10 19:10:28 -05:00
2023-03-30 21:53:52 -04:00
html = index_live |> element(~s/a[aria-label="Clone #{type.name}"]/) |> render_click()
assert html =~ "New Type"
2022-11-10 19:10:28 -05:00
assert html =~ "some bullet_type"
2023-03-30 21:53:52 -04:00
assert_patch(index_live, Routes.type_index_path(conn, :clone, type))
2022-11-10 19:10:28 -05:00
2023-03-28 21:57:29 -04:00
assert index_live
2023-03-30 21:53:52 -04:00
|> form("#type-form")
|> render_change(type: @invalid_attrs) =~ "can't be blank"
2022-11-10 19:10:28 -05:00
{:ok, _view, html} =
index_live
2023-03-30 21:53:52 -04:00
|> form("#type-form")
2023-03-23 22:07:25 -04:00
|> render_submit(
2023-03-30 21:53:52 -04:00
type: Map.merge(@create_attrs, %{bullet_type: "some updated bullet_type"})
2022-11-10 19:10:28 -05:00
)
2023-03-30 21:53:52 -04:00
|> follow_redirect(conn, Routes.type_index_path(conn, :index))
2022-11-10 19:10:28 -05:00
2023-03-30 21:53:52 -04:00
type = type.id |> Ammo.get_type!(current_user)
assert html =~ "#{type.name} created successfully"
2022-11-10 19:10:28 -05:00
assert html =~ "some updated bullet_type"
end
2023-03-30 21:53:52 -04:00
test "deletes type in listing", %{conn: conn, type: type} do
{:ok, index_live, _html} = live(conn, Routes.type_index_path(conn, :index))
assert index_live |> element(~s/a[aria-label="Delete #{type.name}"]/) |> render_click()
refute has_element?(index_live, "#type-#{type.id}")
2021-09-02 23:31:14 -04:00
end
end
2023-03-29 23:49:45 -04:00
describe "Index with pack" do
2023-03-30 21:53:52 -04:00
setup [:register_and_log_in_user, :create_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
2023-03-30 21:53:52 -04:00
{:ok, index_live, html} = live(conn, Routes.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-30 20:43:30 -04:00
shot_record_fixture(%{count: 5}, current_user, pack)
2023-03-30 21:53:52 -04:00
{:ok, index_live, _html} = live(conn, Routes.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
2023-03-30 21:53:52 -04:00
describe "Show type" do
setup [:register_and_log_in_user, :create_type]
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
test "displays type", %{
conn: conn,
2023-03-30 21:53:52 -04:00
type: %{name: name, bullet_type: bullet_type} = type
} do
2023-03-30 21:53:52 -04:00
{:ok, _show_live, html} = live(conn, Routes.type_show_path(conn, :show, type))
2021-09-02 23:31:14 -04:00
assert html =~ name
assert html =~ bullet_type
2021-09-02 23:31:14 -04:00
end
2023-03-30 21:53:52 -04:00
test "updates type within modal",
%{conn: conn, current_user: current_user, type: %{name: name} = type} do
{:ok, show_live, _html} = live(conn, Routes.type_show_path(conn, :show, type))
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
assert show_live |> element(~s/a[aria-label="Edit #{type.name}"]/) |> render_click() =~
2023-03-28 21:57:29 -04:00
"Edit #{name}"
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
assert_patch(show_live, Routes.type_show_path(conn, :edit, type))
2021-09-02 23:31:14 -04:00
2023-03-28 21:57:29 -04:00
assert show_live
2023-03-30 21:53:52 -04:00
|> form("#type-form")
|> render_change(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-30 21:53:52 -04:00
|> form("#type-form")
|> render_submit(type: @update_attrs)
|> follow_redirect(conn, Routes.type_show_path(conn, :show, type))
2021-09-02 23:31:14 -04:00
2023-03-30 21:53:52 -04:00
type = type.id |> Ammo.get_type!(current_user)
assert html =~ "#{type.name} updated successfully"
2021-09-02 23:31:14 -04:00
assert html =~ "some updated bullet_type"
end
end
2023-03-30 21:53:52 -04:00
describe "Show type with pack" do
setup [:register_and_log_in_user, :create_type, :create_pack]
2023-03-29 23:49:45 -04:00
test "displays pack", %{
conn: conn,
2023-03-30 21:53:52 -04:00
type: %{name: type_name} = type,
container: %{name: container_name}
} do
2023-03-30 21:53:52 -04:00
{:ok, _show_live, html} = live(conn, Routes.type_show_path(conn, :show, type))
2023-03-30 21:53:52 -04:00
assert html =~ type_name
assert html =~ "\n20\n"
assert html =~ container_name
end
2023-03-29 23:49:45 -04:00
test "displays pack in table",
2023-03-30 21:53:52 -04:00
%{conn: conn, type: type, container: %{name: container_name}} do
{:ok, show_live, _html} = live(conn, Routes.type_show_path(conn, :show, 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-30 21:53:52 -04:00
describe "Show type with empty pack" do
setup [:register_and_log_in_user, :create_type, :create_empty_pack]
2023-03-29 23:49:45 -04:00
test "displays empty packs on toggle",
2023-03-30 21:53:52 -04:00
%{conn: conn, type: type, container: %{name: container_name}} do
{:ok, show_live, html} = live(conn, Routes.type_show_path(conn, :show, 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",
2023-03-30 21:53:52 -04:00
%{conn: conn, type: type, container: %{name: container_name}} do
{:ok, show_live, _html} = live(conn, Routes.type_show_path(conn, :show, 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