defmodule ZoeyscomputerWeb.GistLiveTest do
  use ZoeyscomputerWeb.ConnCase

  import Phoenix.LiveViewTest
  import Zoeyscomputer.GistsFixtures

  @create_attrs %{code: "some code", lang: "some lang"}
  @update_attrs %{code: "some updated code", lang: "some updated lang"}
  @invalid_attrs %{code: nil, lang: nil}

  defp create_gist(_) do
    gist = gist_fixture()
    %{gist: gist}
  end

  describe "Index" do
    setup [:create_gist]

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

      assert html =~ "Listing Gists"
      assert html =~ gist.code
    end

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

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

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

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

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

      assert_patch(index_live, ~p"/gists")

      html = render(index_live)
      assert html =~ "Gist created successfully"
      assert html =~ "some code"
    end

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

      assert index_live |> element("#gists-#{gist.id} a", "Edit") |> render_click() =~
               "Edit Gist"

      assert_patch(index_live, ~p"/gists/#{gist}/edit")

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

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

      assert_patch(index_live, ~p"/gists")

      html = render(index_live)
      assert html =~ "Gist updated successfully"
      assert html =~ "some updated code"
    end

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

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

  describe "Show" do
    setup [:create_gist]

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

      assert html =~ "Show Gist"
      assert html =~ gist.code
    end

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

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

      assert_patch(show_live, ~p"/gists/#{gist}/show/edit")

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

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

      assert_patch(show_live, ~p"/gists/#{gist}")

      html = render(show_live)
      assert html =~ "Gist updated successfully"
      assert html =~ "some updated code"
    end
  end
end
