prop/test/microwaveprop_web/live/rover_locations_live_test.exs
Graham McIntire d44351366a
feat(rover): /rover-locations/:id detail page with map + marker
Each row now has a View link (and the location cell links too) to a
new show LiveView that renders the full record alongside a Leaflet
map centered on the marker. Owners get a Delete button there as well.
2026-05-02 17:02:00 -05:00

158 lines
4.8 KiB
Elixir

defmodule MicrowavepropWeb.RoverLocationsLiveTest do
use MicrowavepropWeb.ConnCase, async: true
import Phoenix.LiveViewTest
alias Microwaveprop.Rover
describe "anonymous visitor" do
test "can view the locations page", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/rover-locations")
assert html =~ "Rover Locations"
assert html =~ "Sign in"
end
test "shows existing locations from any user", %{conn: conn} do
user = Microwaveprop.AccountsFixtures.user_fixture()
{:ok, _} =
Rover.create_location(user, %{
lat: 32.5,
lon: -97.5,
status: :ideal,
notes: "great hill"
})
{:ok, _lv, html} = live(conn, ~p"/rover-locations")
assert html =~ "Ideal"
assert html =~ "great hill"
end
test "Add button is disabled for anonymous users", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/rover-locations")
assert html =~ ~s(disabled)
assert html =~ "Add location"
end
test "status filter narrows the visible rows", %{conn: conn} do
user = Microwaveprop.AccountsFixtures.user_fixture()
{:ok, _} = Rover.create_location(user, %{lat: 32.0, lon: -97.0, status: :ideal, notes: "ideal-spot"})
{:ok, _} = Rover.create_location(user, %{lat: 33.0, lon: -98.0, status: :off_limits, notes: "no-go-spot"})
{:ok, lv, html} = live(conn, ~p"/rover-locations")
assert html =~ "ideal-spot"
assert html =~ "no-go-spot"
html =
lv
|> form("form[phx-change=filter_status]", %{"value" => "ideal"})
|> render_change()
assert html =~ "ideal-spot"
refute html =~ "no-go-spot"
html =
lv
|> form("form[phx-change=filter_status]", %{"value" => "off_limits"})
|> render_change()
refute html =~ "ideal-spot"
assert html =~ "no-go-spot"
end
end
describe "logged-in user" do
setup :register_and_log_in_user
test "can add a new location", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/rover-locations")
lv |> element("button", "Add location") |> render_click()
lv
|> form("#location-form",
location: %{lat: "32.5", lon: "-97.5", status: "ideal", notes: "test note"}
)
|> render_submit()
html = render(lv)
assert html =~ "test note"
end
test "typing a grid populates lat/lon", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/rover-locations")
lv |> element("button", "Add location") |> render_click()
html =
lv
|> form("#location-form",
location: %{
grid: "EM12kp",
lat: "",
lon: "",
status: "ideal",
notes: ""
}
)
|> render_change(%{"_target" => ["location", "grid"]})
# EM12kp center: lat 32.645833, lon -97.125
assert html =~ ~s(value="32.645833")
assert html =~ ~s(value="-97.125")
end
test "typing lat/lon populates the grid field", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/rover-locations")
lv |> element("button", "Add location") |> render_click()
html =
lv
|> form("#location-form",
location: %{
grid: "",
lat: "32.5",
lon: "-97.5",
status: "ideal",
notes: ""
}
)
|> render_change(%{"_target" => ["location", "lat"]})
assert html =~ ~s(value="EM12gm")
end
test "View link on each row navigates to the show page", %{conn: conn, user: user} do
{:ok, loc} =
Rover.create_location(user, %{
lat: 32.5,
lon: -97.5,
status: :ideal,
notes: "rooftop spot"
})
{:ok, lv, _html} = live(conn, ~p"/rover-locations")
assert {:ok, _show_lv, html} =
lv
|> element("a[href='/rover-locations/#{loc.id}']", "View")
|> render_click()
|> follow_redirect(conn, ~p"/rover-locations/#{loc.id}")
assert html =~ "rooftop spot"
assert html =~ "EM12gm"
end
test "edit/delete buttons appear only on the owner's row", %{conn: conn, user: user} do
other = Microwaveprop.AccountsFixtures.user_fixture()
{:ok, mine} = Rover.create_location(user, %{lat: 32.0, lon: -97.0, status: :ideal})
{:ok, theirs} = Rover.create_location(other, %{lat: 33.0, lon: -98.0, status: :off_limits})
{:ok, lv, _html} = live(conn, ~p"/rover-locations")
assert has_element?(lv, "button[phx-click=edit][phx-value-id='#{mine.id}']", "Edit")
assert has_element?(lv, "button[phx-click=delete][phx-value-id='#{mine.id}']", "Delete")
refute has_element?(lv, "button[phx-click=edit][phx-value-id='#{theirs.id}']", "Edit")
refute has_element?(lv, "button[phx-click=delete][phx-value-id='#{theirs.id}']", "Delete")
end
end
end