From 0ce84b3791814f6fed021ec14f3171720b4644ad Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Thu, 12 Dec 2024 16:07:23 -0800 Subject: [PATCH] Add tests for new helper methods --- .../helpers/pagination_helpers.ex | 19 +++- lib/pinchflat_web/helpers/sorting_helpers.ex | 12 ++- .../sources/source_enable_toggle_test.exs | 4 +- .../helpers/pagination_helpers_test.exs | 96 +++++++++++++++++++ .../helpers/sorting_helpers_test.exs | 31 ++++++ 5 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 test/pinchflat_web/helpers/pagination_helpers_test.exs create mode 100644 test/pinchflat_web/helpers/sorting_helpers_test.exs diff --git a/lib/pinchflat_web/helpers/pagination_helpers.ex b/lib/pinchflat_web/helpers/pagination_helpers.ex index 1d2e690..50c3928 100644 --- a/lib/pinchflat_web/helpers/pagination_helpers.ex +++ b/lib/pinchflat_web/helpers/pagination_helpers.ex @@ -1,8 +1,18 @@ defmodule PinchflatWeb.Helpers.PaginationHelpers do + @moduledoc """ + Methods for working with pagination, usually in the context of LiveViews or LiveComponents. + + These methods are fairly simple, but they're commonly repeated across different Live entities + """ + alias Pinchflat.Repo alias Pinchflat.Utils.NumberUtils - # TODO: test + @doc """ + Given a query, a page number, and a number of records per page, returns a map of pagination attributes. + + Returns map() + """ def get_pagination_attributes(query, page, records_per_page) do total_record_count = Repo.aggregate(query, :count, :id) total_pages = max(ceil(total_record_count / records_per_page), 1) @@ -17,7 +27,12 @@ defmodule PinchflatWeb.Helpers.PaginationHelpers do } end - # TODO: test + @doc """ + Given a current page number, a direction to move in, and the total number of pages, returns the updated page number. + The updated page number is clamped to the range [1, total_pages]. + + Returns integer() + """ def update_page_number(current_page, direction, total_pages) do updated_page = case to_string(direction) do diff --git a/lib/pinchflat_web/helpers/sorting_helpers.ex b/lib/pinchflat_web/helpers/sorting_helpers.ex index 6805424..fb65e62 100644 --- a/lib/pinchflat_web/helpers/sorting_helpers.ex +++ b/lib/pinchflat_web/helpers/sorting_helpers.ex @@ -1,5 +1,15 @@ defmodule PinchflatWeb.Helpers.SortingHelpers do - # TODO: test + @moduledoc """ + Methods for working with sorting, usually in the context of LiveViews or LiveComponents. + + These methods are fairly simple, but they're commonly repeated across different Live entities + """ + + @doc """ + Given the old sort attribute, the new sort attribute, and the old sort direction, returns the new sort direction. + + Returns :asc | :desc + """ def get_sort_direction(old_sort_attr, new_sort_attr, old_sort_direction) do case {new_sort_attr, old_sort_direction} do {^old_sort_attr, :desc} -> :asc diff --git a/test/pinchflat_web/controllers/sources/source_enable_toggle_test.exs b/test/pinchflat_web/controllers/sources/source_enable_toggle_test.exs index 4fff67c..e1abd1d 100644 --- a/test/pinchflat_web/controllers/sources/source_enable_toggle_test.exs +++ b/test/pinchflat_web/controllers/sources/source_enable_toggle_test.exs @@ -7,7 +7,7 @@ defmodule PinchflatWeb.Sources.SourceLive.SourceEnableToggleTest do describe "initial rendering" do test "renders a toggle in the on position if the source is enabled" do - source = %{ id: 1, enabled: true } + source = %{id: 1, enabled: true} html = render_component(SourceEnableToggle, %{id: :foo, source: source}) @@ -16,7 +16,7 @@ defmodule PinchflatWeb.Sources.SourceLive.SourceEnableToggleTest do end test "renders a toggle in the off position if the source is disabled" do - source = %{ id: 1, enabled: false } + source = %{id: 1, enabled: false} html = render_component(SourceEnableToggle, %{id: :foo, source: source}) diff --git a/test/pinchflat_web/helpers/pagination_helpers_test.exs b/test/pinchflat_web/helpers/pagination_helpers_test.exs new file mode 100644 index 0000000..c15a2d3 --- /dev/null +++ b/test/pinchflat_web/helpers/pagination_helpers_test.exs @@ -0,0 +1,96 @@ +defmodule PinchflatWeb.Helpers.PaginationHelpersTest do + use Pinchflat.DataCase + import Pinchflat.SourcesFixtures + + alias Pinchflat.Sources.Source + alias PinchflatWeb.Helpers.PaginationHelpers + + describe "get_pagination_attributes/3" do + test "returns the correct pagination attributes" do + source_fixture() + query = from(s in Source, select: s.id) + page = 1 + records_per_page = 10 + + pagination_attributes = PaginationHelpers.get_pagination_attributes(query, page, records_per_page) + + assert pagination_attributes.page == 1 + assert pagination_attributes.total_pages == 1 + assert pagination_attributes.total_record_count == 1 + assert pagination_attributes.limit == 10 + assert pagination_attributes.offset == 0 + end + + test "returns the correct pagination attributes when there are multiple pages" do + source_fixture() + source_fixture() + + query = from(s in Source, select: s.id) + page = 1 + records_per_page = 1 + + pagination_attributes = PaginationHelpers.get_pagination_attributes(query, page, records_per_page) + + assert pagination_attributes.page == 1 + assert pagination_attributes.total_pages == 2 + assert pagination_attributes.total_record_count == 2 + assert pagination_attributes.limit == 1 + assert pagination_attributes.offset == 0 + end + + test "returns the correct attributes when on a page other than the first" do + source_fixture() + source_fixture() + + query = from(s in Source, select: s.id) + page = 2 + records_per_page = 1 + + pagination_attributes = PaginationHelpers.get_pagination_attributes(query, page, records_per_page) + + assert pagination_attributes.page == 2 + assert pagination_attributes.total_pages == 2 + assert pagination_attributes.total_record_count == 2 + assert pagination_attributes.limit == 1 + assert pagination_attributes.offset == 1 + end + end + + describe "update_page_number/3" do + test "increments the page number" do + current_page = 1 + total_pages = 2 + + updated_page = PaginationHelpers.update_page_number(current_page, :inc, total_pages) + + assert updated_page == 2 + end + + test "decrements the page number" do + current_page = 2 + total_pages = 2 + + updated_page = PaginationHelpers.update_page_number(current_page, :dec, total_pages) + + assert updated_page == 1 + end + + test "doesn't overflow the page number" do + current_page = 2 + total_pages = 2 + + updated_page = PaginationHelpers.update_page_number(current_page, :inc, total_pages) + + assert updated_page == 2 + end + + test "doesn't underflow the page number" do + current_page = 1 + total_pages = 2 + + updated_page = PaginationHelpers.update_page_number(current_page, :dec, total_pages) + + assert updated_page == 1 + end + end +end diff --git a/test/pinchflat_web/helpers/sorting_helpers_test.exs b/test/pinchflat_web/helpers/sorting_helpers_test.exs new file mode 100644 index 0000000..7f1d81b --- /dev/null +++ b/test/pinchflat_web/helpers/sorting_helpers_test.exs @@ -0,0 +1,31 @@ +defmodule PinchflatWeb.Helpers.SortingHelpersTest do + use Pinchflat.DataCase + + alias PinchflatWeb.Helpers.SortingHelpers + + describe "get_sort_direction/3" do + test "returns the correct sort direction when the new sort attribute is the same as the old sort attribute" do + old_sort_attr = "name" + new_sort_attr = "name" + old_sort_direction = :desc + + assert SortingHelpers.get_sort_direction(old_sort_attr, new_sort_attr, old_sort_direction) == :asc + end + + test "returns the correct sort direction when the new sort attribute is the same as the old sort attribute in the other direction" do + old_sort_attr = "name" + new_sort_attr = "name" + old_sort_direction = :asc + + assert SortingHelpers.get_sort_direction(old_sort_attr, new_sort_attr, old_sort_direction) == :desc + end + + test "returns the correct sort direction when the new sort attribute is different from the old sort attribute" do + old_sort_attr = "name" + new_sort_attr = "date" + old_sort_direction = :asc + + assert SortingHelpers.get_sort_direction(old_sort_attr, new_sort_attr, old_sort_direction) == :asc + end + end +end