2022-07-25 20:08:40 -04:00
|
|
|
defmodule MemexWeb.NoteLiveTest do
|
|
|
|
use MemexWeb.ConnCase
|
|
|
|
|
|
|
|
import Phoenix.LiveViewTest
|
2023-03-22 22:08:37 -04:00
|
|
|
import Memex.Fixtures
|
2022-12-15 22:43:18 -05:00
|
|
|
alias MemexWeb.Endpoint
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
@create_attrs %{
|
2023-03-22 22:08:37 -04:00
|
|
|
content: "some content",
|
|
|
|
tags_string: "tag1",
|
|
|
|
slug: "some-slug",
|
|
|
|
visibility: :public
|
2022-11-17 22:38:52 -05:00
|
|
|
}
|
2022-07-25 22:05:54 -04:00
|
|
|
@update_attrs %{
|
2023-03-22 22:08:37 -04:00
|
|
|
content: "some updated content",
|
|
|
|
tags_string: "tag1,tag2",
|
|
|
|
slug: "some-updated-slug",
|
|
|
|
visibility: :private
|
2022-11-17 22:38:52 -05:00
|
|
|
}
|
|
|
|
@invalid_attrs %{
|
2023-03-22 22:08:37 -04:00
|
|
|
content: nil,
|
|
|
|
tags_string: " ",
|
|
|
|
slug: nil,
|
|
|
|
visibility: nil
|
2022-07-25 22:05:54 -04:00
|
|
|
}
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2023-02-04 17:22:06 -05:00
|
|
|
defp create_note(%{current_user: current_user}) do
|
|
|
|
[note: note_fixture(current_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-11-27 21:18:35 -05:00
|
|
|
assert html =~ note.slug
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
2023-03-22 22:08:37 -04:00
|
|
|
test "searches by tag", %{conn: conn, note: %{tags: [tag]}} do
|
2022-12-15 22:33:10 -05:00
|
|
|
{:ok, index_live, html} = live(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2023-03-22 22:08:37 -04:00
|
|
|
assert html =~ tag
|
|
|
|
assert index_live |> element("a", tag) |> render_click()
|
|
|
|
assert_patch(index_live, Routes.note_index_path(conn, :search, tag))
|
2022-12-15 22:33:10 -05:00
|
|
|
end
|
|
|
|
|
2022-07-25 20:08:40 -04:00
|
|
|
test "saves new note", %{conn: conn} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
|
2023-03-22 22:08:37 -04: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))
|
|
|
|
|
2022-12-19 22:29:26 -05:00
|
|
|
html =
|
|
|
|
index_live
|
2023-03-22 22:08:37 -04:00
|
|
|
|> form("#note-form")
|
|
|
|
|> render_change(note: @invalid_attrs)
|
2022-12-19 22:29:26 -05:00
|
|
|
|
|
|
|
assert html =~ "can't be blank"
|
|
|
|
assert html =~ "tags must be comma-delimited"
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2023-02-04 17:36:27 -05:00
|
|
|
{:ok, _live, html} =
|
2022-07-25 20:08:40 -04:00
|
|
|
index_live
|
2023-03-22 22:08:37 -04:00
|
|
|
|> form("#note-form")
|
|
|
|
|> render_submit(note: @create_attrs)
|
2022-07-25 20:08:40 -04:00
|
|
|
|> follow_redirect(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2023-03-22 22:08:37 -04:00
|
|
|
assert html =~ "#{@create_attrs.slug} created"
|
|
|
|
assert html =~ @create_attrs.slug
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "updates note in listing", %{conn: conn, note: note} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2023-03-14 23:51:50 -04:00
|
|
|
assert index_live |> element(~s/a[aria-label="edit #{note.slug}"]/) |> 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
|
2023-03-22 22:08:37 -04:00
|
|
|
|> form("#note-form")
|
|
|
|
|> render_change(note: @invalid_attrs) =~ "can't be blank"
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2023-02-04 17:36:27 -05:00
|
|
|
{:ok, _live, html} =
|
2022-07-25 20:08:40 -04:00
|
|
|
index_live
|
2023-03-22 22:08:37 -04:00
|
|
|
|> form("#note-form")
|
|
|
|
|> render_submit(note: @update_attrs)
|
2022-07-25 20:08:40 -04:00
|
|
|
|> follow_redirect(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2023-03-22 22:08:37 -04:00
|
|
|
assert html =~ "#{@update_attrs.slug} saved"
|
|
|
|
assert html =~ @update_attrs.slug
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "deletes note in listing", %{conn: conn, note: note} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
|
|
|
|
|
2023-03-14 23:51:50 -04:00
|
|
|
assert index_live |> element(~s/a[aria-label="delete #{note.slug}"]/) |> 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-11-27 21:18:35 -05:00
|
|
|
assert html =~ note.slug
|
2022-07-25 20:08:40 -04:00
|
|
|
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-11-17 22:30:01 -05:00
|
|
|
assert show_live |> element("a", "edit") |> render_click() =~ "edit"
|
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
|
2023-03-22 22:08:37 -04:00
|
|
|
|> form("#note-form")
|
|
|
|
|> render_change(note: @invalid_attrs) =~ "can't be blank"
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2023-02-04 17:36:27 -05:00
|
|
|
{:ok, _live, html} =
|
2022-07-25 20:08:40 -04:00
|
|
|
show_live
|
2023-03-22 22:08:37 -04:00
|
|
|
|> form("#note-form")
|
|
|
|
|> render_submit(note: @update_attrs |> Map.put(:slug, note.slug))
|
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"
|
2023-03-22 22:08:37 -04:00
|
|
|
assert html =~ note.slug
|
2022-07-25 20:08:40 -04:00
|
|
|
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
|
2023-03-14 23:51:50 -04:00
|
|
|
|> element(~s/button[aria-label="delete #{note.slug}"]/)
|
2022-11-24 12:43:34 -05:00
|
|
|
|> 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
|
2022-12-15 22:43:18 -05:00
|
|
|
|
|
|
|
describe "show with note" do
|
|
|
|
setup [:register_and_log_in_user]
|
|
|
|
|
2023-02-04 17:22:06 -05:00
|
|
|
setup %{current_user: current_user} do
|
|
|
|
%{slug: note_slug} = note = note_fixture(current_user)
|
2022-12-15 22:43:18 -05:00
|
|
|
|
|
|
|
[
|
|
|
|
note: note,
|
|
|
|
backlinked_note:
|
2023-02-04 17:22:06 -05:00
|
|
|
note_fixture(%{content: "example with backlink to [[#{note_slug}]] note"}, current_user)
|
2022-12-15 22:43:18 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2023-03-22 22:08:37 -04:00
|
|
|
test "searches by tag", %{conn: conn, note: %{tags: [tag]} = note} do
|
2022-12-15 22:33:10 -05:00
|
|
|
{:ok, show_live, html} = live(conn, Routes.note_show_path(conn, :show, note.slug))
|
|
|
|
|
2023-03-22 22:08:37 -04:00
|
|
|
assert html =~ tag
|
|
|
|
assert show_live |> element("a", tag) |> render_click()
|
|
|
|
assert_redirect(show_live, Routes.note_index_path(conn, :search, tag))
|
2022-12-15 22:33:10 -05:00
|
|
|
end
|
|
|
|
|
2022-12-15 22:43:18 -05:00
|
|
|
test "displays context", %{
|
|
|
|
conn: conn,
|
|
|
|
backlinked_note: %{slug: backlinked_note_slug},
|
|
|
|
note: %{slug: note_slug}
|
|
|
|
} do
|
|
|
|
{:ok, show_live, html} =
|
|
|
|
live(conn, Routes.note_show_path(conn, :show, backlinked_note_slug))
|
|
|
|
|
|
|
|
assert html =~ "context"
|
|
|
|
assert html =~ Routes.note_show_path(Endpoint, :show, note_slug)
|
2023-03-14 23:51:50 -04:00
|
|
|
assert has_element?(show_live, "a", note_slug)
|
2022-12-15 22:43:18 -05:00
|
|
|
end
|
|
|
|
end
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|