forked from nareshbhatia/react-testing-techniques
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cart.ts
72 lines (64 loc) · 2.13 KB
/
Cart.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { OrderItem, OrderItemUtils } from './OrderItem';
import { Product } from './Product';
// ----------------------------------------------------------------------------
// Cart contains a list of OrderItems.
// ----------------------------------------------------------------------------
export interface Cart {
items: Array<OrderItem>;
}
// ----------------------------------------------------------------------------
// Cart utility functions
// These are pure functions with a deterministic output.
// If the cart needs to be modified, a new instance is returned.
// In other words, the Cart is immutable.
// ----------------------------------------------------------------------------
function total(cart: Cart): number {
return OrderItemUtils.totalItems(cart.items);
}
function findItem(cart: Cart, productId: string): OrderItem | undefined {
return cart.items.find((item) => item.productId === productId);
}
function addItem(cart: Cart, item: OrderItem): Cart {
// make a copy of items and add a new one
let newItems = cart.items.slice();
newItems.push(item);
return { ...cart, items: newItems };
}
function deleteItem(cart: Cart, productId: string): Cart {
let newItems = cart.items.filter((item) => item.productId !== productId);
return { ...cart, items: newItems };
}
function setItemQuantity(cart: Cart, productId: string, quantity: number) {
let newItems = cart.items.map((item) =>
item.productId !== productId
? item
: {
productId: item.productId,
productName: item.productName,
price: item.price,
quantity,
}
);
return { ...cart, items: newItems };
}
/**
* If the product already exists in the cart, simply increments the quantity
*/
function addProduct(cart: Cart, product: Product): Cart {
const { id, name, price } = product;
const existingItem = findItem(cart, id);
return existingItem
? setItemQuantity(cart, id, existingItem.quantity + 1)
: addItem(cart, {
productId: id,
productName: name,
price: price,
quantity: 1,
});
}
export const CartUtils = {
total,
addProduct,
deleteItem,
setItemQuantity,
};