-
Notifications
You must be signed in to change notification settings - Fork 1
/
Transfer.unit.ts
82 lines (70 loc) · 2.34 KB
/
Transfer.unit.ts
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
process.env.distributeDomainEvents = 'dummy';
import { Transfer } from './Transfer';
import { AccountRepoFake, UserId } from '../../repos/AccountRepoFake';
import {
dateFormat,
getAppSyncEvent as getEvent,
} from '../../../../shared/utils/test';
import Chance from 'chance';
import { LambdaInvokerFake } from '../../../../shared/infra/invocation/LambdaInvokerFake';
const chance = new Chance();
let accountRepo, transfer: Transfer;
beforeAll(() => {
accountRepo = new AccountRepoFake();
transfer = new Transfer(accountRepo, new LambdaInvokerFake());
});
test('Transfer', async () => {
const data = {
fromUserId: UserId.GOOD,
toUserId: UserId.GOOD2,
quantity: 100, // Faked balance is 100 so we can't transfer more than that
fromDescription: `Test: ${chance.sentence()}`,
};
const result = await transfer.execute(getEvent(data));
expect(result).toMatchObject({
time: expect.stringMatching(dateFormat),
result: {
fromTransaction: expect.any(String),
toTransaction: expect.any(String),
},
});
expect(result).not.toMatchObject({
error: expect.anything(),
});
});
it('fails when quantity is greater than source/from balance', async () => {
const data = {
fromUserId: UserId.GOOD,
toUserId: UserId.GOOD2,
quantity: 101, // Faked balance is 100
fromDescription: `Test: ${chance.sentence()}`,
};
const result = await transfer.execute(getEvent(data));
expect(result).toMatchObject({
errorType: 'TransferErrors.InvalidTransfer',
});
});
it('fails when quantity is greater than destination/to balance', async () => {
const data = {
fromUserId: UserId.GOOD,
toUserId: UserId.GOOD2,
quantity: -101, // Faked balance is 100, this implies a transfer from toAccount to fromAccount
fromDescription: `Test: ${chance.sentence()}`,
};
const result = await transfer.execute(getEvent(data));
expect(result).toMatchObject({
errorType: 'TransferErrors.InvalidTransfer',
});
});
it('fails when source/from and destination/to accounts are the same', async () => {
const data = {
fromUserId: UserId.GOOD,
toUserId: UserId.GOOD,
quantity: 100, // Faked balance is 100
fromDescription: `Test: ${chance.sentence()}`,
};
const result = await transfer.execute(getEvent(data));
expect(result).toMatchObject({
errorType: 'TransferErrors.SameFromAndTo',
});
});