-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetAccountByUserId.e2e.ts
72 lines (66 loc) · 2.15 KB
/
GetAccountByUserId.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
import * as dotenv from 'dotenv';
dotenv.config();
import {
AccountRepo,
createUserAndAccount,
deleteUsers,
} from '../../../../shared/utils/repos';
import { Account } from '../../domain/Account';
import { AppSyncClient } from '../../../../shared/infra/appsync/AppSyncClient';
import { QueryGetAccountByUserIdResponse } from '../../../../shared/infra/appsync/schema.graphql';
import gql from 'graphql-tag';
import { User } from '../../../users/domain/User';
import { dateFormat } from '../../../../shared/utils/test';
const appsync = new AppSyncClient();
let seed: { user: User; account: Account }, seedUserId: string;
beforeAll(async () => {
seed = await createUserAndAccount();
seedUserId = seed.user.id.toString();
});
afterAll(async () => {
await deleteUsers([{ id: seedUserId }]);
});
it('gets an account', async () => {
const response = await appsync.send({
query: gql`
query ($userId: ID!) {
getAccountByUserId(userId: $userId) {
balance
active
transactions {
balance
delta
date
}
response_time
}
}
`,
variables: { userId: seedUserId },
});
expect(response.status).toBe(200);
const json = (await response.json()) as QueryGetAccountByUserIdResponse;
const { account } = seed;
const { transactions } = account;
expect(transactions).toHaveLength(1);
const { balance, delta, date } = transactions[0];
expect(json.data.getAccountByUserId).toMatchObject({
balance: account.balance().value,
active: account.active,
transactions: expect.arrayContaining([
expect.objectContaining({
balance: balance.value,
delta: delta.value,
date: date.toJSON(),
}),
]),
response_time: expect.stringMatching(dateFormat),
});
expect(json).not.toMatchObject({
errors: expect.anything(),
});
const accountDB = await AccountRepo.getAccountByUserId(seedUserId);
if (!accountDB) throw new Error(`Account not found for userId ${seedUserId}`);
expect(accountDB.transactions.length).toBe(1); // Initial transaction when seeding
expect(accountDB.transactions[0].balance.value).toBe(0);
});