Add tests for new helper methods

This commit is contained in:
Kieran Eglin 2024-12-12 16:07:23 -08:00
parent 9a361fb5c1
commit 0ce84b3791
No known key found for this signature in database
GPG key ID: 193984967FCF432D
5 changed files with 157 additions and 5 deletions

View file

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

View file

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

View file

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

View file

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

View file

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