feat: add product sorting to collection pages with tests
Add sort functionality to /collections/:slug pages with 6 sort options (featured, newest, price ascending/descending, name A-Z/Z-A). Sort selection persists across category navigation via URL query params. Refactor handle_params to be DRY using load_collection/1 helper. Add comprehensive unit tests for PreviewData category functions and LiveView tests for the Collection page sorting and navigation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -198,4 +198,81 @@ defmodule SimpleshopTheme.Theme.PreviewDataTest do
|
||||
assert PreviewData.has_real_products?() == false
|
||||
end
|
||||
end
|
||||
|
||||
describe "category_by_slug/1" do
|
||||
test "returns category when slug exists" do
|
||||
category = PreviewData.category_by_slug("art-prints")
|
||||
|
||||
assert category != nil
|
||||
assert category.slug == "art-prints"
|
||||
assert is_binary(category.name)
|
||||
end
|
||||
|
||||
test "returns nil when slug does not exist" do
|
||||
assert PreviewData.category_by_slug("nonexistent") == nil
|
||||
end
|
||||
|
||||
test "finds all known categories by slug" do
|
||||
categories = PreviewData.categories()
|
||||
|
||||
for category <- categories do
|
||||
found = PreviewData.category_by_slug(category.slug)
|
||||
assert found != nil
|
||||
assert found.id == category.id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "products_by_category/1" do
|
||||
test "returns all products for nil" do
|
||||
all_products = PreviewData.products()
|
||||
filtered = PreviewData.products_by_category(nil)
|
||||
|
||||
assert filtered == all_products
|
||||
end
|
||||
|
||||
test "returns all products for 'all'" do
|
||||
all_products = PreviewData.products()
|
||||
filtered = PreviewData.products_by_category("all")
|
||||
|
||||
assert filtered == all_products
|
||||
end
|
||||
|
||||
test "returns empty list for nonexistent category" do
|
||||
assert PreviewData.products_by_category("nonexistent") == []
|
||||
end
|
||||
|
||||
test "returns only products matching the category" do
|
||||
category = List.first(PreviewData.categories())
|
||||
products = PreviewData.products_by_category(category.slug)
|
||||
|
||||
assert is_list(products)
|
||||
|
||||
for product <- products do
|
||||
assert product.category == category.name
|
||||
end
|
||||
end
|
||||
|
||||
test "products are filtered correctly for each category" do
|
||||
categories = PreviewData.categories()
|
||||
|
||||
for category <- categories do
|
||||
products = PreviewData.products_by_category(category.slug)
|
||||
|
||||
for product <- products do
|
||||
assert product.category == category.name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "all products belong to at least one category" do
|
||||
all_products = PreviewData.products()
|
||||
categories = PreviewData.categories()
|
||||
category_names = Enum.map(categories, & &1.name)
|
||||
|
||||
for product <- all_products do
|
||||
assert product.category in category_names
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user