defmodule WildWeb.BookReadLiveTest do
  use WildWeb.ConnCase

  import Phoenix.LiveViewTest
  import Wild.BookReadsFixtures

  @create_attrs %{progress: 42, rating: 42, thoughts: "some thoughts", read_start: "2025-01-08T10:31:00Z", read_finish: "2025-01-08T10:31:00Z"}
  @update_attrs %{progress: 43, rating: 43, thoughts: "some updated thoughts", read_start: "2025-01-09T10:31:00Z", read_finish: "2025-01-09T10:31:00Z"}
  @invalid_attrs %{progress: nil, rating: nil, thoughts: nil, read_start: nil, read_finish: nil}

  defp create_book_read(_) do
    book_read = book_read_fixture()
    %{book_read: book_read}
  end

  describe "Index" do
    setup [:create_book_read]

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

      assert html =~ "Listing Book reads"
      assert html =~ book_read.thoughts
    end

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

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

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

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

      assert index_live
             |> form("#book_read-form", book_read: @create_attrs)
             |> render_submit()

      assert_patch(index_live, ~p"/book_reads")

      html = render(index_live)
      assert html =~ "Book read created successfully"
      assert html =~ "some thoughts"
    end

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

      assert index_live |> element("#book_reads-#{book_read.id} a", "Edit") |> render_click() =~
               "Edit Book read"

      assert_patch(index_live, ~p"/book_reads/#{book_read}/edit")

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

      assert index_live
             |> form("#book_read-form", book_read: @update_attrs)
             |> render_submit()

      assert_patch(index_live, ~p"/book_reads")

      html = render(index_live)
      assert html =~ "Book read updated successfully"
      assert html =~ "some updated thoughts"
    end

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

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

  describe "Show" do
    setup [:create_book_read]

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

      assert html =~ "Show Book read"
      assert html =~ book_read.thoughts
    end

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

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

      assert_patch(show_live, ~p"/book_reads/#{book_read}/show/edit")

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

      assert show_live
             |> form("#book_read-form", book_read: @update_attrs)
             |> render_submit()

      assert_patch(show_live, ~p"/book_reads/#{book_read}")

      html = render(show_live)
      assert html =~ "Book read updated successfully"
      assert html =~ "some updated thoughts"
    end
  end
end
