-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateTransaction.e2e.ts
114 lines (105 loc) · 3.48 KB
/
CreateTransaction.e2e.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import * as dotenv from 'dotenv';
dotenv.config();
import {
AccountRepo,
createUserAndAccount,
deleteUsers,
} from '../../../../shared/utils/repos';
import { Account } from '../../domain/Account';
import { Request } from './CreateTransactionDTOs';
import { MutationCreateTransactionResponse } from '../../../../shared/infra/appsync/schema.graphql';
import Chance from 'chance';
import { AppSyncClient } from '../../../../shared/infra/appsync/AppSyncClient';
import gql from 'graphql-tag';
import {
dateFormat,
deleteItems,
getByPart,
getRandom,
retryDefault,
} from '../../../../shared/utils/test';
import { NotificationTypes } from '../../../notification/domain/NotificationTypes';
import retry from 'async-retry';
import { User } from '../../../users/domain/User';
const appsync = new AppSyncClient();
const chance = new Chance();
// Add all process.env used:
const { StorageTable, NotificationsTable } = process.env;
if (!StorageTable || !NotificationsTable) {
console.log('process.env', process.env);
throw new Error(`Undefined env var!`);
}
let seed: { user: User; account: Account }, seedUserId: string;
beforeAll(async () => {
seed = await createUserAndAccount();
seedUserId = seed.user.id.toString();
});
let auditEvent: Record<string, unknown>;
const notifications: {
type: NotificationTypes;
id: string;
}[] = [];
afterAll(async () => {
await deleteUsers([{ id: seedUserId }]);
await deleteItems(notifications, NotificationsTable);
await deleteItems(
[
{
typeAggregateId: auditEvent.typeAggregateId,
dateTimeOccurred: auditEvent.dateTimeOccurred,
},
],
StorageTable
);
});
test('Create transaction', async () => {
const dto: Request = {
userId: seedUserId,
description: `Test: ${chance.sentence()}`,
delta: getRandom({ min: 0 }),
};
const response = await appsync.send({
query: gql`
mutation ($userId: ID!, $description: String!, $delta: Float!) {
createTransaction(
userId: $userId
description: $description
delta: $delta
) {
id
response_time
}
}
`,
variables: dto,
});
expect(response.status).toBe(200);
const json = (await response.json()) as MutationCreateTransactionResponse;
expect(json.data.createTransaction).toMatchObject({
id: expect.any(String),
response_time: expect.stringMatching(dateFormat),
});
expect(json).not.toMatchObject({
errors: expect.anything(),
});
const account = await AccountRepo.getAccountByUserId(seedUserId);
if (!account) throw new Error(`Account not found for userId ${seedUserId}`);
expect(account.transactions.length).toBe(2); // Initial transaction when seeding with createUserAndAccount and the created one
expect(account.transactions[0].balance.value).toBe(
seed.account.balance().value + dto.delta
);
expect(account.transactions[0].delta.value).toBe(dto.delta);
expect(account.transactions[0].description.value).toBe(dto.description);
expect(account.balance().value).toBe(seed.account.balance().value + dto.delta);
const partValue = `TransactionCreatedEvent#${account.id.toString()}`;
await retry(async () => {
const items = await getByPart('typeAggregateId', partValue, StorageTable);
if (!items) throw Error(`No audit events found for ${partValue}`);
expect(items).toHaveLength(1);
auditEvent = items[0];
}, retryDefault);
notifications.push({
type: NotificationTypes.TransactionCreated,
id: json.data.createTransaction.id,
});
});