-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateTotal.test.ts
260 lines (246 loc) · 6.36 KB
/
calculateTotal.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import calculateTotal from './calculateTotal';
describe('calculateTotal', () => {
// default user
it('should calculate total without discounts', () => {
expect(
calculateTotal({
cartItems: { classic: 1, standout: 1, premium: 1 },
pricingRules: [],
}),
).toStrictEqual({ total: 987.97, discountAmount: 0 });
});
// SecondBite
it('should calculate total with 3 for 2 (1 free) discount', () => {
expect(
calculateTotal({
cartItems: { classic: 3, premium: 1 },
pricingRules: [
{
sku: 'classic',
type: 'buyXFreeX',
buyQuantity: 3,
freeQuantity: 1,
},
],
}),
).toStrictEqual({ total: 934.97, discountAmount: 269.99 });
});
// Axil Coffee Roasters
it('should calculate total with fixed discount', () => {
expect(
calculateTotal({
cartItems: { standout: 3, premium: 1 },
pricingRules: [
{
sku: 'standout',
type: 'fixed',
amount: 299.99,
},
],
}),
).toStrictEqual({ total: 1294.96, discountAmount: 69 });
});
// MYER
it('should calculate total with 5 for 4 (1 free) and fixed discount', () => {
expect(
calculateTotal({
cartItems: {
classic: 3,
standout: 5,
premium: 2,
},
pricingRules: [
{
sku: 'standout',
type: 'buyXFreeX',
buyQuantity: 5,
freeQuantity: 1,
},
{
sku: 'premium',
type: 'fixed',
amount: 389.99,
},
{
sku: 'classic',
type: 'buyXDiscountX',
discountAmount: 249.99,
buyAmount: 3,
},
],
}),
).toEqual({ total: 2821.91, discountAmount: 392.99 });
});
// Other tests
it('should calculate total with percentage discount', () => {
expect(
calculateTotal({
cartItems: { standout: 3 },
pricingRules: [
{
sku: 'standout',
type: 'percentage',
percentage: 100,
},
],
}),
).toStrictEqual({ total: 0, discountAmount: 968.97 });
});
it('should calculate total with percentage, fixed, and 10 for 6 (4 free) discount', () => {
expect(
calculateTotal({
cartItems: {
classic: 1,
standout: 1,
premium: 10,
},
pricingRules: [
{
sku: 'classic',
type: 'fixed',
amount: 30,
},
{
sku: 'standout',
type: 'percentage',
percentage: 100,
},
{
sku: 'premium',
type: 'buyXFreeX',
buyQuantity: 10,
freeQuantity: 4,
},
],
}),
).toStrictEqual({ total: 2399.94, discountAmount: 2142.94 });
});
it('should not give any discounts due to buy quantity - buy 3 for 2', () => {
expect(
calculateTotal({
cartItems: { classic: 2 },
pricingRules: [
{
sku: 'classic',
type: 'buyXFreeX',
buyQuantity: 3,
freeQuantity: 1,
},
],
}),
).toStrictEqual({ total: 539.98, discountAmount: 0 });
});
it('should give 10% discount on classic sku', () => {
expect(
calculateTotal({
cartItems: { classic: 1 },
pricingRules: [
{
sku: 'classic',
type: 'percentage',
percentage: 10,
},
],
}),
).toStrictEqual({ total: 242.99, discountAmount: 27 });
});
it('should get classic free when buying standout', () => {
expect(
calculateTotal({
cartItems: { standout: 2, classic: 1 },
pricingRules: [
{
sku: 'classic',
type: 'getYWithX',
xSku: 'standout',
},
],
}),
).toStrictEqual({ total: 645.98, discountAmount: 269.99 });
});
it('should not get any free classic because no standout is added to cart', () => {
expect(
calculateTotal({
cartItems: { classic: 1 },
pricingRules: [
{
sku: 'classic',
type: 'getYWithX',
xSku: 'standout',
},
],
}),
).toStrictEqual({ total: 269.99, discountAmount: 0 });
});
it('should pick the highest percentage discount on top of fixed discount', () => {
expect(
calculateTotal({
cartItems: { classic: 1 },
pricingRules: [
{
sku: 'classic',
type: 'percentage',
percentage: 10,
},
{
sku: 'classic',
type: 'percentage',
percentage: 50,
},
{ sku: 'classic', type: 'fixed', amount: 100 },
],
}),
).toStrictEqual({ total: 50, discountAmount: 219.99 });
});
it('should calculate total with 5 for the price of 4 and get classic free with every standout', () => {
expect(
calculateTotal({
cartItems: { classic: 5, standout: 5 },
pricingRules: [
{
sku: 'classic',
type: 'getYWithX',
xSku: 'standout',
},
{
sku: 'standout',
type: 'buyXFreeX',
buyQuantity: 5,
freeQuantity: 1,
},
],
}),
).toStrictEqual({ total: 1291.96, discountAmount: 1672.94 });
});
it('should use higher percentage discount over 5 for the price of 3', () => {
expect(
calculateTotal({
cartItems: { standout: 5 },
pricingRules: [
{
sku: 'standout',
type: 'buyXFreeX',
buyQuantity: 5,
freeQuantity: 2,
},
{ sku: 'standout', type: 'percentage', percentage: 70 },
],
}),
).toStrictEqual({ total: 484.49, discountAmount: 1130.46 });
});
it('should discount premium ad to $379.99 when buying 4 or more premium ads', () => {
expect(
calculateTotal({
cartItems: { premium: 5 },
pricingRules: [
{
sku: 'premium',
type: 'buyXDiscountX',
discountAmount: 379.99,
buyAmount: 4,
},
],
}),
).toStrictEqual({ total: 1899.95, discountAmount: 75 });
});
});