-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.spec.ts
47 lines (39 loc) · 1.59 KB
/
strategy.spec.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
import { describe, it } from '@jest/globals';
import Card from './Card';
import PaymentService from './PaymentService';
import CreditCardPayment from './CreditCardPayment';
import PayPalUser from './User';
import PayPalPayment from './PayPalPayment';
describe('Behaviours -> Strategy design pattern', () => {
it('Should be able to process an order with a PayPal user', () => {
const payPalUser: PayPalUser = new PayPalUser('John', '1234', 1000);
const strategy: PaymentService = new PaymentService(
new PayPalPayment(payPalUser),
);
expect(strategy.processOrder(10)).toEqual(
`The order has been processed with PayPalPayment`,
);
});
it('Should be able to process an order with a Credit Card', () => {
const creditCard: Card = new Card('374245455400126', 'John', 123, 1000);
const strategy: PaymentService = new PaymentService(
new CreditCardPayment(creditCard),
);
expect(strategy.processOrder(20)).toEqual(
`The order has been processed with CreditCardPayment`,
);
});
it('Should be able to swap the strategy at run-time', () => {
// Processing the order via Credit card
const payPalUser: PayPalUser = new PayPalUser('John', '1234', 1000);
const creditCard: Card = new Card('374245455400136', 'John 1', 123, 1000);
const strategy: PaymentService = new PaymentService(
new CreditCardPayment(creditCard),
);
strategy.setStrategy(new PayPalPayment(payPalUser));
strategy.processOrder(20);
expect(strategy.processOrder(10)).toEqual(
`The order has been processed with PayPalPayment`,
);
});
});