2026-02-18 21:23:15 +00:00
|
|
|
defmodule BerrypodWeb.Shop.CollectionTest do
|
|
|
|
|
use BerrypodWeb.ConnCase, async: false
|
2026-01-19 23:38:22 +00:00
|
|
|
|
|
|
|
|
import Phoenix.LiveViewTest
|
2026-02-18 21:23:15 +00:00
|
|
|
import Berrypod.AccountsFixtures
|
|
|
|
|
import Berrypod.ProductsFixtures
|
2026-01-19 23:38:22 +00:00
|
|
|
|
2026-02-18 21:23:15 +00:00
|
|
|
alias Berrypod.Products
|
2026-01-19 23:38:22 +00:00
|
|
|
|
2026-02-12 14:40:58 +00:00
|
|
|
setup do
|
|
|
|
|
user_fixture()
|
2026-02-18 21:23:15 +00:00
|
|
|
{:ok, _} = Berrypod.Settings.set_site_live(true)
|
2026-02-13 08:27:26 +00:00
|
|
|
|
|
|
|
|
pc = provider_connection_fixture()
|
|
|
|
|
|
|
|
|
|
print =
|
|
|
|
|
product_fixture(%{
|
|
|
|
|
provider_connection: pc,
|
|
|
|
|
title: "Mountain Sunrise Print",
|
|
|
|
|
category: "Art Prints"
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
product_variant_fixture(%{product: print, title: "8x10", price: 1999})
|
|
|
|
|
Products.recompute_cached_fields(print)
|
|
|
|
|
|
|
|
|
|
shirt =
|
|
|
|
|
product_fixture(%{
|
|
|
|
|
provider_connection: pc,
|
|
|
|
|
title: "Forest T-Shirt",
|
|
|
|
|
category: "Apparel",
|
|
|
|
|
on_sale: true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
product_variant_fixture(%{
|
|
|
|
|
product: shirt,
|
|
|
|
|
title: "Large",
|
|
|
|
|
price: 2999,
|
|
|
|
|
compare_at_price: 3999
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Products.recompute_cached_fields(shirt)
|
|
|
|
|
|
|
|
|
|
%{print: print, shirt: shirt}
|
2026-02-12 14:40:58 +00:00
|
|
|
end
|
|
|
|
|
|
2026-01-19 23:38:22 +00:00
|
|
|
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
|
2026-02-13 08:27:26 +00:00
|
|
|
{:ok, _view, html} = live(conn, ~p"/collections/art-prints")
|
2026-01-19 23:38:22 +00:00
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
assert html =~ "Art Prints"
|
2026-01-19 23:38:22 +00:00
|
|
|
end
|
|
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
test "displays products", %{conn: conn, print: print} do
|
2026-01-19 23:38:22 +00:00
|
|
|
{:ok, _view, html} = live(conn, ~p"/collections/all")
|
|
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
assert html =~ print.title
|
2026-01-19 23:38:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "displays category filter buttons", %{conn: conn} do
|
|
|
|
|
{:ok, _view, html} = live(conn, ~p"/collections/all")
|
|
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
assert html =~ "Art Prints"
|
|
|
|
|
assert html =~ "Apparel"
|
2026-01-19 23:38:22 +00:00
|
|
|
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
|
|
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
test "filters products by category", %{conn: conn, print: print, shirt: shirt} do
|
|
|
|
|
{:ok, _view, html} = live(conn, ~p"/collections/art-prints")
|
2026-01-19 23:38:22 +00:00
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
assert html =~ print.title
|
|
|
|
|
refute html =~ shirt.title
|
2026-01-19 23:38:22 +00:00
|
|
|
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")
|
|
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
assert html =~ "/collections/art-prints?sort=price_desc"
|
2026-01-19 23:38:22 +00:00
|
|
|
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")
|
|
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
assert html =~ ~s(href="/collections/art-prints")
|
|
|
|
|
refute html =~ "/collections/art-prints?sort=featured"
|
2026-01-19 23:38:22 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|