-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEvent.test.ts
More file actions
159 lines (141 loc) · 4.32 KB
/
Event.test.ts
File metadata and controls
159 lines (141 loc) · 4.32 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { PutEventsCommand } from '@aws-sdk/client-eventbridge';
import { promisify } from 'util';
import { EventBridgeClient } from '@aws-sdk/client-eventbridge';
import { mockClient } from 'aws-sdk-client-mock';
import type { Handler } from 'aws-lambda';
import context from 'aws-lambda-mock-context';
import createError from 'http-errors';
import middy from '@middy/core';
import jsonValidator from '@middy/validator';
import { Bus } from './Bus';
import { Event } from './Event';
function invoke(handler: Handler, event = {}) {
return promisify(handler)(event, context({ timeout: 1 }));
}
// Required by aws-lambda-mock-context getRemainingTimeInMillis functionality
jest.useFakeTimers();
describe('Event', () => {
describe('#construct', () => {
const eventBridgeMock = mockClient(EventBridgeClient);
eventBridgeMock
.on(PutEventsCommand)
.resolves({ Entries: [{ EventId: '123456' }] });
const myBus = new Bus({
name: 'test',
// @ts-expect-error Mocking library mocked client is not type compatible with actual client
EventBridge: eventBridgeMock,
});
const schema = {
type: 'object',
properties: {
attribute: { type: 'string' },
numberAttribute: { type: 'number' },
},
additionalProperties: false,
required: ['attribute'],
} as const;
const myEvent = new Event({
name: 'myEvent',
source: 'source',
bus: myBus,
schema,
});
afterAll(() => {
eventBridgeMock.reset();
});
it('should allow publishing an event', async () => {
const eventPayload = {
attribute: 'hello',
numberAttribute: 12,
};
expect(await myEvent.publish(eventPayload)).toHaveProperty('Entries', [
{
EventId: '123456',
DetailType: 'myEvent',
Detail: JSON.stringify(eventPayload),
EventBusName: 'test',
Source: 'source',
},
]);
});
it('should fail with the use of validationMiddleware on wrong event', () => {
const handler: Handler = (_event, _ctx, callback) => callback(null, '5');
const middyfiedHandler = middy(handler).use(
jsonValidator({ inputSchema: myEvent.publishedEventSchema }),
);
expect(
invoke(middyfiedHandler, {
source: myEvent.source,
'detail-type': myEvent.name,
detail: {
otherAttribute: 'hello',
},
}),
).rejects.toEqual(
new createError.BadRequest('Event object failed validation'),
);
expect(
invoke(middyfiedHandler, {
source: 'unexpected source',
'detail-type': myEvent.name,
detail: {
attribute: 'goodbye',
numberAttribute: 23,
},
}),
).rejects.toEqual(
new createError.BadRequest('Event object failed validation'),
);
expect(
invoke(middyfiedHandler, {
source: myEvent.source,
'detail-type': 'unexpected detail type',
detail: {
attribute: 'goodbye',
numberAttribute: 23,
},
}),
).rejects.toEqual(
new createError.BadRequest('Event object failed validation'),
);
});
it('should succeed with the use of validationMiddleware on correct event', () => {
const handler: Handler = (_event, _ctx, callback) =>
callback(null, 'returnValue');
const middyfiedHandler = middy(handler).use(
jsonValidator({ inputSchema: myEvent.publishedEventSchema }),
);
expect(
invoke(middyfiedHandler, {
source: myEvent.source,
'detail-type': myEvent.name,
detail: {
attribute: 'goodbye',
numberAttribute: 23,
},
}),
).resolves.toEqual('returnValue');
});
it('should compute correct pattern', async () => {
expect(myEvent.pattern).toStrictEqual({
'detail-type': ['myEvent'],
source: ['source'],
});
});
it('should create event pattern', () => {
expect(
myEvent.create({
attribute: 'hello',
numberAttribute: 12,
}),
).toEqual({
Source: 'source',
DetailType: 'myEvent',
Detail: JSON.stringify({
attribute: 'hello',
numberAttribute: 12,
}),
});
});
});
});