-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy.test.ts
More file actions
333 lines (277 loc) · 10.4 KB
/
strategy.test.ts
File metadata and controls
333 lines (277 loc) · 10.4 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* STRATEGY PATTERN - UNIT TESTS
*/
import {
PaymentStrategy,
CreditCardPayment,
PayPalPayment,
CryptoPayment,
ShoppingCart,
CartItem,
CreditCardDetails,
PayPalDetails,
CryptoDetails,
} from "./strategy";
describe("Strategy Pattern - Payment System", () => {
// ============================================
// Credit Card Payment Tests
// ============================================
describe("CreditCardPayment", () => {
const validCardDetails: CreditCardDetails = {
cardNumber: "1234567890123456",
cardHolderName: "John Doe",
expiryDate: "12/28",
cvv: "123",
};
test("should validate correct credit card details", () => {
const payment = new CreditCardPayment(validCardDetails);
expect(payment.validatePaymentDetails()).toBe(true);
});
test("should reject invalid card number", () => {
const invalidCard = { ...validCardDetails, cardNumber: "12345" };
const payment = new CreditCardPayment(invalidCard);
expect(payment.validatePaymentDetails()).toBe(false);
});
test("should reject invalid CVV", () => {
const invalidCard = { ...validCardDetails, cvv: "12" };
const payment = new CreditCardPayment(invalidCard);
expect(payment.validatePaymentDetails()).toBe(false);
});
test("should reject expired card", () => {
const expiredCard = { ...validCardDetails, expiryDate: "01/20" };
const payment = new CreditCardPayment(expiredCard);
expect(payment.validatePaymentDetails()).toBe(false);
});
test("should process payment successfully", async () => {
const payment = new CreditCardPayment(validCardDetails);
const transactionId = await payment.processPayment(100);
expect(transactionId).toBeDefined();
expect(transactionId).toContain("CC-");
});
test("should return correct payment method name", () => {
const payment = new CreditCardPayment(validCardDetails);
expect(payment.getPaymentMethodName()).toBe("Credit Card");
});
});
// ============================================
// PayPal Payment Tests
// ============================================
describe("PayPalPayment", () => {
const validPayPalDetails: PayPalDetails = {
email: "test@example.com",
password: "securePass123",
};
test("should validate correct PayPal details", () => {
const payment = new PayPalPayment(validPayPalDetails);
expect(payment.validatePaymentDetails()).toBe(true);
});
test("should reject invalid email", () => {
const invalidDetails = { ...validPayPalDetails, email: "invalid-email" };
const payment = new PayPalPayment(invalidDetails);
expect(payment.validatePaymentDetails()).toBe(false);
});
test("should reject short password", () => {
const invalidDetails = { ...validPayPalDetails, password: "123" };
const payment = new PayPalPayment(invalidDetails);
expect(payment.validatePaymentDetails()).toBe(false);
});
test("should process payment successfully", async () => {
const payment = new PayPalPayment(validPayPalDetails);
const transactionId = await payment.processPayment(200);
expect(transactionId).toBeDefined();
expect(transactionId).toContain("PP-");
});
test("should return correct payment method name", () => {
const payment = new PayPalPayment(validPayPalDetails);
expect(payment.getPaymentMethodName()).toBe("PayPal");
});
});
// ============================================
// Cryptocurrency Payment Tests
// ============================================
describe("CryptoPayment", () => {
const validCryptoDetails: CryptoDetails = {
walletAddress: "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
cryptoType: "BTC",
};
test("should validate correct crypto details", () => {
const payment = new CryptoPayment(validCryptoDetails);
expect(payment.validatePaymentDetails()).toBe(true);
});
test("should reject invalid wallet address", () => {
const invalidDetails = { ...validCryptoDetails, walletAddress: "short" };
const payment = new CryptoPayment(invalidDetails);
expect(payment.validatePaymentDetails()).toBe(false);
});
test("should reject unsupported cryptocurrency", () => {
const invalidDetails = {
...validCryptoDetails,
cryptoType: "DOGE" as any,
};
const payment = new CryptoPayment(invalidDetails);
expect(payment.validatePaymentDetails()).toBe(false);
});
test("should process payment successfully", async () => {
const payment = new CryptoPayment(validCryptoDetails);
const transactionId = await payment.processPayment(300);
expect(transactionId).toBeDefined();
expect(transactionId).toContain("CRYPTO-BTC-");
});
test("should return correct payment method name", () => {
const payment = new CryptoPayment(validCryptoDetails);
expect(payment.getPaymentMethodName()).toBe("Cryptocurrency (BTC)");
});
test("should handle different crypto types", () => {
const ethDetails: CryptoDetails = {
walletAddress: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
cryptoType: "ETH",
};
const payment = new CryptoPayment(ethDetails);
expect(payment.getPaymentMethodName()).toBe("Cryptocurrency (ETH)");
});
});
// ============================================
// Shopping Cart Tests
// ============================================
describe("ShoppingCart", () => {
let cart: ShoppingCart;
const sampleItem: CartItem = {
id: "1",
name: "Test Product",
price: 99.99,
quantity: 1,
};
beforeEach(() => {
cart = new ShoppingCart();
});
test("should add items to cart", () => {
cart.addItem(sampleItem);
expect(cart.getItems()).toHaveLength(1);
expect(cart.getItems()[0]).toEqual(sampleItem);
});
test("should increase quantity when adding same item", () => {
cart.addItem(sampleItem);
cart.addItem(sampleItem);
const items = cart.getItems();
expect(items).toHaveLength(1);
expect(items[0].quantity).toBe(2);
});
test("should remove items from cart", () => {
cart.addItem(sampleItem);
cart.removeItem(sampleItem.id);
expect(cart.getItems()).toHaveLength(0);
});
test("should calculate correct total", () => {
cart.addItem({ id: "1", name: "Item 1", price: 10, quantity: 2 });
cart.addItem({ id: "2", name: "Item 2", price: 20, quantity: 1 });
expect(cart.getTotal()).toBe(40); // (10 * 2) + (20 * 1)
});
test("should set payment strategy", () => {
const strategy = new CreditCardPayment({
cardNumber: "1234567890123456",
cardHolderName: "Test",
expiryDate: "12/28",
cvv: "123",
});
expect(() => cart.setPaymentStrategy(strategy)).not.toThrow();
});
test("should throw error when checking out without payment strategy", async () => {
cart.addItem(sampleItem);
await expect(cart.checkout()).rejects.toThrow(
"No payment method selected"
);
});
test("should throw error when checking out with empty cart", async () => {
const strategy = new CreditCardPayment({
cardNumber: "1234567890123456",
cardHolderName: "Test",
expiryDate: "12/28",
cvv: "123",
});
cart.setPaymentStrategy(strategy);
await expect(cart.checkout()).rejects.toThrow("Cart is empty");
});
test("should successfully checkout with valid payment strategy", async () => {
cart.addItem(sampleItem);
const strategy = new CreditCardPayment({
cardNumber: "1234567890123456",
cardHolderName: "Test",
expiryDate: "12/28",
cvv: "123",
});
cart.setPaymentStrategy(strategy);
const transactionId = await cart.checkout();
expect(transactionId).toBeDefined();
expect(transactionId).toContain("CC-");
expect(cart.getItems()).toHaveLength(0); // Cart should be cleared
});
test("should throw error with invalid payment details", async () => {
cart.addItem(sampleItem);
const strategy = new CreditCardPayment({
cardNumber: "123", // Invalid
cardHolderName: "Test",
expiryDate: "12/28",
cvv: "123",
});
cart.setPaymentStrategy(strategy);
await expect(cart.checkout()).rejects.toThrow("Invalid payment details");
});
});
// ============================================
// Strategy Pattern Integration Tests
// ============================================
describe("Strategy Pattern Integration", () => {
let cart: ShoppingCart;
beforeEach(() => {
cart = new ShoppingCart();
cart.addItem({ id: "1", name: "Product", price: 100, quantity: 1 });
});
test("should switch between different payment strategies", async () => {
// First payment with Credit Card
const creditCard = new CreditCardPayment({
cardNumber: "1234567890123456",
cardHolderName: "Test",
expiryDate: "12/28",
cvv: "123",
});
cart.setPaymentStrategy(creditCard);
const tx1 = await cart.checkout();
expect(tx1).toContain("CC-");
// Add item again and pay with PayPal
cart.addItem({ id: "2", name: "Product 2", price: 50, quantity: 1 });
const paypal = new PayPalPayment({
email: "test@example.com",
password: "password123",
});
cart.setPaymentStrategy(paypal);
const tx2 = await cart.checkout();
expect(tx2).toContain("PP-");
// Verify different transaction IDs
expect(tx1).not.toBe(tx2);
});
test("should work with all three payment strategies", async () => {
const strategies: PaymentStrategy[] = [
new CreditCardPayment({
cardNumber: "1234567890123456",
cardHolderName: "Test",
expiryDate: "12/28",
cvv: "123",
}),
new PayPalPayment({
email: "test@example.com",
password: "password123",
}),
new CryptoPayment({
walletAddress: "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
cryptoType: "BTC",
}),
];
for (const strategy of strategies) {
cart.addItem({ id: "1", name: "Product", price: 100, quantity: 1 });
cart.setPaymentStrategy(strategy);
const transactionId = await cart.checkout();
expect(transactionId).toBeDefined();
}
}, 10000); // 10 second timeout for this longer test
});
});