Compare commits

..

4 Commits

Author SHA1 Message Date
cb4de9a6ff add container live test for empty ammo groups
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-08 21:41:14 -05:00
ca4b5b9822 add test to ammo group live for empty ammo groups 2022-11-08 21:41:14 -05:00
13bf8ab2b1 fix gettext for percentages 2022-11-08 21:41:14 -05:00
ffd1029e78 add ammo type live test for empty ammo groups 2022-11-08 21:41:14 -05:00
13 changed files with 207 additions and 34 deletions

View File

@ -194,7 +194,7 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
end
defp get_value_for_key(:remaining, ammo_group),
do: "#{ammo_group |> Ammo.get_percentage_remaining()}%"
do: gettext("%{percentage}%", percentage: ammo_group |> Ammo.get_percentage_remaining())
defp get_value_for_key(:actions, ammo_group) do
assigns = %{ammo_group: ammo_group}

View File

@ -16,7 +16,7 @@
<span class="rounded-lg title text-lg">
<%= gettext("Percentage left:") %>
<%= "#{@ammo_group |> Ammo.get_percentage_remaining()}%" %>
<%= gettext("%{percentage}%", percentage: @ammo_group |> Ammo.get_percentage_remaining()) %>
</span>
<%= if @ammo_group.notes do %>

View File

@ -95,11 +95,12 @@ defmodule CanneryWeb.ViewHelpers do
~H"""
<label for={@id} class="inline-flex relative items-center cursor-pointer">
<input
id={@id}
type="checkbox"
value={@value}
checked={@value}
id={@id}
class="sr-only peer"
data-qa={@id}
{
if assigns |> Map.has_key?(:target),
do: %{"phx-click" => @action, "phx-value-value" => @value, "phx-target" => @target},

View File

@ -962,3 +962,9 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Used up on:"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/index.ex:197
#: lib/cannery_web/live/ammo_group_live/show.html.heex:19
#, elixir-autogen, elixir-format
msgid "%{percentage}%"
msgstr ""

View File

@ -945,3 +945,9 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Used up on:"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/index.ex:197
#: lib/cannery_web/live/ammo_group_live/show.html.heex:19
#, elixir-autogen, elixir-format
msgid "%{percentage}%"
msgstr ""

View File

@ -946,3 +946,9 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Used up on:"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/index.ex:197
#: lib/cannery_web/live/ammo_group_live/show.html.heex:19
#, elixir-autogen, elixir-format
msgid "%{percentage}%"
msgstr ""

View File

@ -960,3 +960,9 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Used up on:"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/index.ex:197
#: lib/cannery_web/live/ammo_group_live/show.html.heex:19
#, elixir-autogen, elixir-format
msgid "%{percentage}%"
msgstr ""

View File

@ -965,3 +965,9 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Used up on:"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/index.ex:197
#: lib/cannery_web/live/ammo_group_live/show.html.heex:19
#, elixir-autogen, elixir-format
msgid "%{percentage}%"
msgstr ""

View File

@ -956,3 +956,9 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Used up on:"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/index.ex:197
#: lib/cannery_web/live/ammo_group_live/show.html.heex:19
#, elixir-autogen, elixir-format
msgid "%{percentage}%"
msgstr ""

View File

@ -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

View File

@ -6,7 +6,7 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
use CanneryWeb.ConnCase
import Phoenix.LiveViewTest
import CanneryWeb.Gettext
alias Cannery.Ammo
alias Cannery.{Ammo, Repo}
@moduletag :ammo_type_live_test
@ -26,6 +26,14 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
"name" => "some updated name",
"grains" => 456
}
@ammo_group_attrs %{
"notes" => "some ammo group",
"count" => 20
}
@shot_group_attrs %{
"notes" => "some shot group",
"count" => 20
}
# @invalid_attrs %{
# "bullet_type" => nil,
@ -40,6 +48,15 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
%{ammo_type: ammo_type_fixture(@create_attrs, current_user)}
end
defp create_empty_ammo_group(%{ammo_type: ammo_type, current_user: current_user}) do
container = container_fixture(current_user)
{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!()
%{ammo_group: ammo_group, shot_group: shot_group}
end
describe "Index" do
setup [:register_and_log_in_user, :create_ammo_type]
@ -105,7 +122,7 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
end
end
describe "Show" do
describe "Show ammo type" do
setup [:register_and_log_in_user, :create_ammo_type]
test "displays ammo_type", %{conn: conn, ammo_type: ammo_type} do
@ -139,4 +156,21 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
assert html =~ "some updated bullet_type"
end
end
describe "Show ammo type with empty ammo group" do
setup [:register_and_log_in_user, :create_ammo_type, :create_empty_ammo_group]
test "hides empty ammo groups by default",
%{conn: conn, ammo_type: ammo_type} do
{:ok, show_live, html} = live(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
assert html =~ gettext("Show used")
refute html =~ "some ammo group"
html = show_live |> element("[data-qa=\"toggle_show_used\"]") |> render_click()
assert html =~ "some ammo group"
assert html =~ "Empty"
end
end
end

View File

@ -6,7 +6,7 @@ defmodule CanneryWeb.ContainerLiveTest do
use CanneryWeb.ConnCase
import Phoenix.LiveViewTest
import CanneryWeb.Gettext
alias Cannery.Containers
alias Cannery.{Containers, Repo}
@moduletag :container_live_test
@ -22,6 +22,22 @@ defmodule CanneryWeb.ContainerLiveTest do
"name" => "some updated name",
"type" => "some updated type"
}
@ammo_type_attrs %{
"bullet_type" => "some bullet_type",
"case_material" => "some case_material",
"desc" => "some desc",
"manufacturer" => "some manufacturer",
"name" => "some name",
"grains" => 120
}
@ammo_group_attrs %{
"notes" => "some ammo group",
"count" => 20
}
@shot_group_attrs %{
"notes" => "some shot group",
"count" => 20
}
# @invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
@ -30,6 +46,15 @@ defmodule CanneryWeb.ContainerLiveTest do
%{container: container}
end
defp create_empty_ammo_group(%{container: container, current_user: current_user}) do
ammo_type = ammo_type_fixture(@ammo_type_attrs, current_user)
{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!()
%{ammo_group: ammo_group, shot_group: shot_group}
end
describe "Index" do
setup [:register_and_log_in_user, :create_container]
@ -134,4 +159,21 @@ defmodule CanneryWeb.ContainerLiveTest do
assert html =~ "some updated location"
end
end
describe "Show with empty ammo group" do
setup [:register_and_log_in_user, :create_container, :create_empty_ammo_group]
test "hides empty ammo groups by default",
%{conn: conn, container: container} do
{:ok, show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
assert html =~ gettext("Show used")
refute html =~ "some ammo group"
html = show_live |> element("[data-qa=\"toggle_show_used\"]") |> render_click()
assert html =~ "some ammo group"
assert html =~ "Empty"
end
end
end

View File

@ -76,7 +76,7 @@ defmodule Cannery.Fixtures do
def shot_group_fixture(attrs \\ %{}, %User{} = user, %AmmoGroup{} = ammo_group) do
attrs
|> Enum.into(%{
"count" => 25,
"count" => 20,
"date" => ~N[2022-02-13 03:17:00],
"notes" => "some notes"
})