-
Notifications
You must be signed in to change notification settings - Fork 1
/
StoreEvent.unit.ts
99 lines (89 loc) · 2.35 KB
/
StoreEvent.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
98
99
import { StoreEvent } from './StoreEvent';
import { EventStorage } from '../../../../shared/infra/dynamo/EventStorage';
import { UserCreatedEventDTO } from '../../../users/domain/events/UserCreatedEvent';
import {
TransactionCreatedEventStored,
UserCreatedEventStored,
} from '../../services/IStorage';
import { TransactionCreatedEventDTO } from '../../../accounts/domain/events/TransactionCreatedEvent';
import Chance from 'chance';
const chance = new Chance();
let useCase: StoreEvent,
spy: jest.SpyInstance<
Promise<void>,
[eventStored: UserCreatedEventStored | TransactionCreatedEventStored]
>;
beforeAll(async () => {
const dynamo = new EventStorage('dummy');
spy = jest
.spyOn(dynamo, 'saveEvent')
.mockImplementation(() => new Promise((resolve) => resolve(Object())));
useCase = new StoreEvent(dynamo);
});
beforeEach(async () => {
spy.mockClear();
});
it('stores UserCreatedEvent', async () => {
const event: UserCreatedEventDTO = {
user: {
email: chance.email(),
username: chance.name(),
},
type: 'UserCreatedEvent',
aggregateId: chance.guid(),
dateTimeOccurred: chance.date().toJSON(),
version: chance.natural(),
};
await useCase.execute(event);
const {
user: { email, username },
type,
aggregateId,
dateTimeOccurred,
version,
} = event;
const stored: UserCreatedEventStored = {
type,
version,
aggregateId,
dateTimeOccurred,
username,
email,
};
expect(spy).toHaveBeenCalledWith(stored);
});
it('stores TransactionCreatedEvent', async () => {
const event: TransactionCreatedEventDTO = {
transaction: {
id: chance.guid(),
date: chance.date().toJSON(),
delta: chance.floating({ fixed: 2 }),
description: chance.sentence(),
balance: chance.floating({ fixed: 2 }),
},
type: 'TransactionCreatedEvent',
aggregateId: chance.guid(),
dateTimeOccurred: chance.date().toJSON(),
version: chance.natural(),
};
await useCase.execute(event);
const {
transaction: { id, date, delta, description, balance },
type,
aggregateId,
dateTimeOccurred,
version,
} = event;
const stored: TransactionCreatedEventStored = {
type,
version,
aggregateId,
dateTimeOccurred,
id,
date,
delta,
description,
balance,
};
expect(spy).toHaveBeenCalledWith(stored);
});