Skip to content

Commit 68f5562

Browse files
committed
Add item controller test for lists
- Add test for item controller managing lists - Fix #168
1 parent a3269b7 commit 68f5562

File tree

7 files changed

+47
-36
lines changed

7 files changed

+47
-36
lines changed

lib/app/item.ex

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ defmodule App.Item do
3131
end
3232

3333
def changeset_with_lists(item, list_ids) do
34-
# get list based on ids
3534
lists = Repo.all(from l in L, where: l.id in ^list_ids)
3635

3736
item
@@ -182,10 +181,8 @@ defmodule App.Item do
182181
accumulate_item_timers(values)
183182
|> Enum.map(fn t ->
184183
Map.put(t, :tags, items_tags[t.id].tags)
185-
# Map.put(t, :lists, items_tags[t.id].lists)
186184
end)
187185
|> Enum.map(fn t ->
188-
# Map.put(t, :tags, items_tags[t.id].tags)
189186
Map.put(t, :lists, items_tags[t.id].lists)
190187
end)
191188
end

lib/app_web/controllers/item_controller.ex

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ defmodule AppWeb.ItemController do
2020
end
2121

2222
def update(conn, %{"id" => id} = params) do
23-
person_id = conn.assigns[:person][:id] || 0
2423
item = Item.get_item!(id)
2524

2625
list_ids =
@@ -29,16 +28,10 @@ defmodule AppWeb.ItemController do
2928
ids -> ids
3029
end
3130

32-
case Item.update_item_with_lists(item, list_ids) do
33-
{:ok, _item} ->
34-
conn
35-
|> put_flash(:info, "Item's list updated successfully.")
36-
|> redirect(to: "/")
37-
38-
{:error, %Ecto.Changeset{} = changeset} ->
39-
lists = List.list_person_lists(person_id) |> Enum.map(&{&1.name, &1.id})
31+
{:ok, _item} = Item.update_item_with_lists(item, list_ids)
4032

41-
render(conn, "edit.html", item: item, lists: lists, changeset: changeset)
42-
end
33+
conn
34+
|> put_flash(:info, "Item's list updated successfully.")
35+
|> redirect(to: "/")
4336
end
4437
end

lib/app_web/router.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule AppWeb.Router do
22
use AppWeb, :router
3-
alias App.{Person, Repo}
3+
alias App.Person
44

55
pipeline :browser do
66
plug :accepts, ["html"]
@@ -52,7 +52,6 @@ defmodule AppWeb.Router do
5252
# add name to their profile for sharing items feature
5353
defp profile_name(conn, _opts) do
5454
person_id = conn.assigns[:person][:id] || 0
55-
# person = Person.get_person!(person_id)
5655

5756
person = Person.get_or_insert(person_id)
5857

priv/repo/migrations/20221005142257_add_list.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule App.Repo.Migrations.AddList do
1919

2020
create table(:lists_items, primary_key: false) do
2121
add(:item_id, references(:items, on_delete: :delete_all))
22-
add(:list_id, references(:tags, on_delete: :delete_all))
22+
add(:list_id, references(:lists, on_delete: :delete_all))
2323

2424
timestamps()
2525
end

test/app/item_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
defmodule App.ItemTest do
22
use App.DataCase, async: false
33
alias App.{Item, Person, Timer}
4-
alias App.List, as: L
54

65
setup [:create_person]
76

test/app/list_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ defmodule App.ListTest do
4242
end
4343

4444
test "get lists from ids" do
45-
{:ok, _list} = List.create_list(@valid_attrs)
46-
assert lists = List.get_lists_from_ids([1, 2, 3])
45+
{:ok, list} = List.create_list(@valid_attrs)
46+
assert lists = List.get_lists_from_ids([list.id])
4747
assert length(lists) == 1
4848
end
4949

test/app_web/controllers/item_controller_test.exs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,58 @@ defmodule AppWeb.ItemControllerTest do
1818
item
1919
end
2020

21-
describe "edit item" do
21+
describe "edit item's list" do
2222
setup [:create_item, :create_list]
2323

24-
test "renders form for editing chosen item's lists", %{
24+
test "renders form for editing item's lists", %{
2525
conn: conn,
26-
list: list,
2726
item: item
2827
} do
2928
conn =
3029
conn
3130
|> assign(:person, %{id: 1})
3231
|> get(Routes.item_path(conn, :edit, item))
3332

34-
assert html_response(conn, 200) =~ "Edit Item's lists"
33+
assert html_response(conn, 200) =~ "Edit Item"
3534
end
3635
end
3736

38-
# describe "update item's list" do
39-
# setup [:create_item, :create_list]
40-
#
41-
# test "redirects to index when data is valid", %{conn: conn, list: list} do
42-
# conn =
43-
# conn
44-
# |> assign(:person, %{id: 1})
45-
# |> put(Routes.list_path(conn, :update, list), list: @update_attrs)
46-
#
47-
# assert redirected_to(conn) == Routes.list_path(conn, :index)
48-
# end
49-
# end
37+
describe "update item's list" do
38+
setup [:create_item, :create_list]
39+
40+
test "Add a list to an item", %{
41+
conn: conn,
42+
list: list,
43+
item: item
44+
} do
45+
update_params = %{item_lists: [list.id]}
46+
47+
conn =
48+
conn
49+
|> assign(:person, %{id: 1})
50+
|> put(Routes.item_path(conn, :update, item), item: update_params)
51+
52+
assert length(Item.get_item!(item.id).lists) == 1
53+
54+
assert redirected_to(conn) == "/"
55+
end
56+
57+
test "Remove list from item", %{
58+
conn: conn,
59+
item: item
60+
} do
61+
update_params = %{item_lists: ""}
62+
63+
conn =
64+
conn
65+
|> assign(:person, %{id: 1})
66+
|> put(Routes.item_path(conn, :update, item), item: update_params)
67+
68+
assert length(Item.get_item!(item.id).lists) == 0
69+
70+
assert redirected_to(conn) == "/"
71+
end
72+
end
5073

5174
defp create_list(_) do
5275
list = fixture(:list)

0 commit comments

Comments
 (0)