defmodule MvWeb.Components.SearchBarComponentTest do
  use MvWeb.ConnCase, async: true
  import Phoenix.LiveViewTest

  describe "SearchBarComponent" do
    test "renders with placeholder", %{conn: conn} do
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/members")

      assert has_element?(view, "input[placeholder='Search...']")
    end

    test "updates query when user types", %{conn: conn} do
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/members")

      # simulate search input and check that other members are not listed
      _html =
        view
        |> element("form[role=search]")
        |> render_submit(%{"query" => "Friedrich"})

      refute has_element?(view, "input[data-testid='search-input'][value='Greta']")

      _html =
        view
        |> element("form[role=search]")
        |> render_submit(%{"query" => "Greta"})

      refute has_element?(view, "input[data-testid='search-input'][value='Friedrich']")
    end
  end
end
