2022-07-25 20:08:40 -04:00
|
|
|
defmodule MemexWeb.NoteLiveTest do
|
|
|
|
use MemexWeb.ConnCase
|
|
|
|
|
|
|
|
import Phoenix.LiveViewTest
|
|
|
|
import Memex.NotesFixtures
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
@create_attrs %{
|
|
|
|
"content" => "some content",
|
|
|
|
"tags_string" => "tag1",
|
2022-11-26 14:51:18 -05:00
|
|
|
"slug" => "some-slug",
|
2022-11-17 22:38:52 -05:00
|
|
|
"visibility" => :public
|
|
|
|
}
|
2022-07-25 22:05:54 -04:00
|
|
|
@update_attrs %{
|
2022-11-17 22:38:52 -05:00
|
|
|
"content" => "some updated content",
|
|
|
|
"tags_string" => "tag1,tag2",
|
2022-11-26 14:51:18 -05:00
|
|
|
"slug" => "some-updated-slug",
|
2022-11-17 22:38:52 -05:00
|
|
|
"visibility" => :private
|
|
|
|
}
|
|
|
|
@invalid_attrs %{
|
|
|
|
"content" => nil,
|
|
|
|
"tags_string" => "",
|
2022-11-26 14:51:18 -05:00
|
|
|
"slug" => nil,
|
2022-11-17 22:38:52 -05:00
|
|
|
"visibility" => nil
|
2022-07-25 22:05:54 -04:00
|
|
|
}
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
defp create_note(%{user: user}) do
|
|
|
|
[note: note_fixture(user)]
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Index" do
|
2022-11-17 22:38:52 -05:00
|
|
|
setup [:register_and_log_in_user, :create_note]
|
2022-07-25 20:08:40 -04:00
|
|
|
|
|
|
|
test "lists all notes", %{conn: conn, note: note} do
|
|
|
|
{:ok, _index_live, html} = live(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2022-11-17 22:30:01 -05:00
|
|
|
assert html =~ "notes"
|
2022-07-25 20:08:40 -04:00
|
|
|
assert html =~ note.content
|
|
|
|
end
|
|
|
|
|
|
|
|
test "saves new note", %{conn: conn} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2022-11-17 22:30:01 -05:00
|
|
|
assert index_live |> element("a", "new note") |> render_click() =~
|
|
|
|
"new note"
|
2022-07-25 20:08:40 -04:00
|
|
|
|
|
|
|
assert_patch(index_live, Routes.note_index_path(conn, :new))
|
|
|
|
|
|
|
|
assert index_live
|
|
|
|
|> form("#note-form", note: @invalid_attrs)
|
|
|
|
|> render_change() =~ "can't be blank"
|
|
|
|
|
|
|
|
{:ok, _, html} =
|
|
|
|
index_live
|
|
|
|
|> form("#note-form", note: @create_attrs)
|
|
|
|
|> render_submit()
|
|
|
|
|> follow_redirect(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert html =~ "#{@create_attrs |> Map.get("slug")} created"
|
2022-07-25 20:08:40 -04:00
|
|
|
assert html =~ "some content"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "updates note in listing", %{conn: conn, note: note} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
assert index_live |> element("[data-qa=\"note-edit-#{note.id}\"]") |> render_click() =~
|
2022-11-17 22:30:01 -05:00
|
|
|
"edit"
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert_patch(index_live, Routes.note_index_path(conn, :edit, note.slug))
|
2022-07-25 20:08:40 -04:00
|
|
|
|
|
|
|
assert index_live
|
|
|
|
|> form("#note-form", note: @invalid_attrs)
|
|
|
|
|> render_change() =~ "can't be blank"
|
|
|
|
|
|
|
|
{:ok, _, html} =
|
|
|
|
index_live
|
|
|
|
|> form("#note-form", note: @update_attrs)
|
|
|
|
|> render_submit()
|
|
|
|
|> follow_redirect(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert html =~ "#{@update_attrs |> Map.get("slug")} saved"
|
2022-07-25 20:08:40 -04:00
|
|
|
assert html =~ "some updated content"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "deletes note in listing", %{conn: conn, note: note} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
assert index_live |> element("[data-qa=\"delete-note-#{note.id}\"]") |> render_click()
|
2022-07-25 20:08:40 -04:00
|
|
|
refute has_element?(index_live, "#note-#{note.id}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-17 22:30:01 -05:00
|
|
|
describe "show" do
|
|
|
|
setup [:register_and_log_in_user, :create_note]
|
2022-07-25 20:08:40 -04:00
|
|
|
|
|
|
|
test "displays note", %{conn: conn, note: note} do
|
2022-11-26 14:51:18 -05:00
|
|
|
{:ok, _show_live, html} = live(conn, Routes.note_show_path(conn, :show, note.slug))
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-24 17:44:01 -05:00
|
|
|
assert html =~ "note"
|
2022-07-25 20:08:40 -04:00
|
|
|
assert html =~ note.content
|
|
|
|
end
|
|
|
|
|
|
|
|
test "updates note within modal", %{conn: conn, note: note} do
|
2022-11-26 14:51:18 -05:00
|
|
|
{:ok, show_live, _html} = live(conn, Routes.note_show_path(conn, :show, note.slug))
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-17 22:30:01 -05:00
|
|
|
assert show_live |> element("a", "edit") |> render_click() =~ "edit"
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert_patch(show_live, Routes.note_show_path(conn, :edit, note.slug))
|
2022-07-25 20:08:40 -04:00
|
|
|
|
|
|
|
assert show_live
|
|
|
|
|> form("#note-form", note: @invalid_attrs)
|
|
|
|
|> render_change() =~ "can't be blank"
|
|
|
|
|
|
|
|
{:ok, _, html} =
|
|
|
|
show_live
|
2022-11-26 14:51:18 -05:00
|
|
|
|> form("#note-form", note: Map.put(@update_attrs, "slug", note.slug))
|
2022-07-25 20:08:40 -04:00
|
|
|
|> render_submit()
|
2022-11-26 14:51:18 -05:00
|
|
|
|> follow_redirect(conn, Routes.note_show_path(conn, :show, note.slug))
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert html =~ "#{note.slug} saved"
|
2022-07-25 20:08:40 -04:00
|
|
|
assert html =~ "some updated content"
|
|
|
|
end
|
2022-11-24 12:43:34 -05:00
|
|
|
|
|
|
|
test "deletes note", %{conn: conn, note: note} do
|
2022-11-26 14:51:18 -05:00
|
|
|
{:ok, show_live, _html} = live(conn, Routes.note_show_path(conn, :show, note.slug))
|
2022-11-24 12:43:34 -05:00
|
|
|
|
|
|
|
{:ok, index_live, _html} =
|
|
|
|
show_live
|
|
|
|
|> element("[data-qa=\"delete-note-#{note.id}\"]")
|
|
|
|
|> render_click()
|
|
|
|
|> follow_redirect(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
|
|
|
refute has_element?(index_live, "#note-#{note.id}")
|
|
|
|
end
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
end
|