-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbdd.BuyAndSell.test.ts
127 lines (106 loc) · 4.1 KB
/
bdd.BuyAndSell.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
import 'react-native';
import { expect } from '@jest/globals';
import { inject, store } from '../src/inject';
import CashBalance from '../src/business/state/CashBalance';
import { Bdd, Feature, FeatureFileReporter, reporter, row, val } from 'easy-bdd-tool-jest';
reporter(new FeatureFileReporter());
const feature = new Feature('Buying and Selling Stocks');
Bdd(feature)
.scenario('Buying stocks.')
.given('The user has 120 dollars in cash-balance.')
.and('IBM price is 30 dollars.')
.and('The user has no IBM stocks.')
.when('The user buys 1 IBM.')
.then('The user now has 1 IBM.')
.and('The cash-balance is now 90 dollars.')
.run(async (ctx) => {
inject({});
await store.availableStocks.loadAvailableStocks();
// Given:
store.portfolio.cashBalance.setAmount(120.00);
const ibm = store.availableStocks.findBySymbol('IBM');
ibm.setCurrentPrice(30.00);
store.portfolio.clearStock('IBM');
// When:
store.portfolio.buy(ibm, 1);
// Then:
expect(store.portfolio.howManyStocks('IBM')).toBe(1);
expect(store.portfolio.cashBalance).toEqual(new CashBalance(90.00));
});
Bdd(feature)
.scenario('Selling stocks.')
.given('The user has 120 dollars in cash-balance.')
.and('The current stock prices are as such:')
.table(
'Available Stocks',
row(val('Ticker', 'AAPL'), val('Price', 50.25)),
row(val('Ticker', 'IBM'), val('Price', 30.00)),
row(val('Ticker', 'GOOG'), val('Price', 60.75)),
)
.and('The user Portfolio contains:')
.table(
'Portfolio',
row(val('Ticker', 'AAPL'), val('Quantity', 5)),
row(val('Ticker', 'IBM'), val('Quantity', 3)),
row(val('Ticker', 'GOOG'), val('Quantity', 12)),
)
.when('The user sells 1 IBM.')
.then('The user now has 2 IBM.')
.and('AAPL is still 5, and GOOG is still 12.')
.and('The cash-balance is now 150 dollars.')
.run(async (ctx) => {
inject({});
await store.availableStocks.loadAvailableStocks();
// Given:
store.portfolio.cashBalance.setAmount(120.00);
// We read and create the info from the "Available Stocks" table:
const availableStocksTable = ctx.table('Available Stocks').rows;
for (const row of availableStocksTable) {
const ticker: string = row.val('Ticker');
const price: number = row.val('Price');
const stock = store.availableStocks.findBySymbol(ticker);
stock.setCurrentPrice(price);
}
// We read and create the info from the "Portfolio" table:
const portfolioTable = ctx.table('Portfolio').rows;
for (const row of portfolioTable) {
const ticker: string = row.val('Ticker');
const quantity: number = row.val('Quantity');
store.portfolio.setStockInPortfolio(ticker, quantity, 100);
}
// When:
const ibm = store.availableStocks.findBySymbol('IBM');
store.portfolio.sell(ibm, 1);
// Then:
expect(store.portfolio.howManyStocks('IBM')).toBe(2);
expect(store.portfolio.howManyStocks('AAPL')).toBe(5);
expect(store.portfolio.howManyStocks('GOOG')).toBe(12);
expect(store.portfolio.cashBalance).toEqual(new CashBalance(150.00));
// The code below shows the alternative hard-coded implementation:
// inject({});
// await store.availableStocks.loadAvailableStocks();
//
// // Given:
// store.portfolio.cashBalance.setAmount(120.00);
//
// const aapl = store.availableStocks.findBySymbol('AAPL');
// const ibm = store.availableStocks.findBySymbol('IBM');
// const goog = store.availableStocks.findBySymbol('GOOG');
//
// aapl.setCurrentPrice(50.25);
// ibm.setCurrentPrice(30.00);
// goog.setCurrentPrice(60.75);
//
// store.portfolio.setStockInPortfolio('AAPL', 5, 100);
// store.portfolio.setStockInPortfolio('IBM', 3, 100);
// store.portfolio.setStockInPortfolio('GOOG', 12, 100);
//
// // When:
// store.portfolio.sell(ibm, 1);
//
// // Then:
// expect(store.portfolio.howManyStocks('IBM')).toBe(2);
// expect(store.portfolio.howManyStocks('AAPL')).toBe(5);
// expect(store.portfolio.howManyStocks('GOOG')).toBe(12);
// expect(store.portfolio.cashBalance).toEqual(new CashBalance(150.00));
});