diff --git a/lib/dotcom_web/components/trip_planner/results_summary.ex b/lib/dotcom_web/components/trip_planner/results_summary.ex index f8bb45513e..0eeacce7b8 100644 --- a/lib/dotcom_web/components/trip_planner/results_summary.ex +++ b/lib/dotcom_web/components/trip_planner/results_summary.ex @@ -16,27 +16,46 @@ defmodule DotcomWeb.Components.TripPlanner.ResultsSummary do >
{submission_summary(@changeset.changes)}
{time_summary(@changeset.changes)}
- <%= if @results.loading? do %> - <.spinner aria_label="Waiting for results" /> Waiting for results... - <% else %> - <%= if @results.error do %> - <.feedback kind={:error}>{@results.error} - <% end %> - <%= if Enum.count(@results.itinerary_groups) == 0 do %> - <.feedback kind={:warning}>No trips found. - <% else %> - <.feedback kind={:success}> - Found {Enum.count(@results.itinerary_groups)} {Inflex.inflect( - "way", - Enum.count(@results.itinerary_groups) - )} to go. - - <% end %> - <% end %> + <.results_feedback results={@results} /> """ end + defp results_feedback(%{results: %{loading?: true}} = assigns) do + ~H""" + <.spinner aria_label="Waiting for results" /> Waiting for results... + """ + end + + defp results_feedback(%{results: %{error: nil}} = assigns) do + ~H""" + <.itinerary_group_feedback itinerary_groups={@results.itinerary_groups} /> + """ + end + + defp results_feedback(assigns) do + ~H""" + <.feedback kind={:error}>{@results.error} + """ + end + + defp itinerary_group_feedback(%{itinerary_groups: []} = assigns) do + ~H""" + <.feedback kind={:warning}>No trips found. + """ + end + + defp itinerary_group_feedback(assigns) do + ~H""" + <.feedback kind={:success}> + Found {Enum.count(@itinerary_groups)} {Inflex.inflect( + "way", + Enum.count(@itinerary_groups) + )} to go. + + """ + end + defp submission_summary(%{from: from, to: to, modes: modes}) do modes_string = modes.changes |> InputForm.Modes.selected_modes() |> String.downcase() diff --git a/test/dotcom_web/live/trip_planner_test.exs b/test/dotcom_web/live/trip_planner_test.exs index 67816ebd03..74b9312ac6 100644 --- a/test/dotcom_web/live/trip_planner_test.exs +++ b/test/dotcom_web/live/trip_planner_test.exs @@ -322,6 +322,17 @@ defmodule DotcomWeb.Live.TripPlannerTest do refute render_async(view) =~ trip_headsign_2 end + test "displays 'No trips found.' if given an empty list of itineraries", %{ + conn: conn, + params: params + } do + stub_otp_results([]) + + {:ok, view, _html} = live(conn, ~p"/preview/trip-planner?#{params}") + + assert render_async(view) =~ "No trips found." + end + test "displays error message from the Open Trip Planner client", %{conn: conn, params: params} do error_message = Faker.Lorem.sentence() @@ -333,5 +344,18 @@ defmodule DotcomWeb.Live.TripPlannerTest do assert render_async(view) =~ error_message end + + test "does not display 'No trips found.' if there's another error", %{ + conn: conn, + params: params + } do + expect(OpenTripPlannerClient.Mock, :plan, fn _ -> + {:error, [%OpenTripPlannerClient.Error{message: Faker.Lorem.sentence()}]} + end) + + {:ok, view, _html} = live(conn, ~p"/preview/trip-planner?#{params}") + + refute render_async(view) =~ "No trips found." + end end end