2022-07-25 20:11:08 -04:00
|
|
|
defmodule MemexWeb.ContextLiveTest do
|
|
|
|
use MemexWeb.ConnCase
|
|
|
|
import Phoenix.LiveViewTest
|
2022-11-26 16:36:30 -05:00
|
|
|
import Memex.{ContextsFixtures, NotesFixtures}
|
|
|
|
alias MemexWeb.Endpoint
|
2022-07-25 20:11:08 -04:00
|
|
|
|
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,
|
2022-12-19 22:29:26 -05:00
|
|
|
"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:11:08 -04:00
|
|
|
|
2023-02-04 17:22:06 -05:00
|
|
|
defp create_context(%{current_user: current_user}) do
|
|
|
|
[context: context_fixture(current_user)]
|
2022-07-25 20:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Index" do
|
2022-11-24 12:44:34 -05:00
|
|
|
setup [:register_and_log_in_user, :create_context]
|
2022-07-25 20:11:08 -04:00
|
|
|
|
|
|
|
test "lists all contexts", %{conn: conn, context: context} do
|
|
|
|
{:ok, _index_live, html} = live(conn, Routes.context_index_path(conn, :index))
|
|
|
|
|
2022-11-24 12:44:34 -05:00
|
|
|
assert html =~ "contexts"
|
2022-11-27 21:18:35 -05:00
|
|
|
assert html =~ context.slug
|
2022-07-25 20:11:08 -04:00
|
|
|
end
|
|
|
|
|
2022-12-15 22:33:10 -05:00
|
|
|
test "searches by tag", %{conn: conn} do
|
|
|
|
{:ok, index_live, html} = live(conn, Routes.context_index_path(conn, :index))
|
|
|
|
|
|
|
|
assert html =~ "example-tag"
|
|
|
|
assert index_live |> element("a", "example-tag") |> render_click()
|
|
|
|
assert_patch(index_live, Routes.context_index_path(conn, :search, "example-tag"))
|
|
|
|
end
|
|
|
|
|
2022-07-25 20:11:08 -04:00
|
|
|
test "saves new context", %{conn: conn} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
|
|
|
|
|
2022-11-17 22:30:01 -05:00
|
|
|
assert index_live |> element("a", "new context") |> render_click() =~
|
|
|
|
"new context"
|
2022-07-25 20:11:08 -04:00
|
|
|
|
|
|
|
assert_patch(index_live, Routes.context_index_path(conn, :new))
|
|
|
|
|
|
|
|
assert index_live
|
|
|
|
|> form("#context-form", context: @invalid_attrs)
|
|
|
|
|> render_change() =~ "can't be blank"
|
|
|
|
|
2023-02-04 17:36:27 -05:00
|
|
|
{:ok, _live, html} =
|
2022-07-25 20:11:08 -04:00
|
|
|
index_live
|
|
|
|
|> form("#context-form", context: @create_attrs)
|
|
|
|
|> render_submit()
|
|
|
|
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
|
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert html =~ "#{@create_attrs |> Map.get("slug")} created"
|
2022-11-27 21:18:35 -05:00
|
|
|
assert html =~ "some-slug"
|
2022-07-25 20:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "updates context in listing", %{conn: conn, context: context} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
|
|
|
|
|
2022-11-24 12:44:34 -05:00
|
|
|
assert index_live |> element("[data-qa=\"context-edit-#{context.id}\"]") |> render_click() =~
|
|
|
|
"edit"
|
2022-07-25 20:11:08 -04:00
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert_patch(index_live, Routes.context_index_path(conn, :edit, context.slug))
|
2022-07-25 20:11:08 -04:00
|
|
|
|
|
|
|
assert index_live
|
|
|
|
|> form("#context-form", context: @invalid_attrs)
|
|
|
|
|> render_change() =~ "can't be blank"
|
|
|
|
|
2023-02-04 17:36:27 -05:00
|
|
|
{:ok, _live, html} =
|
2022-07-25 20:11:08 -04:00
|
|
|
index_live
|
|
|
|
|> form("#context-form", context: @update_attrs)
|
|
|
|
|> render_submit()
|
|
|
|
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
|
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert html =~ "#{@update_attrs |> Map.get("slug")} saved"
|
2022-11-27 21:18:35 -05:00
|
|
|
assert html =~ "some-updated-slug"
|
2022-07-25 20:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "deletes context in listing", %{conn: conn, context: context} do
|
|
|
|
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
|
|
|
|
|
2022-11-24 12:44:34 -05:00
|
|
|
assert index_live |> element("[data-qa=\"delete-context-#{context.id}\"]") |> render_click()
|
2022-07-25 20:11:08 -04:00
|
|
|
refute has_element?(index_live, "#context-#{context.id}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-17 22:30:01 -05:00
|
|
|
describe "show" do
|
2022-11-24 12:44:34 -05:00
|
|
|
setup [:register_and_log_in_user, :create_context]
|
2022-07-25 20:11:08 -04:00
|
|
|
|
|
|
|
test "displays context", %{conn: conn, context: context} do
|
2022-11-26 14:51:18 -05:00
|
|
|
{:ok, _show_live, html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
|
2022-07-25 20:11:08 -04:00
|
|
|
|
2022-11-24 17:44:01 -05:00
|
|
|
assert html =~ "context"
|
2022-11-27 21:18:35 -05:00
|
|
|
assert html =~ context.slug
|
2022-07-25 20:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "updates context within modal", %{conn: conn, context: context} do
|
2022-11-26 14:51:18 -05:00
|
|
|
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
|
2022-07-25 20:11:08 -04:00
|
|
|
|
2022-11-24 12:44:34 -05:00
|
|
|
assert show_live |> element("a", "edit") |> render_click() =~ "edit"
|
2022-07-25 20:11:08 -04:00
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert_patch(show_live, Routes.context_show_path(conn, :edit, context.slug))
|
2022-07-25 20:11:08 -04:00
|
|
|
|
2022-12-19 22:29:26 -05:00
|
|
|
html =
|
|
|
|
show_live
|
|
|
|
|> form("#context-form", context: @invalid_attrs)
|
|
|
|
|> render_change()
|
|
|
|
|
|
|
|
assert html =~ "can't be blank"
|
|
|
|
assert html =~ "tags must be comma-delimited"
|
2022-07-25 20:11:08 -04:00
|
|
|
|
2023-02-04 17:36:27 -05:00
|
|
|
{:ok, _live, html} =
|
2022-07-25 20:11:08 -04:00
|
|
|
show_live
|
2022-11-26 14:51:18 -05:00
|
|
|
|> form("#context-form", context: Map.put(@update_attrs, "slug", context.slug))
|
2022-07-25 20:11:08 -04:00
|
|
|
|> render_submit()
|
2022-11-26 14:51:18 -05:00
|
|
|
|> follow_redirect(conn, Routes.context_show_path(conn, :show, context.slug))
|
2022-07-25 20:11:08 -04:00
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
assert html =~ "#{context.slug} saved"
|
2022-11-27 21:18:35 -05:00
|
|
|
assert html =~ "tag2"
|
2022-07-25 20:11:08 -04:00
|
|
|
end
|
2022-11-24 12:44:34 -05:00
|
|
|
|
|
|
|
test "deletes context", %{conn: conn, context: context} do
|
2022-11-26 14:51:18 -05:00
|
|
|
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
|
2022-11-24 12:44:34 -05:00
|
|
|
|
|
|
|
{:ok, index_live, _html} =
|
|
|
|
show_live
|
|
|
|
|> element("[data-qa=\"delete-context-#{context.id}\"]")
|
|
|
|
|> render_click()
|
|
|
|
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
|
|
|
|
|
|
|
|
refute has_element?(index_live, "#context-#{context.id}")
|
|
|
|
end
|
2022-07-25 20:11:08 -04:00
|
|
|
end
|
2022-11-26 16:36:30 -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-11-26 16:36:30 -05:00
|
|
|
|
|
|
|
[
|
|
|
|
note: note,
|
|
|
|
context:
|
2023-02-04 17:22:06 -05:00
|
|
|
context_fixture(
|
|
|
|
%{content: "example with backlink to [[#{note_slug}]] note"},
|
|
|
|
current_user
|
|
|
|
)
|
2022-11-26 16:36:30 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2022-12-15 22:33:10 -05:00
|
|
|
test "searches by tag", %{conn: conn, context: context} do
|
|
|
|
{:ok, show_live, html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
|
|
|
|
|
|
|
|
assert html =~ "example-tag"
|
|
|
|
assert show_live |> element("a", "example-tag") |> render_click()
|
|
|
|
assert_redirect(show_live, Routes.context_index_path(conn, :search, "example-tag"))
|
|
|
|
end
|
|
|
|
|
2022-11-26 16:36:30 -05:00
|
|
|
test "displays context", %{conn: conn, context: context, note: %{slug: note_slug}} do
|
|
|
|
{:ok, show_live, html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
|
|
|
|
|
|
|
|
assert html =~ "context"
|
|
|
|
assert html =~ Routes.note_show_path(Endpoint, :show, note_slug)
|
|
|
|
assert has_element?(show_live, "[data-qa=\"context-note-#{note_slug}\"]")
|
|
|
|
end
|
|
|
|
end
|
2022-07-25 20:11:08 -04:00
|
|
|
end
|