184 lines
5.8 KiB
Elixir
184 lines
5.8 KiB
Elixir
defmodule PhoenixRealWorldWeb.ArticleLiveTest do
|
|
use PhoenixRealWorldWeb.ConnCase
|
|
|
|
import Phoenix.LiveViewTest
|
|
import PhoenixRealWorld.BlogsFixtures
|
|
|
|
@create_attrs %{title: "some title", body: "some body"}
|
|
@update_attrs %{title: "some updated title", body: "some updated body"}
|
|
@invalid_attrs %{title: nil, body: nil}
|
|
|
|
defp create_article(_) do
|
|
article = article_fixture()
|
|
%{article: article}
|
|
end
|
|
|
|
# ↓追加
|
|
defp create_article_with_tag(_) do
|
|
{:ok, %{article: article}} =
|
|
Blogs.insert_article_with_tag(%{
|
|
title: "some title",
|
|
body: "some body",
|
|
author_id: user_fixture().id,
|
|
tags_string: "test"
|
|
})
|
|
%{article_with_tag: article}
|
|
end
|
|
|
|
describe "Index" do
|
|
setup [:create_article, :create_article_with_tag] # ←変更
|
|
|
|
test "lists all articles", %{conn: conn, article: article} do
|
|
{:ok, _index_live, html} = live(conn, ~p"/articles")
|
|
|
|
assert html =~ "Listing Articles"
|
|
assert html =~ article.title
|
|
end
|
|
|
|
test "searches articles by tag", %{conn: conn, article: article, article_with_tag: article_with_tag} do
|
|
{:ok, index_live, _html} = live(conn, ~p"/articles")
|
|
|
|
index_live |> element("a", "test") |> render_click()
|
|
assert_redirect(index_live, ~p"/articles?tag=test")
|
|
|
|
{:ok, _index_live, html} = live(conn, ~p"/articles?tag=test")
|
|
|
|
refute html =~ "/articles/#{article.id}"
|
|
assert html =~ "/articles/#{article_with_tag.id}"
|
|
end
|
|
|
|
test "saves new article", %{conn: conn} do
|
|
# {:ok, index_live, _html} = live(conn, ~p"/articles")
|
|
#↓追加
|
|
{:ok, index_live, _html} =
|
|
conn
|
|
|> log_in_user(user_fixture()) # 第2引数のユーザーでログインした状態にする
|
|
|> live(~p"/articles")
|
|
|
|
assert index_live |> element("a", "New Article") |> render_click() =~
|
|
"New Article"
|
|
|
|
assert_patch(index_live, ~p"/articles/new")
|
|
|
|
assert index_live
|
|
|> form("#article-form", article: @invalid_attrs)
|
|
|> render_change() =~ "can't be blank"
|
|
|
|
assert index_live
|
|
|> form("#article-form", article: @create_attrs)
|
|
|> render_submit()
|
|
|
|
assert_patch(index_live, ~p"/articles")
|
|
|
|
html = render(index_live)
|
|
assert html =~ "Article created successfully"
|
|
assert html =~ "some title"
|
|
end
|
|
|
|
# test "updates article in listing", %{conn: conn, article: article} do
|
|
# {:ok, index_live, _html} = live(conn, ~p"/articles")
|
|
|
|
# assert index_live |> element("#articles-#{article.id} a", "Edit") |> render_click() =~
|
|
# "Edit Article"
|
|
|
|
# assert_patch(index_live, ~p"/articles/#{article}/edit")
|
|
|
|
# assert index_live
|
|
# |> form("#article-form", article: @invalid_attrs)
|
|
# |> render_change() =~ "can't be blank"
|
|
|
|
# assert index_live
|
|
# |> form("#article-form", article: @update_attrs)
|
|
# |> render_submit()
|
|
|
|
# assert_patch(index_live, ~p"/articles")
|
|
|
|
# html = render(index_live)
|
|
# assert html =~ "Article updated successfully"
|
|
# assert html =~ "some updated title"
|
|
# end
|
|
|
|
# test "deletes article in listing", %{conn: conn, article: article} do
|
|
# {:ok, index_live, _html} = live(conn, ~p"/articles")
|
|
|
|
# assert index_live |> element("#articles-#{article.id} a", "Delete") |> render_click()
|
|
# refute has_element?(index_live, "#articles-#{article.id}")
|
|
# end
|
|
end
|
|
|
|
describe "Show" do
|
|
setup [:create_article]
|
|
|
|
test "displays article", %{conn: conn, article: article} do
|
|
{:ok, _show_live, html} = live(conn, ~p"/articles/#{article}")
|
|
|
|
assert html =~ "Show Article"
|
|
assert html =~ article.title
|
|
end
|
|
|
|
test "updates article within modal", %{conn: conn, article: article} do
|
|
# {:ok, show_live, _html} = live(conn, ~p"/articles/#{article}")
|
|
#↓追加
|
|
{:ok, show_live, _html} =
|
|
conn
|
|
|> log_in_user(PhoenixRealWorld.Repo.preload(article, :author).author)
|
|
|> live(~p"/articles/#{article}")
|
|
|
|
assert show_live |> element("a", "Edit") |> render_click() =~
|
|
"Edit Article"
|
|
|
|
assert_patch(show_live, ~p"/articles/#{article}/show/edit")
|
|
|
|
assert show_live
|
|
|> form("#article-form", article: @invalid_attrs)
|
|
|> render_change() =~ "can't be blank"
|
|
|
|
assert show_live
|
|
|> form("#article-form", article: @update_attrs)
|
|
|> render_submit()
|
|
|
|
assert_patch(show_live, ~p"/articles/#{article}")
|
|
|
|
html = render(show_live)
|
|
assert html =~ "Article updated successfully"
|
|
assert html =~ "some updated title"
|
|
end
|
|
|
|
# 追加 記事の削除のテスト
|
|
test "deletes article", %{conn: conn, article: article} do
|
|
{:ok, show_live, _html}=
|
|
conn
|
|
|> log_in_user(PhoenixRealWorld.Repo.preload(article, :author).author)
|
|
|> live(~p"/articles/#{article}")
|
|
|
|
assert show_live |> element("a", "Delete article") |> render_click()
|
|
assert_redirect(show_live, ~p"/articles")
|
|
|
|
{:ok, index_live, _html} = live(conn, ~p"/articles")
|
|
refute html =~ "/articles/#{article.id}"
|
|
end
|
|
|
|
# 追加 コメントの作成のテスト
|
|
test "creates comment", %{conn: conn, article: article} do
|
|
author = user_fixture()
|
|
{:ok, show_live, _html} =
|
|
conn
|
|
|> log_in_user(author)
|
|
|> live(~p"/articles/#{article}")
|
|
|
|
assert show_live
|
|
|> form("#comment-form", comment: %{body: ""})
|
|
|> render_change() =~ "can't be blank"
|
|
|
|
assert show_live
|
|
|> form("#comment-form", comment: %{body: "some comment"})
|
|
|> render_submit()
|
|
|
|
html = render(show_live)
|
|
|
|
assert html =~ author.email
|
|
assert html =~ "some comment"
|
|
end
|
|
|
|
end
|
|
end
|