defmodule OutlookWeb.DeeplAccountLiveTest do
  use OutlookWeb.ConnCase

  import Phoenix.LiveViewTest
  import Outlook.TranslatorsFixtures

  @create_attrs %{auth_key: "some auth_key", character_count: 42, character_limit: 42, description: "some description", name: "some name", our_character_count: 42}
  @update_attrs %{auth_key: "some updated auth_key", character_count: 43, character_limit: 43, description: "some updated description", name: "some updated name", our_character_count: 43}
  @invalid_attrs %{auth_key: nil, character_count: nil, character_limit: nil, description: nil, name: nil, our_character_count: nil}

  defp create_deepl_account(_) do
    deepl_account = deepl_account_fixture()
    %{deepl_account: deepl_account}
  end

  describe "Index" do
    setup [:create_deepl_account]

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

      assert html =~ "Listing Deepl accounts"
      assert html =~ deepl_account.auth_key
    end

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

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

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

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

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

      assert html =~ "Deepl account created successfully"
      assert html =~ "some auth_key"
    end

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

      assert index_live |> element("#deepl_accounts-#{deepl_account.id} a", "Edit") |> render_click() =~
               "Edit Deepl account"

      assert_patch(index_live, ~p"/deepl_accounts/#{deepl_account}/edit")

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

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

      assert html =~ "Deepl account updated successfully"
      assert html =~ "some updated auth_key"
    end

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

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

  describe "Show" do
    setup [:create_deepl_account]

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

      assert html =~ "Show Deepl account"
      assert html =~ deepl_account.auth_key
    end

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

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

      assert_patch(show_live, ~p"/deepl_accounts/#{deepl_account}/show/edit")

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

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

      assert html =~ "Deepl account updated successfully"
      assert html =~ "some updated auth_key"
    end
  end
end
