@@ -8,30 +8,30 @@ describe('Bank Account', () => {
8
8
expect ( account . balance ) . toEqual ( 0 ) ;
9
9
} ) ;
10
10
11
- xtest ( 'can deposit money ' , ( ) => {
11
+ xtest ( 'Single deposit' , ( ) => {
12
12
const account = new BankAccount ( ) ;
13
13
account . open ( ) ;
14
14
account . deposit ( 100 ) ;
15
15
expect ( account . balance ) . toEqual ( 100 ) ;
16
16
} ) ;
17
17
18
- xtest ( 'can deposit money sequentially ' , ( ) => {
18
+ xtest ( 'Multiple deposits" ' , ( ) => {
19
19
const account = new BankAccount ( ) ;
20
20
account . open ( ) ;
21
21
account . deposit ( 100 ) ;
22
22
account . deposit ( 50 ) ;
23
23
expect ( account . balance ) . toEqual ( 150 ) ;
24
24
} ) ;
25
25
26
- xtest ( 'can withdraw money ' , ( ) => {
26
+ xtest ( 'Withdraw once ' , ( ) => {
27
27
const account = new BankAccount ( ) ;
28
28
account . open ( ) ;
29
29
account . deposit ( 100 ) ;
30
30
account . withdraw ( 50 ) ;
31
31
expect ( account . balance ) . toEqual ( 50 ) ;
32
32
} ) ;
33
33
34
- xtest ( 'can withdraw money sequentially ' , ( ) => {
34
+ xtest ( 'Withdraw twice ' , ( ) => {
35
35
const account = new BankAccount ( ) ;
36
36
account . open ( ) ;
37
37
account . deposit ( 100 ) ;
@@ -40,14 +40,25 @@ describe('Bank Account', () => {
40
40
expect ( account . balance ) . toEqual ( 0 ) ;
41
41
} ) ;
42
42
43
- xtest ( 'checking balance of closed account throws error' , ( ) => {
43
+ xtest ( 'Can do multiple operations sequentially' , ( ) => {
44
+ const account = new BankAccount ( ) ;
45
+ account . open ( ) ;
46
+ account . deposit ( 100 ) ;
47
+ account . deposit ( 110 ) ;
48
+ account . withdraw ( 200 ) ;
49
+ account . deposit ( 60 ) ;
50
+ account . withdraw ( 50 ) ;
51
+ expect ( account . balance ) . toEqual ( 20 ) ;
52
+ } ) ;
53
+
54
+ xtest ( 'Cannot check balance of closed account' , ( ) => {
44
55
const account = new BankAccount ( ) ;
45
56
account . open ( ) ;
46
57
account . close ( ) ;
47
58
expect ( ( ) => account . balance ) . toThrow ( ValueError ) ;
48
59
} ) ;
49
60
50
- xtest ( 'deposit into closed account throws error ' , ( ) => {
61
+ xtest ( 'Cannot deposit into closed account' , ( ) => {
51
62
const account = new BankAccount ( ) ;
52
63
account . open ( ) ;
53
64
account . close ( ) ;
@@ -56,7 +67,14 @@ describe('Bank Account', () => {
56
67
} ) . toThrow ( ValueError ) ;
57
68
} ) ;
58
69
59
- xtest ( 'withdraw from closed account throws error' , ( ) => {
70
+ xtest ( 'Cannot deposit into closed account' , ( ) => {
71
+ const account = new BankAccount ( ) ;
72
+ expect ( ( ) => {
73
+ account . deposit ( 50 ) ;
74
+ } ) . toThrow ( ValueError ) ;
75
+ } ) ;
76
+
77
+ xtest ( 'Cannot withdraw from closed account' , ( ) => {
60
78
const account = new BankAccount ( ) ;
61
79
account . open ( ) ;
62
80
account . close ( ) ;
@@ -65,22 +83,22 @@ describe('Bank Account', () => {
65
83
} ) . toThrow ( ValueError ) ;
66
84
} ) ;
67
85
68
- xtest ( 'close already closed account throws error ' , ( ) => {
86
+ xtest ( 'Cannot close an account that was not opened ' , ( ) => {
69
87
const account = new BankAccount ( ) ;
70
88
expect ( ( ) => {
71
89
account . close ( ) ;
72
90
} ) . toThrow ( ValueError ) ;
73
91
} ) ;
74
92
75
- xtest ( 'open already opened account throws error ' , ( ) => {
93
+ xtest ( 'Cannot open an already opened account' , ( ) => {
76
94
const account = new BankAccount ( ) ;
77
95
account . open ( ) ;
78
96
expect ( ( ) => {
79
97
account . open ( ) ;
80
98
} ) . toThrow ( ValueError ) ;
81
99
} ) ;
82
100
83
- xtest ( 'reopened account does not retain balance' , ( ) => {
101
+ xtest ( 'Reopened account does not retain balance' , ( ) => {
84
102
const account = new BankAccount ( ) ;
85
103
account . open ( ) ;
86
104
account . deposit ( 50 ) ;
@@ -89,7 +107,7 @@ describe('Bank Account', () => {
89
107
expect ( account . balance ) . toEqual ( 0 ) ;
90
108
} ) ;
91
109
92
- xtest ( 'cannot withdraw more than deposited' , ( ) => {
110
+ xtest ( 'Cannot withdraw more than deposited' , ( ) => {
93
111
const account = new BankAccount ( ) ;
94
112
account . open ( ) ;
95
113
account . deposit ( 25 ) ;
@@ -98,7 +116,7 @@ describe('Bank Account', () => {
98
116
} ) . toThrow ( ValueError ) ;
99
117
} ) ;
100
118
101
- xtest ( 'cannot withdraw negative amount ' , ( ) => {
119
+ xtest ( 'Cannot withdraw negative' , ( ) => {
102
120
const account = new BankAccount ( ) ;
103
121
account . open ( ) ;
104
122
account . deposit ( 100 ) ;
@@ -107,15 +125,48 @@ describe('Bank Account', () => {
107
125
} ) . toThrow ( ValueError ) ;
108
126
} ) ;
109
127
110
- xtest ( 'cannot deposit negative amount ' , ( ) => {
128
+ xtest ( 'Cannot deposit negative' , ( ) => {
111
129
const account = new BankAccount ( ) ;
112
130
account . open ( ) ;
113
131
expect ( ( ) => {
114
132
account . deposit ( - 50 ) ;
115
133
} ) . toThrow ( ValueError ) ;
116
134
} ) ;
117
135
118
- xtest ( 'changing balance directly throws error' , ( ) => {
136
+ xtest ( 'Can handle concurrent transactions' , async ( ) => {
137
+ const account = new BankAccount ( ) ;
138
+ account . open ( ) ;
139
+ account . deposit ( 1000 ) ;
140
+
141
+ for ( let i = 0 ; i < 10 ; i ++ ) {
142
+ await adjustBalanceConcurrently ( account ) ;
143
+ expect ( account . balance ) . toEqual ( 1000 ) ;
144
+ }
145
+ } ) ;
146
+
147
+ function adjustBalanceConcurrently ( account ) {
148
+ const random = ( ) => Math . floor ( Math . random ( ) * 10 ) ;
149
+
150
+ const tasks = Array . from (
151
+ { length : 1000 } ,
152
+ ( ) =>
153
+ new Promise ( ( resolve ) => {
154
+ try {
155
+ account . deposit ( 5 ) ;
156
+ setTimeout ( ( ) => {
157
+ account . withdraw ( 5 ) ;
158
+ resolve ( ) ;
159
+ } , random ( ) ) ;
160
+ } catch ( e ) {
161
+ throw new Error ( `Exception should not be thrown: ${ e . message } ` ) ;
162
+ }
163
+ } ) ,
164
+ ) ;
165
+
166
+ return Promise . all ( tasks ) ;
167
+ }
168
+
169
+ xtest ( 'Changing balance directly throws error' , ( ) => {
119
170
const account = new BankAccount ( ) ;
120
171
account . open ( ) ;
121
172
expect ( ( ) => {
0 commit comments