-
Notifications
You must be signed in to change notification settings - Fork 36
/
Cart.test.ts
137 lines (121 loc) · 3.16 KB
/
Cart.test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Cart, CartUtils } from './Cart';
import { Product } from './Product';
const product1: Product = {
id: 'apple-macbook-pro',
name: 'MacBook Pro',
description: 'Defy limits and change the world',
manufacturer: 'Apple',
price: 100,
photo: 'https://photos.example.com/1',
};
const product2: Product = {
id: 'apple-imac',
name: 'iMac',
description: 'Take any idea to the next level',
manufacturer: 'Apple',
price: 200,
photo: 'https://photos.example.com/2',
};
const cartQuantity0: Cart = {
items: [],
};
const cartQuantity1: Cart = {
items: [
{
productId: 'apple-macbook-pro',
productName: 'MacBook Pro',
price: 100,
quantity: 1,
},
],
};
const cartQuantity2: Cart = {
items: [
{
productId: 'apple-macbook-pro',
productName: 'MacBook Pro',
price: 100,
quantity: 1,
},
{
productId: 'apple-imac',
productName: 'iMac',
price: 200,
quantity: 1,
},
],
};
const cartQuantity3: Cart = {
items: [
{
productId: 'apple-macbook-pro',
productName: 'MacBook Pro',
price: 100,
quantity: 2,
},
{
productId: 'apple-imac',
productName: 'iMac',
price: 200,
quantity: 1,
},
],
};
const cartQuantity5: Cart = {
items: [
{
productId: 'apple-macbook-pro',
productName: 'MacBook Pro',
price: 100,
quantity: 2,
},
{
productId: 'apple-imac',
productName: 'iMac',
price: 200,
quantity: 3,
},
],
};
describe('CartUtils', () => {
describe('total()', () => {
it('return 0 if cart is empty', () => {
expect(CartUtils.total(cartQuantity0)).toBe(0);
});
it('return the correct total if cart is populated', () => {
expect(CartUtils.total(cartQuantity5)).toBe(800);
});
});
describe('addProduct()', () => {
it('adds new item if product does not exist in the cart', () => {
let cart = CartUtils.addProduct(cartQuantity0, product1);
expect(cart).toEqual(cartQuantity1);
cart = CartUtils.addProduct(cart, product2);
expect(cart).toEqual(cartQuantity2);
});
it('increments the quantity if product already exists in the cart', () => {
let cart = CartUtils.addProduct(cartQuantity0, product1);
cart = CartUtils.addProduct(cart, product2);
cart = CartUtils.addProduct(cart, product1);
expect(cart).toEqual(cartQuantity3);
});
});
describe('deleteItem()', () => {
it('deletes the specified item from the cart', () => {
let cart = CartUtils.addProduct(cartQuantity0, product1);
cart = CartUtils.addProduct(cart, product2);
expect(cart).toEqual(cartQuantity2);
cart = CartUtils.deleteItem(cart, product2.id);
expect(cart).toEqual(cartQuantity1);
});
});
describe('setItemQuantity()', () => {
it('sets quantity to the specified value', () => {
let cart = CartUtils.addProduct(cartQuantity0, product1);
cart = CartUtils.addProduct(cart, product2);
cart = CartUtils.setItemQuantity(cart, product1.id, 2);
cart = CartUtils.setItemQuantity(cart, product2.id, 3);
expect(cart).toEqual(cartQuantity5);
});
});
});