@@ -8,30 +8,30 @@ describe('Bank Account', () => {
88 expect ( account . balance ) . toEqual ( 0 ) ;
99 } ) ;
1010
11- xtest ( 'can deposit money ' , ( ) => {
11+ xtest ( 'Single deposit' , ( ) => {
1212 const account = new BankAccount ( ) ;
1313 account . open ( ) ;
1414 account . deposit ( 100 ) ;
1515 expect ( account . balance ) . toEqual ( 100 ) ;
1616 } ) ;
1717
18- xtest ( 'can deposit money sequentially ' , ( ) => {
18+ xtest ( 'Multiple deposits" ' , ( ) => {
1919 const account = new BankAccount ( ) ;
2020 account . open ( ) ;
2121 account . deposit ( 100 ) ;
2222 account . deposit ( 50 ) ;
2323 expect ( account . balance ) . toEqual ( 150 ) ;
2424 } ) ;
2525
26- xtest ( 'can withdraw money ' , ( ) => {
26+ xtest ( 'Withdraw once ' , ( ) => {
2727 const account = new BankAccount ( ) ;
2828 account . open ( ) ;
2929 account . deposit ( 100 ) ;
3030 account . withdraw ( 50 ) ;
3131 expect ( account . balance ) . toEqual ( 50 ) ;
3232 } ) ;
3333
34- xtest ( 'can withdraw money sequentially ' , ( ) => {
34+ xtest ( 'Withdraw twice ' , ( ) => {
3535 const account = new BankAccount ( ) ;
3636 account . open ( ) ;
3737 account . deposit ( 100 ) ;
@@ -40,14 +40,25 @@ describe('Bank Account', () => {
4040 expect ( account . balance ) . toEqual ( 0 ) ;
4141 } ) ;
4242
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' , ( ) => {
4455 const account = new BankAccount ( ) ;
4556 account . open ( ) ;
4657 account . close ( ) ;
4758 expect ( ( ) => account . balance ) . toThrow ( ValueError ) ;
4859 } ) ;
4960
50- xtest ( 'deposit into closed account throws error ' , ( ) => {
61+ xtest ( 'Cannot deposit into closed account' , ( ) => {
5162 const account = new BankAccount ( ) ;
5263 account . open ( ) ;
5364 account . close ( ) ;
@@ -56,7 +67,14 @@ describe('Bank Account', () => {
5667 } ) . toThrow ( ValueError ) ;
5768 } ) ;
5869
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' , ( ) => {
6078 const account = new BankAccount ( ) ;
6179 account . open ( ) ;
6280 account . close ( ) ;
@@ -65,22 +83,22 @@ describe('Bank Account', () => {
6583 } ) . toThrow ( ValueError ) ;
6684 } ) ;
6785
68- xtest ( 'close already closed account throws error ' , ( ) => {
86+ xtest ( 'Cannot close an account that was not opened ' , ( ) => {
6987 const account = new BankAccount ( ) ;
7088 expect ( ( ) => {
7189 account . close ( ) ;
7290 } ) . toThrow ( ValueError ) ;
7391 } ) ;
7492
75- xtest ( 'open already opened account throws error ' , ( ) => {
93+ xtest ( 'Cannot open an already opened account' , ( ) => {
7694 const account = new BankAccount ( ) ;
7795 account . open ( ) ;
7896 expect ( ( ) => {
7997 account . open ( ) ;
8098 } ) . toThrow ( ValueError ) ;
8199 } ) ;
82100
83- xtest ( 'reopened account does not retain balance' , ( ) => {
101+ xtest ( 'Reopened account does not retain balance' , ( ) => {
84102 const account = new BankAccount ( ) ;
85103 account . open ( ) ;
86104 account . deposit ( 50 ) ;
@@ -89,7 +107,7 @@ describe('Bank Account', () => {
89107 expect ( account . balance ) . toEqual ( 0 ) ;
90108 } ) ;
91109
92- xtest ( 'cannot withdraw more than deposited' , ( ) => {
110+ xtest ( 'Cannot withdraw more than deposited' , ( ) => {
93111 const account = new BankAccount ( ) ;
94112 account . open ( ) ;
95113 account . deposit ( 25 ) ;
@@ -98,7 +116,7 @@ describe('Bank Account', () => {
98116 } ) . toThrow ( ValueError ) ;
99117 } ) ;
100118
101- xtest ( 'cannot withdraw negative amount ' , ( ) => {
119+ xtest ( 'Cannot withdraw negative' , ( ) => {
102120 const account = new BankAccount ( ) ;
103121 account . open ( ) ;
104122 account . deposit ( 100 ) ;
@@ -107,15 +125,48 @@ describe('Bank Account', () => {
107125 } ) . toThrow ( ValueError ) ;
108126 } ) ;
109127
110- xtest ( 'cannot deposit negative amount ' , ( ) => {
128+ xtest ( 'Cannot deposit negative' , ( ) => {
111129 const account = new BankAccount ( ) ;
112130 account . open ( ) ;
113131 expect ( ( ) => {
114132 account . deposit ( - 50 ) ;
115133 } ) . toThrow ( ValueError ) ;
116134 } ) ;
117135
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' , ( ) => {
119170 const account = new BankAccount ( ) ;
120171 account . open ( ) ;
121172 expect ( ( ) => {
0 commit comments