-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateTransaction.unit.ts
97 lines (80 loc) · 2.71 KB
/
CreateTransaction.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { ReturnUnexpectedError } from '../../../../shared/decorators/ReturnUnexpectedError';
process.env.distributeDomainEvents = 'dummy';
import { CreateTransaction } from './CreateTransaction';
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';
import { Transaction } from '../../../../shared/decorators/Transaction';
const chance = new Chance();
let accountRepo: AccountRepoFake, controller: CreateTransaction;
beforeAll(() => {
accountRepo = new AccountRepoFake();
controller = new CreateTransaction(accountRepo, new LambdaInvokerFake());
});
it('creates a transaction', async () => {
const validData = {
userId: UserId.GOOD,
description: `Test: ${chance.sentence()}`,
delta: 30,
};
const result = await controller.execute(getEvent(validData));
expect(result).toMatchObject({
result: {
id: expect.any(String),
},
time: expect.stringMatching(dateFormat),
});
expect(result).not.toMatchObject({
error: expect.anything(),
});
});
it(`fails when userId isn't an uuid`, async () => {
const badData = {
userId: 'not an uuid',
description: `Test: ${chance.sentence()}`,
delta: 30,
};
const result = await controller.execute(getEvent(badData));
expect(result).toMatchObject({
errorType: 'CreateTransactionErrors.UserIdNotUuid',
});
});
it('fails when delta subtracts more than balance', async () => {
const data = {
userId: UserId.GOOD,
description: `Test: ${chance.sentence()}`,
delta: -101, // faked balance is 100
};
const result = await controller.execute(getEvent(data));
expect(result).toMatchObject({
errorType: 'CreateTransactionErrors.InvalidTransaction',
});
});
it('fails when no transactions are found for the user', async () => {
const data = {
userId: UserId.NO_TRANSACTIONS,
description: `Test: ${chance.sentence()}`,
delta: 30,
};
const result = await controller.execute(getEvent(data));
expect(result).toMatchObject({
errorType: 'CreateTransactionErrors.AccountNotFound',
});
});
test('Internal server error when no transactions are found for the user', async () => {
const data = {
userId: UserId.TRANSACTIONS_WITHOUT_ACCOUNT,
description: `Test: ${chance.sentence()}`,
delta: 30,
};
const decorated1 = new Transaction(controller, Object(), [accountRepo]);
const decorated2 = new ReturnUnexpectedError(decorated1);
const result = await decorated2.execute(getEvent(data), Object());
expect(result).toMatchObject({
errorType: 'UnexpectedError',
});
});