cannery/test/cannery_web/live/container_live_test.exs

326 lines
11 KiB
Elixir
Raw Permalink 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
2023-03-23 22:07:25 -04:00
alias Cannery.Containers
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 %{
2023-03-28 21:57:29 -04:00
desc: "some desc",
location: "some location",
name: "some name",
type: "some type"
2022-01-22 17:21:13 -05:00
}
@update_attrs %{
2023-03-28 21:57:29 -04:00
desc: "some updated desc",
location: "some updated location",
name: "some updated name",
type: "some updated type"
2022-01-22 17:21:13 -05:00
}
2023-03-28 21:57:29 -04:00
@invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
2023-03-30 21:53:52 -04:00
@type_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
}
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
}
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)
2023-03-28 21:57:29 -04:00
[container: container]
2021-09-02 23:31:14 -04:00
end
2023-03-29 22:54:55 -04:00
defp create_pack(%{container: container, current_user: current_user}) do
2023-03-30 21:53:52 -04:00
type = type_fixture(@type_attrs, current_user)
{1, [pack]} = pack_fixture(@pack_attrs, type, container, current_user)
2023-03-30 21:53:52 -04:00
[type: type, pack: pack]
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))
2023-03-28 21:57:29 -04:00
assert html =~ "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
2023-03-28 21:57:29 -04:00
assert html =~ "Containers"
2022-12-03 20:07:51 -05:00
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-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: container.location}) =~ 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-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: "something_else"}) =~ 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-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: ""}) =~ 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
2023-03-28 21:57:29 -04:00
assert index_live |> element("a", "New Container") |> render_click() =~ "New Container"
2022-11-10 18:51:24 -05:00
assert_patch(index_live, Routes.container_index_path(conn, :new))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#container-form")
|> render_change(container: @invalid_attrs) =~ "can't be blank"
2022-11-10 18:51:24 -05:00
{:ok, _view, html} =
index_live
2023-03-28 21:57:29 -04:00
|> form("#container-form")
|> render_submit(container: @create_attrs)
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
2023-03-28 21:57:29 -04:00
assert html =~ "#{container.name} created successfully"
2022-11-10 18:51:24 -05:00
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() =~
2023-03-28 21:57:29 -04:00
"Edit #{container.name}"
2022-11-10 18:51:24 -05:00
assert_patch(index_live, Routes.container_index_path(conn, :edit, container))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#container-form")
|> render_change(container: @invalid_attrs) =~ "can't be blank"
2022-11-10 18:51:24 -05:00
{:ok, _view, html} =
index_live
2023-03-28 21:57:29 -04:00
|> form("#container-form")
|> render_submit(container: @update_attrs)
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)
2023-03-28 21:57:29 -04:00
assert html =~ "#{container.name} updated successfully"
2022-11-10 18:51:24 -05:00
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()
2023-03-28 21:57:29 -04:00
assert html =~ "New Container"
2022-11-10 19:02:35 -05:00
assert html =~ "some location"
assert_patch(index_live, Routes.container_index_path(conn, :clone, container))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#container-form")
|> render_change(container: @invalid_attrs) =~ "can't be blank"
2022-11-10 19:02:35 -05:00
{:ok, _view, html} =
index_live
2023-03-28 21:57:29 -04:00
|> form("#container-form")
|> render_submit(container: @create_attrs)
2022-11-10 19:02:35 -05:00
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
container = container.id |> Containers.get_container!(current_user)
2023-03-28 21:57:29 -04:00
assert html =~ "#{container.name} created successfully"
2022-11-10 19:02:35 -05:00
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() =~
2023-03-28 21:57:29 -04:00
"New Container"
2022-11-10 19:02:35 -05:00
assert_patch(index_live, Routes.container_index_path(conn, :clone, container))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#container-form")
|> render_change(container: @invalid_attrs) =~ "can't be blank"
2022-11-10 19:02:35 -05:00
{:ok, _view, html} =
index_live
2023-03-28 21:57:29 -04:00
|> form("#container-form")
|> render_submit(
2022-11-10 19:02:35 -05:00
container: Map.merge(@create_attrs, %{location: "some updated location"})
)
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
container = container.id |> Containers.get_container!(current_user)
2023-03-28 21:57:29 -04:00
assert html =~ "#{container.name} created successfully"
2022-11-10 19:02:35 -05:00
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))
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() =~
2023-03-28 21:57:29 -04:00
"Edit #{container.name}"
2021-09-02 23:31:14 -04:00
assert_patch(show_live, Routes.container_show_path(conn, :edit, container))
2023-03-28 21:57:29 -04:00
assert show_live
|> form("#container-form")
|> render_change(container: @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-28 21:57:29 -04:00
|> form("#container-form")
|> render_submit(container: @update_attrs)
2021-09-02 23:31:14 -04:00
|> 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)
2023-03-28 21:57:29 -04:00
assert html =~ "#{container.name} updated successfully"
2022-02-17 20:19:35 -05:00
assert html =~ "some updated location"
2021-09-02 23:31:14 -04:00
end
2023-03-23 22:07:25 -04:00
test "can sort by type",
%{conn: conn, container: container, current_user: current_user} do
2023-03-30 21:53:52 -04:00
rifle_type = type_fixture(%{class: :rifle}, current_user)
2023-03-29 22:54:55 -04:00
{1, [rifle_pack]} = pack_fixture(rifle_type, container, current_user)
2023-03-30 21:53:52 -04:00
shotgun_type = type_fixture(%{class: :shotgun}, current_user)
2023-03-29 22:54:55 -04:00
{1, [shotgun_pack]} = pack_fixture(shotgun_type, container, current_user)
2023-03-30 21:53:52 -04:00
pistol_type = type_fixture(%{class: :pistol}, current_user)
2023-03-29 22:54:55 -04:00
{1, [pistol_pack]} = pack_fixture(pistol_type, container, current_user)
2023-03-23 22:07:25 -04:00
{:ok, index_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
2023-03-23 22:07:25 -04:00
assert html =~ "All"
2023-03-30 21:53:52 -04:00
assert html =~ rifle_pack.type.name
assert html =~ shotgun_pack.type.name
assert html =~ pistol_pack.type.name
2023-03-15 00:45:08 -04:00
html =
2023-03-23 22:07:25 -04:00
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-30 21:53:52 -04:00
assert html =~ rifle_pack.type.name
refute html =~ shotgun_pack.type.name
refute html =~ pistol_pack.type.name
2023-03-23 22:07:25 -04:00
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-30 21:53:52 -04:00
refute html =~ rifle_pack.type.name
assert html =~ shotgun_pack.type.name
refute html =~ pistol_pack.type.name
2023-03-23 22:07:25 -04:00
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-30 21:53:52 -04:00
refute html =~ rifle_pack.type.name
refute html =~ shotgun_pack.type.name
assert html =~ pistol_pack.type.name
2023-03-15 00:45:08 -04:00
html =
2023-03-23 22:07:25 -04:00
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
2023-03-30 21:53:52 -04:00
assert html =~ rifle_pack.type.name
assert html =~ shotgun_pack.type.name
assert html =~ pistol_pack.type.name
2023-03-23 22:07:25 -04:00
end
end
2023-03-29 23:49:45 -04:00
describe "Show with pack" do
2023-03-29 22:54:55 -04:00
setup [:register_and_log_in_user, :create_container, :create_pack]
2023-03-23 22:07:25 -04:00
2023-03-29 23:49:45 -04:00
test "displays pack",
2023-03-30 21:53:52 -04:00
%{conn: conn, type: %{name: type_name}, container: container} do
2023-03-23 22:07:25 -04:00
{:ok, _show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
2023-03-30 21:53:52 -04:00
assert html =~ type_name
assert html =~ "\n20\n"
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: %{name: 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()
2023-03-30 21:53:52 -04:00
assert html =~ type_name
assert html =~ "\n20\n"
end
end
2021-09-02 23:31:14 -04:00
end