add notes

This commit is contained in:
2022-07-25 20:08:40 -04:00
parent 1a423f703b
commit 63fca7ff6a
14 changed files with 607 additions and 12 deletions

65
test/memex/notes_test.exs Normal file
View File

@ -0,0 +1,65 @@
defmodule Memex.NotesTest do
use Memex.DataCase
alias Memex.Notes
describe "notes" do
alias Memex.Notes.Note
import Memex.NotesFixtures
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
test "list_notes/0 returns all notes" do
note = note_fixture()
assert Notes.list_notes() == [note]
end
test "get_note!/1 returns the note with given id" do
note = note_fixture()
assert Notes.get_note!(note.id) == note
end
test "create_note/1 with valid data creates a note" do
valid_attrs = %{content: "some content", tag: [], title: "some title", visibility: :public}
assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs)
assert note.content == "some content"
assert note.tag == []
assert note.title == "some title"
assert note.visibility == :public
end
test "create_note/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs)
end
test "update_note/2 with valid data updates the note" do
note = note_fixture()
update_attrs = %{content: "some updated content", tag: [], title: "some updated title", visibility: :private}
assert {:ok, %Note{} = note} = Notes.update_note(note, update_attrs)
assert note.content == "some updated content"
assert note.tag == []
assert note.title == "some updated title"
assert note.visibility == :private
end
test "update_note/2 with invalid data returns error changeset" do
note = note_fixture()
assert {:error, %Ecto.Changeset{}} = Notes.update_note(note, @invalid_attrs)
assert note == Notes.get_note!(note.id)
end
test "delete_note/1 deletes the note" do
note = note_fixture()
assert {:ok, %Note{}} = Notes.delete_note(note)
assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id) end
end
test "change_note/1 returns a note changeset" do
note = note_fixture()
assert %Ecto.Changeset{} = Notes.change_note(note)
end
end
end

View File

@ -0,0 +1,110 @@
defmodule MemexWeb.NoteLiveTest do
use MemexWeb.ConnCase
import Phoenix.LiveViewTest
import Memex.NotesFixtures
@create_attrs %{content: "some content", tag: [], title: "some title", visibility: :public}
@update_attrs %{content: "some updated content", tag: [], title: "some updated title", visibility: :private}
@invalid_attrs %{content: nil, tag: [], title: nil, visibility: nil}
defp create_note(_) do
note = note_fixture()
%{note: note}
end
describe "Index" do
setup [:create_note]
test "lists all notes", %{conn: conn, note: note} do
{:ok, _index_live, html} = live(conn, Routes.note_index_path(conn, :index))
assert html =~ "Listing Notes"
assert html =~ note.content
end
test "saves new note", %{conn: conn} do
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
assert index_live |> element("a", "New Note") |> render_click() =~
"New Note"
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))
assert html =~ "Note created successfully"
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))
assert index_live |> element("#note-#{note.id} a", "Edit") |> render_click() =~
"Edit Note"
assert_patch(index_live, Routes.note_index_path(conn, :edit, note))
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))
assert html =~ "Note updated successfully"
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))
assert index_live |> element("#note-#{note.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#note-#{note.id}")
end
end
describe "Show" do
setup [:create_note]
test "displays note", %{conn: conn, note: note} do
{:ok, _show_live, html} = live(conn, Routes.note_show_path(conn, :show, note))
assert html =~ "Show Note"
assert html =~ note.content
end
test "updates note within modal", %{conn: conn, note: note} do
{:ok, show_live, _html} = live(conn, Routes.note_show_path(conn, :show, note))
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Note"
assert_patch(show_live, Routes.note_show_path(conn, :edit, note))
assert show_live
|> form("#note-form", note: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
show_live
|> form("#note-form", note: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.note_show_path(conn, :show, note))
assert html =~ "Note updated successfully"
assert html =~ "some updated content"
end
end
end

View File

@ -0,0 +1,23 @@
defmodule Memex.NotesFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Memex.Notes` context.
"""
@doc """
Generate a note.
"""
def note_fixture(attrs \\ %{}) do
{:ok, note} =
attrs
|> Enum.into(%{
content: "some content",
tag: [],
title: "some title",
visibility: :public
})
|> Memex.Notes.create_note()
note
end
end