4.8 KiB · text History e569b6d
defmodule CartosnapAppWeb.TripLiveTest do
  use CartosnapAppWeb.ConnCase, async: true

  import Phoenix.LiveViewTest
  import CartosnapApp.PhotosFixtures

  alias CartosnapApp.Trips

  setup :register_and_log_in_user

  test "lists your trips", %{conn: conn, scope: scope} do
    trip = trip_fixture(scope)
    {:ok, view, _html} = live(conn, ~p"/trips")
    assert has_element?(view, "#trips-#{trip.id}")
  end

  test "creates a trip", %{conn: conn} do
    {:ok, view, _html} = live(conn, ~p"/trips/new")

    assert view
           |> form("#trip-form",
             trip: %{name: "", date_start: "2025-07-01", date_end: "2025-07-02"}
           )
           |> render_change() =~ "can't be blank"

    {:ok, _view, html} =
      view
      |> form("#trip-form",
        trip: %{name: "Maine", date_start: "2025-07-01", date_end: "2025-07-04"}
      )
      |> render_submit()
      |> follow_redirect(conn)

    assert html =~ "Maine"
  end

  test "edits and deletes a trip", %{conn: conn, scope: scope} do
    trip = trip_fixture(scope, name: "Old name")

    {:ok, view, _html} = live(conn, ~p"/trips/#{trip}/edit")

    view
    |> form("#trip-form", trip: %{name: "New name"})
    |> render_submit()

    assert Trips.get_trip!(scope, trip.id).name == "New name"

    {:ok, view, _html} = live(conn, ~p"/trips/#{trip}")
    view |> element("#delete-trip") |> render_click()
    assert_redirect(view, ~p"/trips")
    assert Trips.list_trips(scope) == []
  end

  test "shows a trip's photos and map", %{conn: conn, scope: scope} do
    trip = trip_fixture(scope)
    photo = photo_fixture(scope, lat: 44.0, lon: -69.0)
    {:ok, _} = Trips.add_photos(scope, trip.id, [photo.id])

    {:ok, view, _html} = live(conn, ~p"/trips/#{trip}")
    assert has_element?(view, "#trip-photos #photos-#{photo.id}")
    assert has_element?(view, "#trip-map[phx-hook=PhotoMap]")
  end

  test "someone else's trip is not found", %{conn: conn} do
    theirs = trip_fixture(CartosnapApp.AccountsFixtures.user_scope_fixture())
    assert_raise Ecto.NoResultsError, fn -> live(conn, ~p"/trips/#{theirs}") end
  end

  test "the owner makes a trip public, apart from sharing it", %{conn: conn, scope: scope} do
    trip = trip_fixture(scope)
    {:ok, view, _html} = live(conn, ~p"/trips/#{trip}")

    view |> element("#toggle-public") |> render_click()
    view |> element("#make-public") |> render_click()
    assert has_element?(view, "#public-link[value$='/t/#{trip.id}']")
    assert Trips.get_public_trip(trip.id)
    assert Trips.get_trip!(scope, trip.id).share_token == nil

    view |> element("#public-promoted-only") |> render_click()
    assert Trips.get_trip!(scope, trip.id).public_promoted_only

    view |> element("#make-private") |> render_click()
    assert Trips.get_public_trip(trip.id) == nil
  end

  describe "areas" do
    setup %{scope: scope} do
      Req.Test.stub(CartosnapApp.Geocoder, fn conn ->
        city = if String.to_float(conn.query_params["lat"]) > 35.3, do: "Tokyo", else: "Kyoto"
        Req.Test.json(conn, %{"features" => [%{"properties" => %{"city" => city}}]})
      end)

      trip = trip_fixture(scope)

      tokyo =
        for i <- 1..2,
            do:
              photo_fixture(scope,
                lat: 35.6762 + i / 1000,
                lon: 139.6503,
                date: ~U[2025-06-01 10:00:00Z]
              )

      kyoto = photo_fixture(scope, lat: 35.0116, lon: 135.7681, date: ~U[2025-06-04 10:00:00Z])
      {:ok, _} = Trips.add_photos(scope, trip.id, Enum.map([kyoto | tokyo], & &1.id))
      %{trip: trip, tokyo: tokyo, kyoto: kyoto}
    end

    test "split the album by place, named, and narrow to one", %{conn: conn} = ctx do
      {:ok, view, _html} = live(conn, ~p"/trips/#{ctx.trip}")

      # Named once the lookup is back.
      render_async(view, 2_000)
      assert has_element?(view, "#area-1", "Tokyo")
      assert has_element?(view, "#area-2", "Kyoto")

      view |> element("#area-2") |> render_click()
      assert_patch(view, ~p"/trips/#{ctx.trip}?area=2")
      assert has_element?(view, "#photos-#{ctx.kyoto.id}")
      refute has_element?(view, "#photos-#{hd(ctx.tokyo).id}")

      # Clicking its ring on the map again goes back to everything.
      render_hook(view, "select-area", %{"id" => "2"})
      assert_patch(view, ~p"/trips/#{ctx.trip}")
      assert has_element?(view, "#photos-#{hd(ctx.tokyo).id}")
    end

    test "the owner turns them off and on", %{conn: conn, scope: scope, trip: trip} do
      {:ok, view, _html} = live(conn, ~p"/trips/#{trip}")
      assert has_element?(view, "#trip-areas")

      view |> element("#toggle-areas") |> render_click()
      refute has_element?(view, "#trip-areas")
      refute Trips.get_trip!(scope, trip.id).areas

      view |> element("#toggle-areas") |> render_click()
      assert has_element?(view, "#trip-areas")
    end
  end
end