defmodule OutlookWeb.AuthorLiveTest do
  use OutlookWeb.ConnCase

  import Phoenix.LiveViewTest
  import Outlook.AuthorsFixtures

  @create_attrs %{description: "some description", homepage_name: "some homepage_name", homepage_url: "some homepage_url", name: "some name"}
  @update_attrs %{description: "some updated description", homepage_name: "some updated homepage_name", homepage_url: "some updated homepage_url", name: "some updated name"}
  @invalid_attrs %{description: nil, homepage_name: nil, homepage_url: nil, name: nil}

  defp create_author(_) do
    author = author_fixture()
    %{author: author}
  end

  describe "Index" do
    setup [:create_author]

    test "lists all authors", %{conn: conn, author: author} do
      {:ok, _index_live, html} = live(conn, ~p"/authors")

      assert html =~ "Listing Authors"
      assert html =~ author.description
    end

    test "saves new author", %{conn: conn} do
      {:ok, index_live, _html} = live(conn, ~p"/authors")

      assert index_live |> element("a", "New Author") |> render_click() =~
               "New Author"

      assert_patch(index_live, ~p"/authors/new")

      assert index_live
             |> form("#author-form", author: @invalid_attrs)
             |> render_change() =~ "can&#39;t be blank"

      {:ok, _, html} =
        index_live
        |> form("#author-form", author: @create_attrs)
        |> render_submit()
        |> follow_redirect(conn, ~p"/authors")

      assert html =~ "Author created successfully"
      assert html =~ "some description"
    end

    test "updates author in listing", %{conn: conn, author: author} do
      {:ok, index_live, _html} = live(conn, ~p"/authors")

      assert index_live |> element("#authors-#{author.id} a", "Edit") |> render_click() =~
               "Edit Author"

      assert_patch(index_live, ~p"/authors/#{author}/edit")

      assert index_live
             |> form("#author-form", author: @invalid_attrs)
             |> render_change() =~ "can&#39;t be blank"

      {:ok, _, html} =
        index_live
        |> form("#author-form", author: @update_attrs)
        |> render_submit()
        |> follow_redirect(conn, ~p"/authors")

      assert html =~ "Author updated successfully"
      assert html =~ "some updated description"
    end

    test "deletes author in listing", %{conn: conn, author: author} do
      {:ok, index_live, _html} = live(conn, ~p"/authors")

      assert index_live |> element("#authors-#{author.id} a", "Delete") |> render_click()
      refute has_element?(index_live, "#author-#{author.id}")
    end
  end

  describe "Show" do
    setup [:create_author]

    test "displays author", %{conn: conn, author: author} do
      {:ok, _show_live, html} = live(conn, ~p"/authors/#{author}")

      assert html =~ "Show Author"
      assert html =~ author.description
    end

    test "updates author within modal", %{conn: conn, author: author} do
      {:ok, show_live, _html} = live(conn, ~p"/authors/#{author}")

      assert show_live |> element("a", "Edit") |> render_click() =~
               "Edit Author"

      assert_patch(show_live, ~p"/authors/#{author}/show/edit")

      assert show_live
             |> form("#author-form", author: @invalid_attrs)
             |> render_change() =~ "can&#39;t be blank"

      {:ok, _, html} =
        show_live
        |> form("#author-form", author: @update_attrs)
        |> render_submit()
        |> follow_redirect(conn, ~p"/authors/#{author}")

      assert html =~ "Author updated successfully"
      assert html =~ "some updated description"
    end
  end
end
