-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.test.ts
More file actions
104 lines (82 loc) · 3.18 KB
/
index.test.ts
File metadata and controls
104 lines (82 loc) · 3.18 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
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
import {
getBankAccount,
InsufficientFundsError,
TransferFailedError,
SynchronizationFailedError,
} from '.';
import { random } from 'lodash';
jest.mock('lodash', () => ({
random: jest.fn(),
}));
const initialBalance = 1000;
const balance = 100;
describe('BankAccount', () => {
test('should create account with initial balance', () => {
const account = getBankAccount(initialBalance);
expect(account.getBalance()).toBe(initialBalance);
});
test('should throw InsufficientFundsError error when withdrawing more than balance', () => {
const account = getBankAccount(initialBalance);
expect(() => account.withdraw(10000)).toThrow(InsufficientFundsError);
});
test('should throw error when transferring more than balance', () => {
const account1 = getBankAccount(initialBalance);
const account2 = getBankAccount(initialBalance);
expect(() => account1.transfer(10000, account2)).toThrow(
InsufficientFundsError,
);
});
test('should throw error when transferring to the same account', () => {
const account = getBankAccount(initialBalance);
expect(() => account.transfer(100, account)).toThrow(TransferFailedError);
});
test('should deposit money', () => {
const account = getBankAccount(initialBalance);
const depositAmount = 100;
account.deposit(depositAmount);
expect(account.getBalance()).toBe(initialBalance + depositAmount);
});
test('should withdraw money', () => {
const account = getBankAccount(initialBalance);
const withdrawAmount = 100;
account.withdraw(withdrawAmount);
expect(account.getBalance()).toBe(initialBalance - withdrawAmount);
});
test('should transfer money', () => {
const account1 = getBankAccount(initialBalance);
const account2 = getBankAccount(initialBalance);
const transferAmount = 100;
account1.transfer(transferAmount, account2);
expect(account1.getBalance()).toBe(initialBalance - transferAmount);
expect(account2.getBalance()).toBe(initialBalance + transferAmount);
});
test('fetchBalance should return number in case if request did not failed', async () => {
const account = getBankAccount(initialBalance);
const balance = 100;
const requestSucceeded = 1;
(random as jest.Mock)
.mockReturnValueOnce(balance)
.mockReturnValueOnce(requestSucceeded);
await expect(account.fetchBalance()).resolves.toBe(balance);
});
test('should set new balance if fetchBalance returned number', async () => {
const account = getBankAccount(initialBalance);
const requestSucceeded = 1;
(random as jest.Mock)
.mockReturnValueOnce(balance)
.mockReturnValueOnce(requestSucceeded);
await account.synchronizeBalance();
expect(account.getBalance()).toBe(balance);
});
test('should throw SynchronizationFailedError if fetchBalance returned null', async () => {
const account = getBankAccount(initialBalance);
const requestFailed = 0;
(random as jest.Mock)
.mockReturnValueOnce(balance)
.mockReturnValueOnce(requestFailed);
await expect(account.synchronizeBalance()).rejects.toThrow(
SynchronizationFailedError,
);
expect(account.getBalance()).toBe(initialBalance);
});
});