defmodule MicrowavepropWeb.RoverLocationsLive.MapTest do
  use MicrowavepropWeb.ConnCase, async: true

  import Phoenix.LiveViewTest

  alias Microwaveprop.AccountsFixtures
  alias Microwaveprop.Rover

  test "renders the map shell with a count + JS hook container", %{conn: conn} do
    user = AccountsFixtures.user_fixture()

    {:ok, _} = Rover.create_location(user, %{lat: 32.0, lon: -97.0, status: :good})
    {:ok, _} = Rover.create_location(user, %{lat: 33.0, lon: -98.0, status: :good})
    # `bad` rows must NOT show up on the map.
    {:ok, _} = Rover.create_location(user, %{lat: 34.0, lon: -99.0, status: :bad})

    {:ok, lv, html} = live(conn, ~p"/rover-locations/map")

    assert html =~ "Rover Locations Map"
    assert html =~ "2 good locations"
    assert has_element?(lv, "#rover-locations-map[phx-hook=RoverLocationsMap]")
    # Marker payload only includes the two good rows.
    refute html =~ ~s("lat":34.0)
  end

  test "List link returns to /rover-locations", %{conn: conn} do
    {:ok, lv, _html} = live(conn, ~p"/rover-locations/map")
    assert has_element?(lv, "a[href='/rover-locations']", "List")
  end

  test "/rover-locations renders a Map link", %{conn: conn} do
    {:ok, lv, _html} = live(conn, ~p"/rover-locations")
    assert has_element?(lv, "a[href='/rover-locations/map']", "Map")
  end
end
