add no-JS fallback for cart country selector
All checks were successful
deploy / deploy (push) Successful in 59s

The delivery country form now has action="/cart/country" with a
noscript submit button. Without JS, changing the country and clicking
Update POSTs to a new CartController.update_country action that saves
the country to session and redirects back to /cart.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jamey
2026-02-24 23:14:48 +00:00
parent 79764c7766
commit d0ea9d59f5
5 changed files with 64 additions and 1 deletions

View File

@@ -108,4 +108,19 @@ defmodule BerrypodWeb.CartControllerTest do
assert cart == []
end
end
describe "POST /cart/country" do
test "saves country to session and redirects to /cart", %{conn: conn} do
conn = post(conn, ~p"/cart/country", %{"country" => "DE"})
assert redirected_to(conn) == "/cart"
assert get_session(conn, "country_code") == "DE"
end
test "handles missing country param", %{conn: conn} do
conn = post(conn, ~p"/cart/country", %{})
assert redirected_to(conn) == "/cart"
end
end
end

View File

@@ -106,6 +106,36 @@ defmodule BerrypodWeb.Shop.CartTest do
end
end
describe "no-JS fallback" do
setup [:create_cart_with_product]
test "country selector form has action for no-JS submission", %{
conn: conn,
variant: variant
} do
# Insert a shipping rate so the country selector renders
now = DateTime.utc_now() |> DateTime.truncate(:second)
pc = Berrypod.ProductsFixtures.provider_connection_fixture()
Berrypod.Repo.insert_all(Berrypod.Shipping.ShippingRate, [
[
blueprint_id: 1,
print_provider_id: 1,
country_code: "GB",
first_item_cost: 399,
additional_item_cost: 100,
provider_connection_id: pc.id,
inserted_at: now,
updated_at: now
]
])
{:ok, view, _html} = conn |> conn_with_cart(variant.id) |> live(~p"/cart")
assert has_element?(view, "form[action='/cart/country'][method='post']")
end
end
describe "Cart page title" do
test "page title is Cart", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/cart")