cannery/test/cannery_web/live/range_live_test.exs

183 lines
6.3 KiB
Elixir
Raw Normal View History

2022-02-16 23:30:12 -05:00
defmodule CanneryWeb.RangeLiveTest do
@moduledoc """
This module tests the Range LiveViews
"""
use CanneryWeb.ConnCase
import Phoenix.LiveViewTest
import Cannery.Fixtures
@moduletag :range_live_test
2023-03-28 21:57:29 -04:00
@create_attrs %{ammo_left: 5, notes: "some notes"}
@update_attrs %{count: 16, notes: "some updated notes"}
@invalid_attrs %{count: nil, notes: nil}
2022-02-16 23:30:12 -05:00
defp create_shot_group(%{current_user: current_user}) do
2023-03-28 21:57:29 -04:00
container = container_fixture(%{staged: true}, current_user)
2022-02-16 23:30:12 -05:00
ammo_type = ammo_type_fixture(current_user)
2022-02-24 00:16:23 -05:00
2023-03-29 22:54:55 -04:00
{1, [pack]} = pack_fixture(%{staged: true}, ammo_type, container, current_user)
2022-02-16 23:30:12 -05:00
shot_group =
2023-03-28 21:57:29 -04:00
%{count: 5, date: ~N[2022-02-13 03:17:00], notes: "some notes"}
2023-03-29 22:54:55 -04:00
|> shot_group_fixture(current_user, pack)
2022-02-16 23:30:12 -05:00
2023-03-23 22:07:25 -04:00
[
container: container,
ammo_type: ammo_type,
2023-03-29 22:54:55 -04:00
pack: pack,
2023-03-23 22:07:25 -04:00
shot_group: shot_group
]
2022-02-16 23:30:12 -05:00
end
describe "Index" do
setup [:register_and_log_in_user, :create_shot_group]
test "lists all shot_groups", %{conn: conn, shot_group: shot_group} do
{:ok, _index_live, html} = live(conn, Routes.range_index_path(conn, :index))
2023-03-28 21:57:29 -04:00
assert html =~ "Range day"
2022-02-16 23:30:12 -05:00
assert html =~ shot_group.notes
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-28 23:08:40 -04:00
rifle_ammo_type = ammo_type_fixture(%{class: :rifle}, current_user)
2023-03-29 22:54:55 -04:00
{1, [rifle_pack]} = pack_fixture(rifle_ammo_type, container, current_user)
2023-03-23 22:07:25 -04:00
2023-03-29 22:54:55 -04:00
rifle_shot_group = shot_group_fixture(%{notes: "group_one"}, current_user, rifle_pack)
2023-03-23 22:07:25 -04:00
2023-03-28 23:08:40 -04:00
shotgun_ammo_type = ammo_type_fixture(%{class: :shotgun}, current_user)
2023-03-29 22:54:55 -04:00
{1, [shotgun_pack]} = pack_fixture(shotgun_ammo_type, container, current_user)
2023-03-23 22:07:25 -04:00
2023-03-29 22:54:55 -04:00
shotgun_shot_group = shot_group_fixture(%{notes: "group_two"}, current_user, shotgun_pack)
2023-03-23 22:07:25 -04:00
2023-03-28 23:08:40 -04:00
pistol_ammo_type = ammo_type_fixture(%{class: :pistol}, current_user)
2023-03-29 22:54:55 -04:00
{1, [pistol_pack]} = pack_fixture(pistol_ammo_type, container, current_user)
2023-03-23 22:07:25 -04:00
2023-03-29 22:54:55 -04:00
pistol_shot_group = shot_group_fixture(%{notes: "group_three"}, current_user, pistol_pack)
2023-03-23 22:07:25 -04:00
{:ok, index_live, html} = live(conn, Routes.range_index_path(conn, :index))
assert html =~ "All"
assert html =~ rifle_shot_group.notes
assert html =~ shotgun_shot_group.notes
assert html =~ pistol_shot_group.notes
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_shot_group.notes
refute html =~ shotgun_shot_group.notes
refute html =~ pistol_shot_group.notes
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_shot_group.notes
assert html =~ shotgun_shot_group.notes
refute html =~ pistol_shot_group.notes
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_shot_group.notes
refute html =~ shotgun_shot_group.notes
assert html =~ pistol_shot_group.notes
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_shot_group.notes
assert html =~ shotgun_shot_group.notes
assert html =~ pistol_shot_group.notes
end
2022-12-03 21:27:39 -05:00
test "can search for shot_group", %{conn: conn, shot_group: shot_group} do
{:ok, index_live, html} = live(conn, Routes.range_index_path(conn, :index))
assert html =~ shot_group.notes
assert index_live
2023-03-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: shot_group.notes}) =~ shot_group.notes
2022-12-03 21:27:39 -05:00
assert_patch(index_live, Routes.range_index_path(conn, :search, shot_group.notes))
refute index_live
2023-03-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: "something_else"}) =~ shot_group.notes
2022-12-03 21:27:39 -05:00
assert_patch(index_live, Routes.range_index_path(conn, :search, "something_else"))
assert index_live
2023-03-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: ""}) =~ shot_group.notes
2022-12-03 21:27:39 -05:00
assert_patch(index_live, Routes.range_index_path(conn, :index))
end
2023-03-29 22:54:55 -04:00
test "saves new shot_group", %{conn: conn, pack: pack} do
2022-02-16 23:30:12 -05:00
{:ok, index_live, _html} = live(conn, Routes.range_index_path(conn, :index))
2023-03-28 21:57:29 -04:00
assert index_live |> element("a", "Record shots") |> render_click() =~ "Record shots"
2023-03-29 22:54:55 -04:00
assert_patch(index_live, Routes.range_index_path(conn, :add_shot_group, pack))
2022-02-16 23:30:12 -05:00
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#shot-group-form")
|> render_change(shot_group: @invalid_attrs) =~ "can't be blank"
2022-02-16 23:30:12 -05:00
2022-03-28 23:05:12 -04:00
{:ok, _view, html} =
2022-02-16 23:30:12 -05:00
index_live
2023-03-28 21:57:29 -04:00
|> form("#shot-group-form")
|> render_submit(shot_group: @create_attrs)
2022-02-16 23:30:12 -05:00
|> follow_redirect(conn, Routes.range_index_path(conn, :index))
2023-03-28 21:57:29 -04:00
assert html =~ "Shots recorded successfully"
2022-02-16 23:30:12 -05:00
assert html =~ "some notes"
end
test "updates shot_group in listing", %{conn: conn, shot_group: shot_group} do
{:ok, index_live, _html} = live(conn, Routes.range_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
assert index_live
|> element(~s/a[aria-label="Edit shot record of #{shot_group.count} shots"]/)
2023-03-28 21:57:29 -04:00
|> render_click() =~ "Edit Shot Records"
2022-02-16 23:30:12 -05:00
assert_patch(index_live, Routes.range_index_path(conn, :edit, shot_group))
2023-03-28 21:57:29 -04:00
assert index_live
|> form("#shot-group-form")
|> render_change(shot_group: @invalid_attrs) =~ "can't be blank"
2022-02-16 23:30:12 -05:00
2022-03-28 23:05:12 -04:00
{:ok, _view, html} =
2022-02-16 23:30:12 -05:00
index_live
|> form("#shot-group-form", shot_group: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.range_index_path(conn, :index))
2023-03-28 21:57:29 -04:00
assert html =~ "Shot records updated successfully"
2022-02-16 23:30:12 -05:00
assert html =~ "some updated notes"
end
test "deletes shot_group in listing", %{conn: conn, shot_group: shot_group} do
{:ok, index_live, _html} = live(conn, Routes.range_index_path(conn, :index))
2023-03-15 00:45:08 -04:00
assert index_live
|> element(~s/a[aria-label="Delete shot record of #{shot_group.count} shots"]/)
|> render_click()
2022-02-16 23:30:12 -05:00
refute has_element?(index_live, "#shot_group-#{shot_group.id}")
end
end
end