-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateFunction.spec.ts
104 lines (97 loc) · 2.59 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
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
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', () => {
const mutedLogger: Logger = {
info: () => {},
error: () => {},
}
let publishedEvents: any[] = []
const topicStub: Dependencies['topic'] = {
name: 'dummy-topic-name',
publish: async (data: Buffer) => {
publishedEvents.push(JSON.parse(data.toString()))
return ''
},
}
const publishJapannetbankNotificationOnEmail = createFunction({
topic: topicStub,
logger: mutedLogger,
})
const dummyContext = {
timestamp: '',
eventId: '',
eventType: '',
}
beforeEach(() => {
publishedEvents = []
})
it('publishes notification', async () => {
const email: EmailData = {
to: [],
cc: [],
replyTo: [],
subject: '【Visaデビット】ご利用代金お引き落としのお知らせ',
from: [],
bodyText: `いつもジャパンネット銀行をご利用いただきありがとうございます。
JNB Visaデビットのご利用代金を普通預金口座よりお引き落としいたしました。
お引落日時:2001/02/03 04:05:06
お引落金額:2,760円
加盟店名:AMAZON CO JP
取引明細番号:1A165002
[...略...]`,
}
await publishJapannetbankNotificationOnEmail(
createValidEvent({
correlationId: 'dummy',
data: email,
}),
dummyContext,
)
expect(publishedEvents).toEqual([
{
correlationId: 'dummy',
data: {
email,
notification: {
type: 'visaWithdrawn',
withdrawnOn: '2001-02-03T04:05:06+09:00',
amount: 2760,
shop: 'AMAZON CO JP',
number: '1A165002',
},
},
},
])
})
it('publishes without notification', async () => {
const email: EmailData = {
to: [],
cc: [],
replyTo: [],
from: [],
}
await publishJapannetbankNotificationOnEmail(
createValidEvent({
correlationId: 'dummy',
data: email,
}),
dummyContext,
)
expect(publishedEvents).toEqual([
{
correlationId: 'dummy',
data: { email },
},
])
})
})
const createValidEvent = (
data: EventData<EmailData>,
): Parameters<PubSubFunction>[0] => {
return {
data: Buffer.from(JSON.stringify(data)).toString('base64'),
attributes: null,
}
}