diff --git a/lib/arrow_web/live/shuttle_live/shuttle_live.ex b/lib/arrow_web/live/shuttle_live/shuttle_live.ex index 1c266115..c8bcdf96 100644 --- a/lib/arrow_web/live/shuttle_live/shuttle_live.ex +++ b/lib/arrow_web/live/shuttle_live/shuttle_live.ex @@ -17,7 +17,14 @@ defmodule ArrowWeb.ShuttleViewLive do def shuttle_form(assigns) do ~H""" - <.simple_form :let={f} for={@form} as={:shuttle_route} action={@http_action} phx-submit={@action}> + <.simple_form + :let={f} + for={@form} + as={:shuttle_route} + action={@http_action} + phx-submit={@action} + id="shuttle-form" + >
%{ + :_persistent_id => "0", + destination: "Broadway", + direction_desc: "Southbound", + direction_id: "0", + shape_id: "", + suffix: "", + waypoint: "" + }, + "1" => %{ + :_persistent_id => "1", + destination: "Harvard", + direction_desc: "Northbound", + direction_id: "1", + shape_id: "", + suffix: "", + waypoint: "" + } + }, + shuttle_name: "Blah", + status: "draft" + } + + @update_attrs %{ + disrupted_route_id: "", + routes: %{ + "0" => %{ + :_persistent_id => "0", + destination: "Broadway", + direction_desc: "Southbound", + direction_id: "0", + shape_id: "", + suffix: "", + waypoint: "" + }, + "1" => %{ + :_persistent_id => "1", + destination: "Harvard", + direction_desc: "Northbound", + direction_id: "1", + shape_id: "", + suffix: "", + waypoint: "" + } + }, + shuttle_name: "Meh", + status: "draft" + } + + @invalid_attrs %{ + disrupted_route_id: "", + shuttle_name: nil, + status: "draft" + } + + describe "new shuttle" do + @tag :authenticated_admin + test "renders form", %{conn: conn} do + {:ok, _new_live, html} = live(conn, ~p"/shuttles/new") + assert html =~ "create new replacement service shuttle" + end + end + + describe "create shuttle" do + @tag :authenticated_admin + test "redirects to new shuttle when data is valid", %{conn: conn} do + {:ok, new_live, _html} = live(conn, ~p"/shuttles/new") + + {:ok, conn} = + new_live + |> form("#shuttle-form", shuttle_route: @create_attrs) + |> render_submit() + |> follow_redirect(conn) + + assert html_response(conn, 200) =~ ~r/shuttle created successfully/i + + assert %{"id" => _id} = conn.params + end + + @tag :authenticated_admin + test "renders errors when data is invalid", %{conn: conn} do + {:ok, new_live, _html} = live(conn, ~p"/shuttles/new") + + assert new_live |> form("#shuttle-form", shuttle_route: @invalid_attrs) |> render_submit() =~ + "can't be blank" + end + end + + describe "edit shuttle" do + setup [:create_shuttle] + + @tag :authenticated_admin + test "redirects to updated shuttle when data is valid", %{conn: conn, shuttle: shuttle} do + {:ok, edit_live, _html} = live(conn, ~p"/shuttles/#{shuttle}/edit") + + {:ok, conn} = + edit_live + |> form("#shuttle-form", shuttle_route: @update_attrs) + |> render_submit() + |> follow_redirect(conn) + + assert html_response(conn, 200) =~ ~r/shuttle updated successfully/i + end + + @tag :authenticated_admin + test "renders errors when data is invalid", %{conn: conn, shuttle: shuttle} do + {:ok, new_live, _html} = live(conn, ~p"/shuttles/#{shuttle}/edit") + + assert new_live |> form("#shuttle-form", shuttle_route: @invalid_attrs) |> render_submit() =~ + "can't be blank" + end + end + + defp create_shuttle(_) do + shuttle = shuttle_fixture() + %{shuttle: shuttle} + end +end