pinchflat/test/pinchflat_web/helpers/pagination_helpers_test.exs
Kieran 53e106dac2
[Enhancement] Add sorting, pagination, and new attributes to sources index table (#510)
* WIP - started improving handling of sorting for sources index table

* WIP - Added UI to table to indicate sort column and direction

* Refactored toggle liveview into a livecomponent

* Added sorting for all table attrs

* Added pagination to the sources table

* Added tests for updated liveviews and live components

* Add tests for new helper methods

* Added fancy new CSS to my sources table

* Added size to sources table

* Adds relative div to ensure that sorting arrow doesn't run away

* Fixed da tests
2024-12-13 09:49:00 -08:00

96 lines
2.8 KiB
Elixir

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