cannery/test/cannery_web/live/range_live_test.exs

175 lines
6.0 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
"""
2023-04-14 23:48:50 -04:00
use CanneryWeb.ConnCase, async: true
2022-02-16 23:30:12 -05:00
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
2023-03-30 20:43:30 -04:00
defp create_shot_record(%{current_user: current_user}) do
2023-03-28 21:57:29 -04:00
container = container_fixture(%{staged: true}, current_user)
2023-03-30 21:53:52 -04:00
type = type_fixture(current_user)
2022-02-24 00:16:23 -05:00
2023-03-30 21:53:52 -04:00
{1, [pack]} = pack_fixture(%{staged: true}, type, container, current_user)
2022-02-16 23:30:12 -05:00
2023-03-30 20:43:30 -04:00
shot_record =
2023-03-28 21:57:29 -04:00
%{count: 5, date: ~N[2022-02-13 03:17:00], notes: "some notes"}
2023-03-30 20:43:30 -04:00
|> shot_record_fixture(current_user, pack)
2022-02-16 23:30:12 -05:00
2023-03-23 22:07:25 -04:00
[
container: container,
2023-03-30 21:53:52 -04:00
type: type,
2023-03-29 22:54:55 -04:00
pack: pack,
2023-03-30 20:43:30 -04:00
shot_record: shot_record
2023-03-23 22:07:25 -04:00
]
2022-02-16 23:30:12 -05:00
end
describe "Index" do
2023-03-30 20:43:30 -04:00
setup [:register_and_log_in_user, :create_shot_record]
2022-02-16 23:30:12 -05:00
2023-03-30 20:43:30 -04:00
test "lists all shot_records", %{conn: conn, shot_record: shot_record} do
2023-04-14 23:34:11 -04:00
{:ok, _index_live, html} = live(conn, ~p"/range")
2023-03-28 21:57:29 -04:00
assert html =~ "Range day"
2023-03-30 20:43:30 -04:00
assert html =~ shot_record.notes
2022-02-16 23:30:12 -05: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)
{1, [rifle_pack]} = pack_fixture(rifle_type, container, current_user)
2023-03-30 20:43:30 -04:00
rifle_shot_record = shot_record_fixture(%{notes: "group_one"}, current_user, rifle_pack)
2023-03-23 22:07:25 -04:00
2023-03-30 21:53:52 -04:00
shotgun_type = type_fixture(%{class: :shotgun}, current_user)
{1, [shotgun_pack]} = pack_fixture(shotgun_type, container, current_user)
2023-03-30 20:43:30 -04:00
shotgun_shot_record = shot_record_fixture(%{notes: "group_two"}, current_user, shotgun_pack)
2023-03-23 22:07:25 -04:00
2023-03-30 21:53:52 -04:00
pistol_type = type_fixture(%{class: :pistol}, current_user)
{1, [pistol_pack]} = pack_fixture(pistol_type, container, current_user)
2023-03-30 20:43:30 -04:00
pistol_shot_record = shot_record_fixture(%{notes: "group_three"}, current_user, pistol_pack)
2023-03-23 22:07:25 -04:00
2023-04-14 23:34:11 -04:00
{:ok, index_live, html} = live(conn, ~p"/range")
2023-03-23 22:07:25 -04:00
assert html =~ "All"
2023-03-30 20:43:30 -04:00
assert html =~ rifle_shot_record.notes
assert html =~ shotgun_shot_record.notes
assert html =~ pistol_shot_record.notes
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: :rifle})
2023-03-23 22:07:25 -04:00
2023-03-30 20:43:30 -04:00
assert html =~ rifle_shot_record.notes
refute html =~ shotgun_shot_record.notes
refute html =~ pistol_shot_record.notes
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-23 22:07:25 -04:00
2023-03-30 20:43:30 -04:00
refute html =~ rifle_shot_record.notes
assert html =~ shotgun_shot_record.notes
refute html =~ pistol_shot_record.notes
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-23 22:07:25 -04:00
2023-03-30 20:43:30 -04:00
refute html =~ rifle_shot_record.notes
refute html =~ shotgun_shot_record.notes
assert html =~ pistol_shot_record.notes
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: :all})
2023-03-23 22:07:25 -04:00
2023-03-30 20:43:30 -04:00
assert html =~ rifle_shot_record.notes
assert html =~ shotgun_shot_record.notes
assert html =~ pistol_shot_record.notes
2023-03-23 22:07:25 -04:00
end
2023-03-30 20:43:30 -04:00
test "can search for shot_record", %{conn: conn, shot_record: shot_record} do
2023-04-14 23:34:11 -04:00
{:ok, index_live, html} = live(conn, ~p"/range")
2023-03-30 20:43:30 -04:00
assert html =~ shot_record.notes
2022-12-03 21:27:39 -05:00
assert index_live
2023-03-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
2023-03-30 20:43:30 -04:00
|> render_change(search: %{search_term: shot_record.notes}) =~ shot_record.notes
2022-12-03 21:27:39 -05:00
2023-04-14 23:34:11 -04:00
assert_patch(index_live, ~p"/range/search/#{shot_record.notes}")
2022-12-03 21:27:39 -05:00
refute index_live
2023-03-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
2023-03-30 20:43:30 -04:00
|> render_change(search: %{search_term: "something_else"}) =~ shot_record.notes
2022-12-03 21:27:39 -05:00
2023-04-14 23:34:11 -04:00
assert_patch(index_live, ~p"/range/search/something_else")
2022-12-03 21:27:39 -05:00
assert index_live
2023-03-28 21:57:29 -04:00
|> form(~s/form[phx-change="search"]/)
2023-03-30 20:43:30 -04:00
|> render_change(search: %{search_term: ""}) =~ shot_record.notes
2022-12-03 21:27:39 -05:00
2023-04-14 23:34:11 -04:00
assert_patch(index_live, ~p"/range")
2022-12-03 21:27:39 -05:00
end
2023-03-30 20:43:30 -04:00
test "saves new shot_record", %{conn: conn, pack: pack} do
2023-04-14 23:34:11 -04:00
{:ok, index_live, _html} = live(conn, ~p"/range")
2023-03-28 21:57:29 -04:00
assert index_live |> element("a", "Record shots") |> render_click() =~ "Record shots"
2023-04-14 23:34:11 -04:00
assert_patch(index_live, ~p"/range/add_shot_record/#{pack}")
2022-02-16 23:30:12 -05:00
2023-03-28 21:57:29 -04:00
assert index_live
2023-03-30 21:38:56 -04:00
|> form("#shot-record-form")
2023-03-30 20:43:30 -04:00
|> render_change(shot_record: @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-30 21:38:56 -04:00
|> form("#shot-record-form")
2023-03-30 20:43:30 -04:00
|> render_submit(shot_record: @create_attrs)
2023-04-14 23:34:11 -04:00
|> follow_redirect(conn, ~p"/range")
2022-02-16 23:30:12 -05:00
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
2023-03-30 20:43:30 -04:00
test "updates shot_record in listing", %{conn: conn, shot_record: shot_record} do
2023-04-14 23:34:11 -04:00
{:ok, index_live, _html} = live(conn, ~p"/range")
2022-02-16 23:30:12 -05:00
2023-03-15 00:45:08 -04:00
assert index_live
2023-03-30 20:43:30 -04:00
|> element(~s/a[aria-label="Edit shot record of #{shot_record.count} shots"]/)
2023-03-30 21:38:56 -04:00
|> render_click() =~ "Edit Shot Record"
2022-02-16 23:30:12 -05:00
2023-04-14 23:34:11 -04:00
assert_patch(index_live, ~p"/range/edit/#{shot_record}")
2022-02-16 23:30:12 -05:00
2023-03-28 21:57:29 -04:00
assert index_live
2023-03-30 21:38:56 -04:00
|> form("#shot-record-form")
2023-03-30 20:43:30 -04:00
|> render_change(shot_record: @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-30 21:38:56 -04:00
|> form("#shot-record-form", shot_record: @update_attrs)
2022-02-16 23:30:12 -05:00
|> render_submit()
2023-04-14 23:34:11 -04:00
|> follow_redirect(conn, ~p"/range")
2022-02-16 23:30:12 -05:00
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
2023-03-30 20:43:30 -04:00
test "deletes shot_record in listing", %{conn: conn, shot_record: shot_record} do
2023-04-14 23:34:11 -04:00
{:ok, index_live, _html} = live(conn, ~p"/range")
2022-02-16 23:30:12 -05:00
2023-03-15 00:45:08 -04:00
assert index_live
2023-03-30 20:43:30 -04:00
|> element(~s/a[aria-label="Delete shot record of #{shot_record.count} shots"]/)
2023-03-15 00:45:08 -04:00
|> render_click()
2023-03-30 20:43:30 -04:00
refute has_element?(index_live, "#shot_record-#{shot_record.id}")
2022-02-16 23:30:12 -05:00
end
end
end