-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.test.js
More file actions
75 lines (66 loc) · 3.23 KB
/
Account.test.js
File metadata and controls
75 lines (66 loc) · 3.23 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
const Account = require('./Account');
const Deposit = require('./Deposit');
const Withdrawal = require('./Withdrawal');
const Dates = require('./Dates');
describe('Account', () => {
it('should show the starting balance as zero', () => {
const account = new Account();
expect(account.balance).toEqual(0);
});
it('should add the deposited amount to the balance', () => {
const account = new Account(0);
const deposit1 = new Deposit(200);
account.addDeposit(deposit1);
expect(account.balance).toEqual(200);
const deposit2 = new Deposit(300);
account.addDeposit(deposit2);
expect(account.balance).toEqual(500);
});
it('should subtract the withdrawn amount from the balance', () => {
const account = new Account(0);
const deposit = new Deposit(1000);
account.addDeposit(deposit);
const withdrawal = new Withdrawal(100);
account.subtractWithdrawal(withdrawal);
expect(account.balance).toEqual(900);
});
test('should throw error if the balance is less than the withrawal amount', () => {
const account = new Account(100);
// const withdrawal = new Withdrawal(200);
// const date = new Date().toLocaleDateString('en-gb');
// const account = new Account(0);
// const deposit = new Deposit(200);
// account.addDeposit(deposit);
// expect(account.subtractWithdrawal(200)).toThrow('Insufficient funds');
expect(() => account.subtractWithdrawal(200)).toThrow('Insufficient funds');
});
test('should show the date of the deposit in the correct format', () => {
const date = new Date().toLocaleDateString('en-gb');
const account = new Account(0);
const deposit = new Deposit(200);
account.addDeposit(deposit);
expect(account.transactions).toEqual([{ balance: 200, credit: "", date: `${date}`, debit: 200 }]);
});
test('should show the date of the withdrawal in the correct format', () => {
const date = new Date().toLocaleDateString('en-gb');
const account = new Account(0);
const deposit = new Deposit(300);
account.addDeposit(deposit);
const withdrawal = new Withdrawal(150);
account.subtractWithdrawal(withdrawal);
expect(account.transactions[0]).toEqual({ balance: 300, credit: "", date: `${date}`, debit: 300 });
expect(account.transactions[1]).toEqual({ balance: 150, credit: 150, date: `${date}`, debit: "" });
expect(account.transactions).toEqual([{ balance: 300, credit: "", date: `${date}`, debit: 300 }, {balance: 150, credit: 150, date: `${date}`, debit: ""}]);
});
test('should print the statement in the correct format', () => {
const date = new Date().toLocaleDateString('en-gb');
const account = new Account(0);
const deposit = new Deposit(1000);
account.addDeposit(deposit);
const withdrawal = new Withdrawal(200);
account.subtractWithdrawal(withdrawal);
// account.printStatement();
const result = `date || credit || debit || balance\n${date} || || 1000 || 1000\n${date} || 200 || || 800\n`
expect(account.printStatement()).toEqual(result);
});
})