[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
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user