2.0 KiB · text History e569b6d
defmodule CartosnapAppWeb.HomeLiveTest do
  use CartosnapAppWeb.ConnCase, async: true

  import Phoenix.LiveViewTest
  import CartosnapApp.PhotosFixtures

  alias CartosnapApp.AccountsFixtures
  alias CartosnapApp.Trips

  setup do
    owner = AccountsFixtures.user_scope_fixture()
    trip = trip_fixture(owner, name: "Acadia")
    photo = photo_fixture(owner, lat: 44.35, lon: -68.21)
    {:ok, 1} = Trips.add_photos(owner, trip.id, [photo.id])
    %{owner: owner, trip: trip, photo: photo}
  end

  test "visitors get the intro and the public trips on a map", %{
    owner: owner,
    trip: trip,
    photo: photo
  } do
    {:ok, trip} = Trips.set_public(owner, trip, true)
    {:ok, view, _html} = live(build_conn(), ~p"/")

    assert has_element?(view, "#landing")
    assert has_element?(view, "#public-trips #trips-#{trip.id}", "Acadia")

    map = view |> element("#public-map") |> render()
    assert map =~ ~s(phx-hook="PhotoMap")
    assert map =~ "/t/#{trip.id}?photo=#{photo.id}"
  end

  test "trips shared only by link, and private ones, are never listed", %{
    owner: owner,
    trip: trip
  } do
    {:ok, _} = Trips.share_trip(owner, trip)
    {:ok, view, _html} = live(build_conn(), ~p"/")
    refute has_element?(view, "#public-trips #trips-#{trip.id}")
    assert has_element?(view, "#public-trips-empty")
  end

  test "signed in, it's the same map, no redirect", %{conn: conn, owner: owner, trip: trip} do
    conn = log_in_user(conn, owner.user)
    {:ok, view, _html} = live(conn, ~p"/")
    refute has_element?(view, "#landing")
    assert has_element?(view, "#home-my-photos")

    # A trip made public while the page is open shows up.
    # (render/1 waits for the page to have handled the broadcast.)
    {:ok, _} = Trips.set_public(owner, trip, true)
    render(view)
    assert has_element?(view, "#public-trips #trips-#{trip.id}")

    {:ok, _} = Trips.set_public(owner, trip, false)
    render(view)
    refute has_element?(view, "#public-trips #trips-#{trip.id}")
  end
end