Skip to content

Commit

Permalink
fix: Don't show "No trips found" message when there are other errors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson authored Dec 24, 2024
1 parent ebed273 commit cbd040f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
53 changes: 36 additions & 17 deletions lib/dotcom_web/components/trip_planner/results_summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,46 @@ defmodule DotcomWeb.Components.TripPlanner.ResultsSummary do
>
<p class="text-lg font-semibold mb-0">{submission_summary(@changeset.changes)}</p>
<p>{time_summary(@changeset.changes)}</p>
<%= if @results.loading? do %>
<.spinner aria_label="Waiting for results" /> Waiting for results...
<% else %>
<%= if @results.error do %>
<.feedback kind={:error}>{@results.error}</.feedback>
<% end %>
<%= if Enum.count(@results.itinerary_groups) == 0 do %>
<.feedback kind={:warning}>No trips found.</.feedback>
<% else %>
<.feedback kind={:success}>
Found {Enum.count(@results.itinerary_groups)} {Inflex.inflect(
"way",
Enum.count(@results.itinerary_groups)
)} to go.
</.feedback>
<% end %>
<% end %>
<.results_feedback results={@results} />
</section>
"""
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}</.feedback>
"""
end

defp itinerary_group_feedback(%{itinerary_groups: []} = assigns) do
~H"""
<.feedback kind={:warning}>No trips found.</.feedback>
"""
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.
</.feedback>
"""
end

defp submission_summary(%{from: from, to: to, modes: modes}) do
modes_string = modes.changes |> InputForm.Modes.selected_modes() |> String.downcase()

Expand Down
24 changes: 24 additions & 0 deletions test/dotcom_web/live/trip_planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

0 comments on commit cbd040f

Please sign in to comment.