-
Notifications
You must be signed in to change notification settings - Fork 0
/
amplitudePlugin.spec.ts
181 lines (155 loc) · 4.55 KB
/
amplitudePlugin.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import * as amplitude from '@amplitude/analytics-node'
import type { BaseEvent, EnrichmentPlugin, Event, Result } from '@amplitude/analytics-types'
import type { FastifyInstance } from 'fastify'
import fastify from 'fastify'
import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest'
import { amplitudePlugin } from './amplitudePlugin'
describe('amplitudePlugin', () => {
let app: FastifyInstance
beforeEach(() => {
app = fastify()
})
afterEach(async () => {
await app.close()
})
it('skips initialization if isEnabled is false', async () => {
// Given
const initSpy = vitest.spyOn(amplitude, 'init')
// When
await app.register(amplitudePlugin, {
isEnabled: false,
})
// Then
expect(initSpy).not.toHaveBeenCalled()
})
it('throws error if is enabled and api key is not defined', async () => {
// Given
const initSpy = vitest.spyOn(amplitude, 'init')
// When
let error = null
try {
await app.register(amplitudePlugin, {
isEnabled: true,
})
} catch (e) {
error = e
}
// Then
expect(initSpy).not.toHaveBeenCalled()
expect(error).not.toBeNull()
})
it('amplitudeTrack avoids track if plugin is not enabled', async () => {
// Given
const trackSpy = vitest.spyOn(amplitude, 'track')
await app.register(amplitudePlugin, {
isEnabled: false,
})
// When
const result = await app.amplitude.track({
event_type: 'event not tracked',
}).promise
// Then
expect(result).toBeNull()
expect(trackSpy).not.toHaveBeenCalled()
})
it('amplitudeTrack calls track if the plugin is enabled', async () => {
// Given
const trackResponse: Result = {
event: { event_type: 'test' },
code: 1,
message: 'message',
}
const trackSpy = vitest.spyOn(amplitude, 'track').mockImplementation(() => ({
promise: Promise.resolve(trackResponse),
}))
await app.register(amplitudePlugin, {
isEnabled: true,
apiKey: 'This is an api key',
})
// When
const event: BaseEvent = { event_type: 'event tracked' }
const response = await app.amplitude.track(event).promise
// Then
expect(response).toBe(trackResponse)
expect(trackSpy).toHaveBeenCalledWith(event)
})
it('tracks api usage if apiUsageTracking callback returns an event', async () => {
// Given
const trackSpy = vitest.spyOn(amplitude, 'track').mockImplementation(() => ({
promise: Promise.resolve<Result>({
event: { event_type: 'test' },
code: 1,
message: 'message',
}),
}))
const event: BaseEvent = { event_type: 'My api event' }
await app.register(amplitudePlugin, {
isEnabled: true,
apiKey: 'This is an api key',
apiUsageTracking: () => event,
})
// When
const response = await app
.route({
url: '/test',
method: 'GET',
handler: (_, reply) => {
return reply.send('Testing')
},
})
.inject({
method: 'GET',
url: '/test',
})
// Then
expect(response.statusCode).toBe(200)
expect(response.body).toBe('Testing')
expect(trackSpy).toHaveBeenCalledWith(event)
})
it('does not track api usage if apiUsageTracking returns null', async () => {
// Given
const trackSpy = vitest.spyOn(amplitude, 'track')
await app.register(amplitudePlugin, {
isEnabled: true,
apiKey: 'This is an api key',
apiUsageTracking: () => null,
})
// When
const response = await app
.route({
url: '/test',
method: 'GET',
handler: (_, reply) => {
return reply.send('Testing')
},
})
.inject({
method: 'GET',
url: '/test',
})
// Then
expect(response.statusCode).toBe(200)
expect(response.body).toBe('Testing')
expect(trackSpy).not.toHaveBeenCalled()
})
it('defined amplitude plugins are set up', async () => {
// Given
const addSpy = vitest.spyOn(amplitude, 'add')
const plugin = new FakePlugin()
const pluginSetUpSpy = vitest.spyOn(plugin, 'setup')
// When
await app.register(amplitudePlugin, {
isEnabled: true,
apiKey: 'This is an api key',
plugins: [plugin],
})
// Then
expect(addSpy).toHaveBeenCalledWith(plugin)
expect(pluginSetUpSpy).toHaveBeenCalled()
})
})
class FakePlugin implements EnrichmentPlugin {
type = 'enrichment' as const
setup = (): Promise<undefined> => Promise.resolve(undefined)
execute = (event: Event): Promise<Event> => Promise.resolve(event)
}