Skip to content

Commit

Permalink
added context tests for the cart module
Browse files Browse the repository at this point in the history
  • Loading branch information
MICHAELMUNAVU83 committed Nov 16, 2023
1 parent e0af9fe commit 63bdb46
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
7 changes: 3 additions & 4 deletions lib/elixir_conf_africa/cart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule ElixirConfAfrica.Cart do
cart |> Enum.map(fn cart_item -> cart_item.id end)
end

def add_quantity_to_cart_item(cart_item, quantity) do
def increase_cart_item_quantity(cart_item, quantity) do
cart_item
|> Map.put(:quantity, quantity)
end
Expand Down Expand Up @@ -47,13 +47,12 @@ defmodule ElixirConfAfrica.Cart do
def add_quantity_to_cart_item(cart, cart_item, quantity) do
case cart |> Enum.filter(fn item -> item.id == cart_item.id end) do
[] ->
cart_item
|> Map.put(:quantity, quantity)
increase_cart_item_quantity(cart_item, quantity)

_ ->
cart
|> Enum.filter(fn item -> item.id == cart_item.id end)
|> Enum.map(fn item -> add_quantity_to_cart_item(item, item.quantity + 1) end)
|> Enum.map(fn item -> increase_cart_item_quantity(item, item.quantity + 1) end)
|> List.first()
end
end
Expand Down
51 changes: 51 additions & 0 deletions test/elixir_conf_africa/cart_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule ElixirConfAfrica.CartTest do
use ElixirConfAfrica.DataCase
alias ElixirConfAfrica.Cart

describe "Cart Functionaity" do
import ElixirConfAfrica.Factory

setup do
event = insert!(:elixir_conf_event)
ticket_type = insert!(:elixir_conf_ticket_type, event_id: event.id)

[ticket_type: ticket_type]
end

test "cart_list_ids/1 returns all ids from the carts in a list" do
assert Cart.cart_list_ids([%{id: 1}, %{id: 2}]) == [1, 2]
end

test "increase_cart_item_quantity/2 increases the quantity of a cart item" do
assert Cart.increase_cart_item_quantity(%{quantity: 1}, 2) == %{quantity: 2}
end

test "add quantity to cart_item/3 returns the cart item with an increased quantity " do
assert Cart.add_quantity_to_cart_item([%{id: 1, quantity: 1}], %{id: 1, quantity: 1}, 2) ==
%{id: 1, quantity: 2}

assert Cart.add_quantity_to_cart_item([%{id: 1, quantity: 1}], %{id: 2}, 1) ==
%{id: 2, quantity: 1}
end

test "get_updated_cart/3 returns the updated cart" do
assert Cart.get_updated_cart("in-cart", %{id: 1, quantity: 1}, [%{id: 1, quantity: 1}]) ==
[%{id: 1, quantity: 2}]

assert Cart.get_updated_cart("out-of-cart", %{id: 2}, [%{id: 1, quantity: 1}]) ==
[%{id: 2, quantity: 1}, %{id: 1, quantity: 1}]
end

test "add_to_cart/2 adds an item to the cart", %{ticket_type: ticket_type} do
assert Cart.add_to_cart([%{id: 1, quantity: 1}, %{id: 2, quantity: 1}], 1) ==
[%{id: 2, quantity: 1}, %{id: 1, quantity: 2}]

assert Cart.add_to_cart([%{id: 1, quantity: 1}, %{id: 2, quantity: 1}], ticket_type.id) ==
[
ticket_type |> Map.put(:quantity, 1),
%{id: 1, quantity: 1},
%{id: 2, quantity: 1}
]
end
end
end
2 changes: 1 addition & 1 deletion test/elixir_conf_africa/events_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule ElixirConfAfrica.EventsTest do
alias ElixirConfAfrica.Events.Event

@invalid_attrs %{
name: nil,
name: nil,
description: nil,
location: nil,
event_type: nil,
Expand Down

0 comments on commit 63bdb46

Please sign in to comment.