-
Notifications
You must be signed in to change notification settings - Fork 0
/
createFunction.spec.ts
66 lines (62 loc) · 1.75 KB
/
createFunction.spec.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
import { Topic } from '@google-cloud/pubsub'
import { EmailData } from '@suin/email-data'
import { EventData } from '@suin/event-data'
import { PubSubFunction } from '@suin/google-cloud-typed-pubsub-function'
import { createFunction, Dependencies, Logger } from './createFunction'
describe('createFunction', () => {
// @ts-ignore
const mutedLogger: Logger = {
info: () => {},
error: () => {},
}
let publishedEvents: any[] = []
const pubsubStub: Dependencies['pubsub'] = {
topic: () => ({
publish: async (data: Buffer) => {
publishedEvents.push(JSON.parse(data.toString()))
return ''
},
}),
getTopics: async () => [
[
{ name: encodeURIComponent('1@example.com') },
{ name: encodeURIComponent('2@example.com') },
{ name: encodeURIComponent('3@example.com') },
] as Topic[],
],
}
const routeEmail = createFunction({ pubsub: pubsubStub, logger: mutedLogger })
const dummyContext = {
timestamp: '',
eventId: '',
eventType: '',
}
beforeEach(() => {
publishedEvents = []
})
it('publishes event', async () => {
const event: EventData<EmailData> = {
correlationId: 'dummy',
data: {
to: [{ name: '', address: '1@example.com' }],
cc: [
{ name: '', address: '2@example.com' },
{ name: '', address: 'no@example.com' },
],
replyTo: [],
subject: '',
from: [],
},
}
await routeEmail(createValidEvent(event), dummyContext)
expect(publishedEvents).toEqual([event, event])
})
})
const createValidEvent = (
data: EventData<EmailData>,
): Parameters<PubSubFunction>[0] => {
return {
data: Buffer.from(JSON.stringify(data)).toString('base64'),
attributes: null,
}
}