-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.test.ts
More file actions
52 lines (41 loc) · 1.91 KB
/
index.test.ts
File metadata and controls
52 lines (41 loc) · 1.91 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
import { getBankAccount, InsufficientFundsError, TransferFailedError, SynchronizationFailedError, } from './index';
import lodash from 'lodash';
describe('BankAccount', () => {
test('should create account with initial balance', () => {
const gBA=getBankAccount(10);
expect(gBA["_balance"]).toBe(10);
});
test('should throw InsufficientFundsError error when withdrawing more than balance', () => {
expect((new InsufficientFundsError(10)).toString()).toBe(`Error: Insufficient funds: cannot withdraw more than 10`);
});
test('should throw error when transferring more than balance', () => {
expect((new TransferFailedError).toString()).toBe('Error: Transfer failed');
});
test('should throw error when transferring to the same account', () => {
expect((new SynchronizationFailedError).toString()).toBe('Error: Synchronization failed');
});
test('should deposit money', () => {
const gBAt=getBankAccount(10).deposit(100);
expect(gBAt["_balance"]).toBe(110);
});
test('should withdraw money', () => {
const gBAtd=getBankAccount(100).withdraw(10);
expect(gBAtd["_balance"]).toBe(90);
});
test('should transfer money', () => {
const gBAtd=getBankAccount(100).transfer(10, getBankAccount(100));
expect(gBAtd["_balance"]).toBe(90);
});
test('fetchBalance should return number in case if request did not failed', async () => {
const gBAtd=getBankAccount(100).transfer(10, getBankAccount(100));
expect(gBAtd["_balance"]).toBe(90);
});
test('should set new balance if fetchBalance returned number', async () => {
jest.spyOn(lodash, 'random').mockImplementation(() => 10);
const gBAtd=await getBankAccount(100).fetchBalance();
expect(gBAtd).toBe(10);
});
test('should throw SynchronizationFailedError if fetchBalance returned null', async () => {
expect((new SynchronizationFailedError).toString()).toBe('Error: Synchronization failed');
});
});