-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateUserEvents.int.ts
98 lines (85 loc) · 2.71 KB
/
CreateUserEvents.int.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
import * as dotenv from 'dotenv';
dotenv.config();
import setHooks from '../../../../shared/infra/database/sequelize/hooks';
import { LambdaInvokerFake } from '../../../../shared/infra/invocation/LambdaInvokerFake';
import { CreateUser } from './CreateUser';
import {
dateFormat,
getAppSyncEvent as getEvent,
getNewUserDto,
} from '../../../../shared/utils/test';
import {
CreatedUser,
deleteUsers,
UserRepo,
} from '../../../../shared/utils/repos';
import { UserRepoFake } from '../../repos/UserRepoFake';
import { DomainEventBase } from '../../../../shared/domain/events/DomainEventBase';
import { Envelope } from '../../../../shared/core/Envelope';
import { Created } from '../../../../shared/core/Created';
import { IInvoker } from '../../../../shared/infra/invocation/LambdaInvoker';
// Add all process.env used:
const { distributeDomainEvents } = process.env;
if (!distributeDomainEvents) {
console.log('process.env', process.env);
throw new Error(`Undefined env var!`);
}
let createUser: CreateUser,
invokerFake: IInvoker,
spyOnInvoker: jest.SpyInstance<
unknown,
[event: DomainEventBase, handler: string]
>;
beforeAll(() => {
setHooks();
invokerFake = new LambdaInvokerFake();
spyOnInvoker = jest.spyOn(invokerFake, 'invoke');
});
beforeEach(() => {
spyOnInvoker.mockClear();
});
const createdUsers: CreatedUser[] = [];
afterAll(async () => {
await deleteUsers(createdUsers);
});
test('Domain event dispatcher invokes distributeDomainEvents with user data for UserCreatedEvent', async () => {
createUser = new CreateUser(UserRepo, invokerFake);
const newUser = getNewUserDto();
const response = (await createUser.execute(
getEvent(newUser)
)) as Envelope<Created>;
expect(response).toMatchObject({
time: expect.stringMatching(dateFormat),
result: {
id: expect.any(String),
},
});
const invokerIntake = expect.objectContaining({
aggregateId: expect.any(String),
dateTimeOccurred: expect.any(Date),
user: {
username: newUser.username,
email: newUser.email,
},
type: 'UserCreatedEvent',
version: 0,
});
expect(spyOnInvoker).toHaveBeenCalledWith(invokerIntake, distributeDomainEvents);
expect(spyOnInvoker).toBeCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const id = response.result!.id;
createdUsers.push({ id });
});
test(`distributeDomainEvents isn't called when saving to DB fails [createUser]`, async () => {
createUser = new CreateUser(new UserRepoFake(), invokerFake);
const newUser = {
...getNewUserDto(),
username: 'THROW_WHEN_SAVE',
};
try {
await createUser.execute(getEvent(newUser));
} catch {
/*do nothing*/
}
expect(spyOnInvoker).toBeCalledTimes(0);
});