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:
2026-01-19 23:38:22 +00:00
parent 9fb836ca0d
commit fe29c1ad36
3 changed files with 329 additions and 47 deletions

View File

@@ -0,0 +1,154 @@
defmodule SimpleshopThemeWeb.ShopLive.CollectionTest do
use SimpleshopThemeWeb.ConnCase, async: false
import Phoenix.LiveViewTest
alias SimpleshopTheme.Theme.PreviewData
describe "Collection page" do
test "renders collection page for /collections/all", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/collections/all")
assert html =~ "All Products"
end
test "renders collection page for specific category", %{conn: conn} do
category = List.first(PreviewData.categories())
{:ok, _view, html} = live(conn, ~p"/collections/#{category.slug}")
assert html =~ category.name
end
test "displays products", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/collections/all")
products = PreviewData.products()
first_product = List.first(products)
assert html =~ first_product.name
end
test "displays category filter buttons", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/collections/all")
categories = PreviewData.categories()
for category <- categories do
assert html =~ category.name
end
end
test "displays sort dropdown", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/collections/all")
assert html =~ "Featured"
assert html =~ "Newest"
assert html =~ "Price: Low to High"
assert html =~ "Price: High to Low"
assert html =~ "Name: A-Z"
assert html =~ "Name: Z-A"
end
test "redirects to /collections/all for unknown category", %{conn: conn} do
{:error, {:live_redirect, %{to: to, flash: flash}}} =
live(conn, ~p"/collections/nonexistent")
assert to == "/collections/all"
assert flash["error"] == "Collection not found"
end
test "filters products by category", %{conn: conn} do
category = List.first(PreviewData.categories())
{:ok, _view, html} = live(conn, ~p"/collections/#{category.slug}")
products = PreviewData.products_by_category(category.slug)
for product <- products do
assert html =~ product.name
end
end
end
describe "Sorting" do
test "sorts by price ascending", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collections/all")
html =
view
|> element("form[phx-change='sort_changed']")
|> render_change(%{sort: "price_asc"})
assert html =~ "Price: Low to High"
end
test "sorts by price descending", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collections/all")
html =
view
|> element("form[phx-change='sort_changed']")
|> render_change(%{sort: "price_desc"})
assert html =~ "Price: High to Low"
end
test "sorts by name ascending", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collections/all")
html =
view
|> element("form[phx-change='sort_changed']")
|> render_change(%{sort: "name_asc"})
assert html =~ "Name: A-Z"
end
test "sorts by name descending", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collections/all")
html =
view
|> element("form[phx-change='sort_changed']")
|> render_change(%{sort: "name_desc"})
assert html =~ "Name: Z-A"
end
test "sort parameter is preserved in URL", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/collections/all?sort=price_asc")
html = render(view)
assert html =~ "selected"
end
test "default sort is featured", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/collections/all")
assert html =~ ~r/<option[^>]*value="featured"[^>]*selected/
end
end
describe "Navigation" do
test "category links preserve sort order", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/collections/all?sort=price_desc")
category = List.first(PreviewData.categories())
assert html =~ "/collections/#{category.slug}?sort=price_desc"
end
test "All button link preserves sort order", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/collections/art-prints?sort=name_asc")
assert html =~ "/collections/all?sort=name_asc"
end
test "featured sort does not include query param in links", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/collections/all")
category = List.first(PreviewData.categories())
assert html =~ ~s(href="/collections/#{category.slug}")
refute html =~ "/collections/#{category.slug}?sort=featured"
end
end
end