defmodule WishWeb.ItemLiveTest do
  use WishWeb.ConnCase

  import Phoenix.LiveViewTest
  import Wish.WishlistFixtures
  import Wish.AccountsFixtures

  @create_attrs %{
    description: "some description",
    title: "some title",
    url: "some url",
    desire: 2
  }
  @update_attrs %{
    description: "some updated description",
    title: "some updated title",
    url: "some updated url",
    desire: 3
  }
  @invalid_attrs %{description: nil, title: nil, url: nil, desire: nil}

  defp create_item(_) do
    item = item_fixture()
    %{item: item}
  end

  describe "Index" do
    setup [:create_item]

    test "lists all items", %{conn: conn, item: item} do
      {:ok, _index_live, html} =
        conn
        |> log_in_user(user_fixture())
        |> live(~p"/items")

      assert html =~ "Listing Items"
      assert html =~ item.description
    end

    test "saves new item", %{conn: conn} do
      {:ok, index_live, _html} =
        conn
        |> log_in_user(user_fixture())
        |> live(~p"/items")

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

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

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

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

      assert_patch(index_live, ~p"/items")

      html = render(index_live)
      assert html =~ "Item created successfully"
      assert html =~ "some description"
    end

    # test "updates item in listing", %{conn: conn, item: item} do
    # {:ok, index_live, _html} =
    # conn
    # |> log_in_user(user_fixture())
    # |> live(~p"/items")
    # 
    # index_live
    # |> element("#items-#{item.id} a", "Edit")
    # |> render_click()
    # 
    # assert_patch(index_live, ~p"/items/#{item}/show/edit")
    # 
    # assert index_live
    # |> form("#item-form", item: @invalid_attrs)
    # |> render_change() =~ "can&#39;t be blank"
    # 
    # assert index_live
    # |> form("#item-form", item: @update_attrs)
    # |> render_submit()
    # 
    # assert_patch(index_live, ~p"/items")
    # 
    # html = render(index_live)
    # assert html =~ "Item updated successfully"
    # assert html =~ "some updated description"
    # end

    test "deletes item in listing", %{conn: conn, item: item} do
      {:ok, index_live, _html} =
        conn
        |> log_in_user(user_fixture())
        |> live(~p"/items")

      assert index_live
             |> element("#delete-items-#{item.id}")
             |> render_click()

      refute has_element?(index_live, "#items-#{item.id}")
    end
  end

  describe "Edit" do
    setup [:create_item]

    test "displays item", %{conn: conn, item: item} do
      {:ok, _show_live, html} =
        conn
        |> log_in_user(user_fixture())
        |> live(~p"/items/#{item}/show/edit")

      assert html =~ "Edit Item"
      assert html =~ item.description
    end

    test "updates item within modal", %{conn: conn, item: item} do
      {:ok, edit_live, _html} =
        conn
        |> log_in_user(user_fixture())
        |> live(~p"/items/#{item}/show/edit")

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

      assert edit_live
             |> form("#item-form", item: @update_attrs)
             |> render_submit()

      html = render(edit_live)
      assert html =~ "Item updated successfully"
      assert html =~ "some updated description"
    end
  end
end
