defmodule AprsmeWeb.ApiDocsLiveTest do use AprsmeWeb.ConnCase import Phoenix.LiveViewTest describe "ApiDocsLive" do test "renders the API documentation page", %{conn: conn} do {:ok, _lv, html} = live(conn, ~p"/api", on_error: :warn) assert html =~ "API Documentation" assert html =~ "/api/v1/callsign" assert html =~ "/api/v1/weather/nearby" end test "update_callsign event updates the callsign input", %{conn: conn} do {:ok, lv, _html} = live(conn, ~p"/api", on_error: :warn) rendered = lv |> element("#test_callsign") |> render_change(%{"callsign" => "k1abc"}) assert rendered =~ "k1abc" end test "submitting test_api with empty callsign shows an error", %{conn: conn} do {:ok, lv, _html} = live(conn, ~p"/api", on_error: :warn) rendered = lv |> form("form[phx-submit=\"test_api\"]", %{"callsign" => " "}) |> render_submit() assert rendered =~ "Please enter a callsign" end test "invalid callsign format returns an error response", %{conn: conn} do {:ok, lv, _html} = live(conn, ~p"/api", on_error: :warn) # Seed the callsign via the phx-change so the form submits properly. lv |> element("#test_callsign") |> render_change(%{"callsign" => "BAD!"}) rendered = lv |> form("form[phx-submit=\"test_api\"]", %{"callsign" => "BAD!"}) |> render_submit() # The handler sends itself a :call_api message; after it returns the # result assign should contain the invalid-format error JSON. render(lv) assert rendered =~ "Test API" or rendered =~ "Testing..." # Wait for the async message handler to complete. :ok = Process.sleep(50) assert render(lv) =~ "Invalid callsign format" end end end