From ca4b5b9822140c750bec28e3e751ac30a29021fa Mon Sep 17 00:00:00 2001 From: shibao Date: Tue, 8 Nov 2022 21:35:17 -0500 Subject: [PATCH] add test to ammo group live for empty ammo groups --- .../cannery_web/live/ammo_group_live_test.exs | 114 +++++++++++++----- 1 file changed, 87 insertions(+), 27 deletions(-) diff --git a/test/cannery_web/live/ammo_group_live_test.exs b/test/cannery_web/live/ammo_group_live_test.exs index 2a53b2f0..bb7153bd 100644 --- a/test/cannery_web/live/ammo_group_live_test.exs +++ b/test/cannery_web/live/ammo_group_live_test.exs @@ -10,10 +10,23 @@ defmodule CanneryWeb.AmmoGroupLiveTest do @moduletag :ammo_group_live_test @shot_group_create_attrs %{"ammo_left" => 5, "notes" => "some notes"} - @shot_group_update_attrs %{"count" => 5, "notes" => "some updated notes"} + @shot_group_update_attrs %{ + "count" => 5, + "date" => ~N[2022-02-13 03:17:00], + "notes" => "some updated notes" + } @create_attrs %{"count" => 42, "notes" => "some notes", "price_paid" => 120.5} @update_attrs %{"count" => 43, "notes" => "some updated notes", "price_paid" => 456.7} @ammo_group_create_limit 10_000 + @ammo_group_attrs %{ + "price_paid" => 50, + "count" => 20 + } + @shot_group_attrs %{ + "price_paid" => 50, + "count" => 20 + } + # @invalid_attrs %{count: -1, notes: nil, price_paid: nil} defp create_ammo_group(%{current_user: current_user}) do @@ -21,14 +34,28 @@ defmodule CanneryWeb.AmmoGroupLiveTest do container = container_fixture(current_user) {1, [ammo_group]} = ammo_group_fixture(ammo_type, container, current_user) - shot_group = - %{"count" => 5, "date" => ~N[2022-02-13 03:17:00], "notes" => "some notes"} - |> shot_group_fixture(current_user, ammo_group) + %{ammo_type: ammo_type, ammo_group: ammo_group, container: container} + end + + defp create_shot_group(%{current_user: current_user, ammo_group: ammo_group}) do + shot_group = shot_group_fixture(@shot_group_update_attrs, current_user, ammo_group) + ammo_group = ammo_group |> Repo.reload!() %{ammo_group: ammo_group, shot_group: shot_group} end - describe "Index" do + defp create_empty_ammo_group(%{ + current_user: current_user, + ammo_type: ammo_type, + container: container + }) do + {1, [ammo_group]} = ammo_group_fixture(@ammo_group_attrs, ammo_type, container, current_user) + shot_group = shot_group_fixture(@shot_group_attrs, current_user, ammo_group) + ammo_group = ammo_group |> Repo.reload!() + %{empty_ammo_group: ammo_group, shot_group: shot_group} + end + + describe "Index of ammo group" do setup [:register_and_log_in_user, :create_ammo_group] test "lists all ammo_groups", %{conn: conn, ammo_group: ammo_group} do @@ -123,27 +150,6 @@ defmodule CanneryWeb.AmmoGroupLiveTest do ) end - test "saves new shot_group", %{conn: conn, ammo_group: ammo_group} do - {:ok, index_live, _html} = live(conn, Routes.ammo_group_index_path(conn, :index)) - - assert index_live |> element("a", dgettext("actions", "Record shots")) |> render_click() =~ - gettext("Record shots") - - assert_patch(index_live, Routes.ammo_group_index_path(conn, :add_shot_group, ammo_group)) - - # assert index_live - # |> form("#shot_group-form", shot_group: @invalid_attrs) - # |> render_change() =~ dgettext("errors", "is invalid") - - {:ok, _view, html} = - index_live - |> form("#shot-group-form", shot_group: @shot_group_create_attrs) - |> render_submit() - |> follow_redirect(conn, Routes.ammo_group_index_path(conn, :index)) - - assert html =~ dgettext("prompts", "Shots recorded successfully") - end - test "updates ammo_group in listing", %{conn: conn, ammo_group: ammo_group} do {:ok, index_live, _html} = live(conn, Routes.ammo_group_index_path(conn, :index)) @@ -177,9 +183,59 @@ defmodule CanneryWeb.AmmoGroupLiveTest do refute has_element?(index_live, "#ammo_group-#{ammo_group.id}") end + + test "saves new shot_group", %{conn: conn, ammo_group: ammo_group} do + {:ok, index_live, _html} = live(conn, Routes.ammo_group_index_path(conn, :index)) + + assert index_live |> element("a", dgettext("actions", "Record shots")) |> render_click() =~ + gettext("Record shots") + + assert_patch(index_live, Routes.ammo_group_index_path(conn, :add_shot_group, ammo_group)) + + # assert index_live + # |> form("#shot_group-form", shot_group: @invalid_attrs) + # |> render_change() =~ dgettext("errors", "is invalid") + + {:ok, _view, html} = + index_live + |> form("#shot-group-form", shot_group: @shot_group_create_attrs) + |> render_submit() + |> follow_redirect(conn, Routes.ammo_group_index_path(conn, :index)) + + assert html =~ dgettext("prompts", "Shots recorded successfully") + end end - describe "Show" do + describe "Index of empty ammo group" do + setup [:register_and_log_in_user, :create_ammo_group, :create_empty_ammo_group] + + test "hides empty ammo groups by default", %{conn: conn, empty_ammo_group: ammo_group} do + {:ok, show_live, html} = live(conn, Routes.ammo_group_index_path(conn, :index)) + + assert html =~ gettext("Show used") + refute html =~ gettext("$%{amount}", amount: 50.00 |> :erlang.float_to_binary(decimals: 2)) + + refute html =~ + "\n" <> + gettext("%{percentage}%", + percentage: ammo_group |> Ammo.get_percentage_remaining() + ) <> + "\n" + + html = show_live |> element("[data-qa=\"toggle_show_used\"]") |> render_click() + + assert html =~ gettext("$%{amount}", amount: 50.00 |> :erlang.float_to_binary(decimals: 2)) + + assert html =~ + "\n" <> + gettext("%{percentage}%", + percentage: ammo_group |> Ammo.get_percentage_remaining() + ) <> + "\n" + end + end + + describe "Show ammo group" do setup [:register_and_log_in_user, :create_ammo_group] test "displays ammo_group", %{conn: conn, ammo_group: ammo_group} do @@ -234,6 +290,10 @@ defmodule CanneryWeb.AmmoGroupLiveTest do assert html =~ dgettext("prompts", "Shots recorded successfully") end + end + + describe "Show ammo group with shot group" do + setup [:register_and_log_in_user, :create_ammo_group, :create_shot_group] test "updates shot_group in listing", %{conn: conn, ammo_group: ammo_group, shot_group: shot_group} do