defmodule OutlookWeb.TranslationLiveTest do
  use OutlookWeb.ConnCase

  import Phoenix.LiveViewTest
  import Outlook.TranslationsFixtures

  @create_attrs %{content: %{}, date: "2022-12-25T17:13:00Z", lang: "some lang", public: true, teaser: "some teaser", title: "some title", unauthorized: true}
  @update_attrs %{content: %{}, date: "2022-12-26T17:13:00Z", lang: "some updated lang", public: false, teaser: "some updated teaser", title: "some updated title", unauthorized: false}
  @invalid_attrs %{content: nil, date: nil, lang: nil, public: false, teaser: nil, title: nil, unauthorized: false}

  defp create_translation(_) do
    translation = translation_fixture()
    %{translation: translation}
  end

  describe "Index" do
    setup [:create_translation]

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

      assert html =~ "Listing Translations"
      assert html =~ translation.lang
    end

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

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

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

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

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

      assert html =~ "Translation created successfully"
      assert html =~ "some lang"
    end

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

      assert index_live |> element("#translations-#{translation.id} a", "Edit") |> render_click() =~
               "Edit Translation"

      assert_patch(index_live, ~p"/translations/#{translation}/edit")

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

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

      assert html =~ "Translation updated successfully"
      assert html =~ "some updated lang"
    end

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

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

  describe "Show" do
    setup [:create_translation]

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

      assert html =~ "Show Translation"
      assert html =~ translation.lang
    end

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

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

      assert_patch(show_live, ~p"/translations/#{translation}/show/edit")

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

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

      assert html =~ "Translation updated successfully"
      assert html =~ "some updated lang"
    end
  end
end
