add contexts

This commit is contained in:
2022-07-25 20:11:08 -04:00
parent 63fca7ff6a
commit 3347b26256
13 changed files with 587 additions and 0 deletions

View File

@ -0,0 +1,65 @@
defmodule Memex.ContextsTest do
use Memex.DataCase
alias Memex.Contexts
describe "contexts" do
alias Memex.Contexts.Context
import Memex.ContextsFixtures
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
test "list_contexts/0 returns all contexts" do
context = context_fixture()
assert Contexts.list_contexts() == [context]
end
test "get_context!/1 returns the context with given id" do
context = context_fixture()
assert Contexts.get_context!(context.id) == context
end
test "create_context/1 with valid data creates a context" do
valid_attrs = %{content: "some content", tag: [], title: "some title", visibility: :public}
assert {:ok, %Context{} = context} = Contexts.create_context(valid_attrs)
assert context.content == "some content"
assert context.tag == []
assert context.title == "some title"
assert context.visibility == :public
end
test "create_context/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Contexts.create_context(@invalid_attrs)
end
test "update_context/2 with valid data updates the context" do
context = context_fixture()
update_attrs = %{content: "some updated content", tag: [], title: "some updated title", visibility: :private}
assert {:ok, %Context{} = context} = Contexts.update_context(context, update_attrs)
assert context.content == "some updated content"
assert context.tag == []
assert context.title == "some updated title"
assert context.visibility == :private
end
test "update_context/2 with invalid data returns error changeset" do
context = context_fixture()
assert {:error, %Ecto.Changeset{}} = Contexts.update_context(context, @invalid_attrs)
assert context == Contexts.get_context!(context.id)
end
test "delete_context/1 deletes the context" do
context = context_fixture()
assert {:ok, %Context{}} = Contexts.delete_context(context)
assert_raise Ecto.NoResultsError, fn -> Contexts.get_context!(context.id) end
end
test "change_context/1 returns a context changeset" do
context = context_fixture()
assert %Ecto.Changeset{} = Contexts.change_context(context)
end
end
end

View File

@ -0,0 +1,110 @@
defmodule MemexWeb.ContextLiveTest do
use MemexWeb.ConnCase
import Phoenix.LiveViewTest
import Memex.ContextsFixtures
@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_context(_) do
context = context_fixture()
%{context: context}
end
describe "Index" do
setup [:create_context]
test "lists all contexts", %{conn: conn, context: context} do
{:ok, _index_live, html} = live(conn, Routes.context_index_path(conn, :index))
assert html =~ "Listing Contexts"
assert html =~ context.content
end
test "saves new context", %{conn: conn} do
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
assert index_live |> element("a", "New Context") |> render_click() =~
"New Context"
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"
{:ok, _, html} =
index_live
|> form("#context-form", context: @create_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
assert html =~ "Context created successfully"
assert html =~ "some content"
end
test "updates context in listing", %{conn: conn, context: context} do
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
assert index_live |> element("#context-#{context.id} a", "Edit") |> render_click() =~
"Edit Context"
assert_patch(index_live, Routes.context_index_path(conn, :edit, context))
assert index_live
|> form("#context-form", context: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
index_live
|> form("#context-form", context: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
assert html =~ "Context updated successfully"
assert html =~ "some updated content"
end
test "deletes context in listing", %{conn: conn, context: context} do
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
assert index_live |> element("#context-#{context.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#context-#{context.id}")
end
end
describe "Show" do
setup [:create_context]
test "displays context", %{conn: conn, context: context} do
{:ok, _show_live, html} = live(conn, Routes.context_show_path(conn, :show, context))
assert html =~ "Show Context"
assert html =~ context.content
end
test "updates context within modal", %{conn: conn, context: context} do
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context))
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Context"
assert_patch(show_live, Routes.context_show_path(conn, :edit, context))
assert show_live
|> form("#context-form", context: @invalid_attrs)
|> render_change() =~ "can't be blank"
{:ok, _, html} =
show_live
|> form("#context-form", context: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.context_show_path(conn, :show, context))
assert html =~ "Context updated successfully"
assert html =~ "some updated content"
end
end
end

View File

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