Skip to content

Commit

Permalink
feat: Suppress "Depart at" buttons when there's only one itinerary (#…
Browse files Browse the repository at this point in the history
…2250)

* refactor(test): Replace update_trip_details with update_trip_id and update_start_time

* feat: Suppress "Depart at" buttons when there's only one itinerary

* refactor: Use `:if` directive instead of pattern-matching

Co-authored-by: Cristen Jones <[email protected]>

---------

Co-authored-by: Cristen Jones <[email protected]>
  • Loading branch information
joshlarson and thecristen authored Dec 10, 2024
1 parent 6db2c89 commit 591ea7e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
18 changes: 17 additions & 1 deletion lib/dotcom_web/components/trip_planner/itinerary_detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryDetail do

~H"""
<div>
<.depart_at_buttons
selected_itinerary_detail_index={@selected_itinerary_detail_index}
itineraries={@itineraries}
target={@target}
/>
<.specific_itinerary_detail itinerary={@selected_itinerary} />
</div>
"""
end

attr :itineraries, :list
attr :selected_itinerary_detail_index, :integer
attr :target, :string

defp depart_at_buttons(assigns) do
~H"""
<div :if={Enum.count(@itineraries) > 1}>
<p class="text-sm mb-2 mt-3">Depart at</p>
<div class="flex">
<.depart_at_button
Expand All @@ -33,7 +50,6 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryDetail do
{Timex.format!(itinerary.start, "%-I:%M%p", :strftime)}
</.depart_at_button>
</div>
<.specific_itinerary_detail itinerary={@selected_itinerary} />
</div>
"""
end
Expand Down
36 changes: 31 additions & 5 deletions test/dotcom_web/live/trip_planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ defmodule DotcomWeb.Live.TripPlannerTest do
stub_otp_results(itineraries)
end

defp update_trip_details(itinerary, trip_id: trip_id, start_time: start_time) do
defp update_trip_id(itinerary, trip_id) do
updated_transit_leg =
itinerary.legs
|> Enum.at(1)
|> update_in([:trip, :gtfs_id], fn _ -> "ma_mbta_us:#{trip_id}" end)

itinerary
|> Map.update!(:legs, &List.replace_at(&1, 1, updated_transit_leg))
end

defp update_start_time(itinerary, start_time) do
itinerary
|> Map.put(:start, DateTime.new!(Date.utc_today(), start_time, "America/New_York"))
end

Expand Down Expand Up @@ -212,8 +216,8 @@ defmodule DotcomWeb.Live.TripPlannerTest do
# should update these updates and the assertions below to use
# the headsign instead of the trip ID.
stub_otp_results([
update_trip_details(base_itinerary, trip_id: trip_id_1, start_time: trip_time_1),
update_trip_details(base_itinerary, trip_id: trip_id_2, start_time: trip_time_2)
base_itinerary |> update_trip_id(trip_id_1) |> update_start_time(trip_time_1),
base_itinerary |> update_trip_id(trip_id_2) |> update_start_time(trip_time_2)
])

{:ok, view, _html} = live(conn, ~p"/preview/trip-planner?#{params}")
Expand All @@ -231,6 +235,28 @@ defmodule DotcomWeb.Live.TripPlannerTest do
refute render_async(view) =~ trip_id_1
end

test "'Depart At' buttons don't appear if there would only be one", %{
conn: conn,
params: params
} do
trip_time_1 = Faker.DateTime.forward(2) |> DateTime.to_time()
trip_time_display_1 = trip_time_1 |> Timex.format!("%-I:%M", :strftime)

base_itinerary = TripPlannerFactory.build(:otp_itinerary)

stub_otp_results([
base_itinerary |> update_start_time(trip_time_1)
])

{:ok, view, _html} = live(conn, ~p"/preview/trip-planner?#{params}")

render_async(view)

view |> element("button", "Details") |> render_click()

refute view |> element("button", trip_time_display_1) |> has_element?()
end

test "'Depart At' button state is not preserved when leaving details view", %{
conn: conn,
params: params
Expand All @@ -252,8 +278,8 @@ defmodule DotcomWeb.Live.TripPlannerTest do
# should update these updates and the assertions below to use
# the headsign instead of the trip ID.
stub_otp_results([
update_trip_details(base_itinerary, trip_id: trip_id_1, start_time: trip_time_1),
update_trip_details(base_itinerary, trip_id: trip_id_2, start_time: trip_time_2)
base_itinerary |> update_trip_id(trip_id_1) |> update_start_time(trip_time_1),
base_itinerary |> update_trip_id(trip_id_2) |> update_start_time(trip_time_2)
])

{:ok, view, _html} = live(conn, ~p"/preview/trip-planner?#{params}")
Expand Down

0 comments on commit 591ea7e

Please sign in to comment.