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 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 "can edit and delete only their own location", %{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 html =~ "location-#{mine.id}" assert html =~ "location-#{theirs.id}" # Edit/Delete buttons rendered for owned location only. assert has_element?(lv, "#location-#{mine.id} button", "Edit") refute has_element?(lv, "#location-#{theirs.id} button", "Edit") end end end