forked from mixpanel/mixpanel-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.js
333 lines (288 loc) · 10.4 KB
/
track.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
const https = require('https');
const events = require('events');
const proxyquire = require('proxyquire');
const Mixpanel = require('../lib/mixpanel-node');
const packageInfo = require('../package.json');
const utils = require('../lib/utils');
var mock_now_time = new Date(2016, 1, 1).getTime();
describe('track', () => {
let mixpanel;
beforeAll(() => {
mixpanel = Mixpanel.init('token');
vi.useFakeTimers();
vi.setSystemTime(mock_now_time);
vi.spyOn(mixpanel, 'send_request');
return () => {
vi.useRealTimers();
mixpanel.send_request.mockRestore();
}
});
it('calls send_request with correct endpoint and data', () => {
var event = 'test',
props = { key1: 'val1' },
expected_endpoint = '/track',
expected_data = {
event: 'test',
properties: expect.objectContaining({
key1: 'val1',
token: 'token'
}),
};
mixpanel.track(event, props);
expect(mixpanel.send_request).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: expected_endpoint,
data: expected_data
}),
undefined,
);
});
it('can be called with optional properties', () => {
var expected_endpoint = '/track',
expected_data = {
event: 'test',
properties: expect.objectContaining({
token: 'token',
}),
};
mixpanel.track('test');
expect(mixpanel.send_request).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: expected_endpoint,
data: expected_data,
}),
undefined,
);
});
it('can be called with optional callback', (test) => {
var expected_endpoint = '/track',
expected_data = {
event: 'test',
properties: {
token: 'token',
},
};
mixpanel.send_request.mockImplementationOnce((_, cb) => cb(undefined));
const callback = vi.fn();
mixpanel.track('test', callback);
expect(callback).toHaveBeenCalledWith(undefined);
});
it('supports Date object for time', (test) => {
var event = 'test',
time = new Date(mock_now_time),
props = { time: time },
expected_endpoint = '/track',
expected_data = {
event: 'test',
properties: expect.objectContaining({
token: 'token',
time: time.getTime(),
mp_lib: 'node',
$lib_version: packageInfo.version
}),
};
mixpanel.track(event, props);
expect(mixpanel.send_request).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: expected_endpoint,
data: expected_data,
}),
undefined,
);
});
it('supports unix timestamp for time', (test) => {
var event = 'test',
time = mock_now_time,
props = { time: time },
expected_endpoint = '/track',
expected_data = {
event: 'test',
properties: expect.objectContaining({
token: 'token',
time: time,
mp_lib: 'node',
$lib_version: packageInfo.version
}),
};
mixpanel.track(event, props);
expect(mixpanel.send_request).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: expected_endpoint,
data: expected_data,
}),
undefined,
);
});
it('throws error if time is not a number or Date', () => {
var event = 'test',
props = { time: 'not a number or Date' };
expect(() => mixpanel.track(event, props)).toThrowError(
/`time` property must be a Date or Unix timestamp/,
);
});
it('does not require time property', (test) => {
var event = 'test',
props = {};
expect(() => mixpanel.track(event, props)).not.toThrowError();
});
});
describe('track_batch', () => {
let mixpanel;
beforeEach(() => {
mixpanel = Mixpanel.init('token');
vi.useFakeTimers();
vi.spyOn(mixpanel, 'send_request');
return () => {
vi.useRealTimers();
mixpanel.send_request.mockRestore();
}
});
it('calls send_request with correct endpoint, data, and method', () => {
var expected_endpoint = '/track',
event_list = [
{event: 'test', properties: { key1: 'val1', time: 500 }},
{event: 'test', properties: { key2: 'val2', time: 1000}},
{event: 'test2', properties: { key2: 'val2', time: 1500}},
],
expected_data = [
{event: 'test', properties: { key1: 'val1', time: 500, token: 'token'}},
{event: 'test', properties: { key2: 'val2', time: 1000, token: 'token'}},
{event: 'test2', properties: { key2: 'val2', time: 1500, token: 'token'}}
].map((val) => expect.objectContaining(val));
mixpanel.track_batch(event_list);
expect(mixpanel.send_request).toHaveBeenCalledWith(
{
method: 'POST',
endpoint: expected_endpoint,
data: expected_data,
},
expect.any(Function),
);
});
it('does not require the time argument for every event', () => {
var event_list = [
{event: 'test', properties: {key1: 'val1', time: 500 }},
{event: 'test', properties: {key2: 'val2', time: 1000}},
{event: 'test2', properties: {key2: 'val2' }}
];
expect(() => mixpanel.track_batch(event_list)).not.toThrowError();
});
it('batches 50 events at a time', () => {
var event_list = [];
for (var ei = 0; ei < 130; ei++) { // 3 batches: 50 + 50 + 30
event_list.push({
event: 'test',
properties: { key1: 'val1', time: 500 + ei },
});
}
mixpanel.track_batch(event_list);
expect(mixpanel.send_request).toHaveBeenCalledTimes(3);
});
});
describe('track_batch_integration', () => {
let mixpanel;
let http_emitter;
let res;
let event_list;
beforeEach(() => {
mixpanel = Mixpanel.init('token', { key: 'key' });
vi.useFakeTimers();
vi.spyOn(https, 'request');
http_emitter = new events.EventEmitter();
// stub sequence of https responses
res = [];
for (let ri = 0; ri < 5; ri++) {
res.push(new events.EventEmitter());
https.request.mockImplementationOnce((_, cb) => {
cb(res[ri]);
return {
write: function () {},
end: function () {},
on: function (event) {},
};
});
}
event_list = [];
for (var ei = 0; ei < 130; ei++) {// 3 batches: 50 + 50 + 30
event_list.push({event: 'test', properties: { key1: 'val1', time: 500 + ei }});
}
return () => {
vi.restoreAllMocks();
}
});
it('calls provided callback after all requests finish', () => {
const callback = vi.fn();
mixpanel.track_batch(event_list, callback);
for (var ri = 0; ri < 3; ri++) {
res[ri].emit('data', '1');
res[ri].emit('end');
}
expect(https.request).toHaveBeenCalledTimes(3);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(null, [
undefined,
undefined,
undefined,
]);
});
it('passes error list to callback', () => {
const callback = vi.fn();
mixpanel.track_batch(event_list, callback);
for (var ri = 0; ri < 3; ri++) {
res[ri].emit('data', '0');
res[ri].emit('end');
}
expect(callback.mock.calls[0][0].length).toBe(3);
});
it('calls provided callback when options are passed', () => {
const callback = vi.fn();
mixpanel.track_batch(event_list, { max_batch_size: 100 }, callback);
for (var ri = 0; ri < 3; ri++) {
res[ri].emit('data', '1');
res[ri].emit('end');
}
expect(callback).toHaveBeenCalledTimes(1);
expect(https.request).toHaveBeenCalledTimes(3);
expect(callback).toHaveBeenCalledWith(null, [undefined]);
});
it('sends more requests when max_batch_size < 50', (test) => {
const callback = vi.fn();
mixpanel.track_batch(event_list, { max_batch_size: 30 }, callback);
for (var ri = 0; ri < 5; ri++) {
res[ri].emit('data', '1');
res[ri].emit('end');
}
expect(callback).toHaveBeenCalledTimes(1);
expect(https.request).toHaveBeenCalledTimes(5);
expect(callback).toHaveBeenCalledWith(null, [
undefined,
undefined,
undefined,
undefined,
undefined,
]);
});
it('can set max concurrent requests', (test) => {
const async_all_stub = vi.fn();
async_all_stub.mockImplementation((_, __, cb) => cb(null));
const PatchedMixpanel = proxyquire('../lib/mixpanel-node', {
'./utils': { async_all: async_all_stub },
});
mixpanel = PatchedMixpanel.init('token', { key: 'key' });
const callback = vi.fn();
mixpanel.track_batch(event_list, { max_batch_size: 30, max_concurrent_requests: 2 }, callback);
for (var ri = 0; ri < 3; ri++) {
res[ri].emit('data', '1');
res[ri].emit('end');
}
expect(callback).toHaveBeenCalledTimes(1);
expect(async_all_stub).toHaveBeenCalledTimes(3);
expect(callback).toHaveBeenCalledWith(null, undefined);
});
it('behaves well without a callback', () => {
mixpanel.track_batch(event_list);
expect(https.request).toHaveBeenCalledTimes(3);
mixpanel.track_batch(event_list, { max_batch_size: 100 });
expect(https.request).toHaveBeenCalledTimes(5);
});
});