You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.2 KiB
111 lines
3.2 KiB
2 years ago
|
defmodule ChirpWeb.PostLiveTest do
|
||
|
use ChirpWeb.ConnCase
|
||
|
|
||
|
import Phoenix.LiveViewTest
|
||
|
import Chirp.TimelineFixtures
|
||
|
|
||
|
@create_attrs %{body: "some body", likes_count: 42, reposts_count: 42, username: "some username"}
|
||
|
@update_attrs %{body: "some updated body", likes_count: 43, reposts_count: 43, username: "some updated username"}
|
||
|
@invalid_attrs %{body: nil, likes_count: nil, reposts_count: nil, username: nil}
|
||
|
|
||
|
defp create_post(_) do
|
||
|
post = post_fixture()
|
||
|
%{post: post}
|
||
|
end
|
||
|
|
||
|
describe "Index" do
|
||
|
setup [:create_post]
|
||
|
|
||
|
test "lists all posts", %{conn: conn, post: post} do
|
||
|
{:ok, _index_live, html} = live(conn, ~p"/posts")
|
||
|
|
||
|
assert html =~ "Listing Posts"
|
||
|
assert html =~ post.body
|
||
|
end
|
||
|
|
||
|
test "saves new post", %{conn: conn} do
|
||
|
{:ok, index_live, _html} = live(conn, ~p"/posts")
|
||
|
|
||
|
assert index_live |> element("a", "New Post") |> render_click() =~
|
||
|
"New Post"
|
||
|
|
||
|
assert_patch(index_live, ~p"/posts/new")
|
||
|
|
||
|
assert index_live
|
||
|
|> form("#post-form", post: @invalid_attrs)
|
||
|
|> render_change() =~ "can't be blank"
|
||
|
|
||
|
{:ok, _, html} =
|
||
|
index_live
|
||
|
|> form("#post-form", post: @create_attrs)
|
||
|
|> render_submit()
|
||
|
|> follow_redirect(conn, ~p"/posts")
|
||
|
|
||
|
assert html =~ "Post created successfully"
|
||
|
assert html =~ "some body"
|
||
|
end
|
||
|
|
||
|
test "updates post in listing", %{conn: conn, post: post} do
|
||
|
{:ok, index_live, _html} = live(conn, ~p"/posts")
|
||
|
|
||
|
assert index_live |> element("#posts-#{post.id} a", "Edit") |> render_click() =~
|
||
|
"Edit Post"
|
||
|
|
||
|
assert_patch(index_live, ~p"/posts/#{post}/edit")
|
||
|
|
||
|
assert index_live
|
||
|
|> form("#post-form", post: @invalid_attrs)
|
||
|
|> render_change() =~ "can't be blank"
|
||
|
|
||
|
{:ok, _, html} =
|
||
|
index_live
|
||
|
|> form("#post-form", post: @update_attrs)
|
||
|
|> render_submit()
|
||
|
|> follow_redirect(conn, ~p"/posts")
|
||
|
|
||
|
assert html =~ "Post updated successfully"
|
||
|
assert html =~ "some updated body"
|
||
|
end
|
||
|
|
||
|
test "deletes post in listing", %{conn: conn, post: post} do
|
||
|
{:ok, index_live, _html} = live(conn, ~p"/posts")
|
||
|
|
||
|
assert index_live |> element("#posts-#{post.id} a", "Delete") |> render_click()
|
||
|
refute has_element?(index_live, "#post-#{post.id}")
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe "Show" do
|
||
|
setup [:create_post]
|
||
|
|
||
|
test "displays post", %{conn: conn, post: post} do
|
||
|
{:ok, _show_live, html} = live(conn, ~p"/posts/#{post}")
|
||
|
|
||
|
assert html =~ "Show Post"
|
||
|
assert html =~ post.body
|
||
|
end
|
||
|
|
||
|
test "updates post within modal", %{conn: conn, post: post} do
|
||
|
{:ok, show_live, _html} = live(conn, ~p"/posts/#{post}")
|
||
|
|
||
|
assert show_live |> element("a", "Edit") |> render_click() =~
|
||
|
"Edit Post"
|
||
|
|
||
|
assert_patch(show_live, ~p"/posts/#{post}/show/edit")
|
||
|
|
||
|
assert show_live
|
||
|
|> form("#post-form", post: @invalid_attrs)
|
||
|
|> render_change() =~ "can't be blank"
|
||
|
|
||
|
{:ok, _, html} =
|
||
|
show_live
|
||
|
|> form("#post-form", post: @update_attrs)
|
||
|
|> render_submit()
|
||
|
|> follow_redirect(conn, ~p"/posts/#{post}")
|
||
|
|
||
|
assert html =~ "Post updated successfully"
|
||
|
assert html =~ "some updated body"
|
||
|
end
|
||
|
end
|
||
|
end
|