1 | const Mixpanel = require('../lib/mixpanel-node');
|
2 | const Sinon = require('sinon');
|
3 | const {create_group_funcs} = require('../lib/groups');
|
4 | const {create_profile_helpers} = require('../lib/profile_helpers');
|
5 |
|
6 |
|
7 | const test_send_request_args = function(test, func, {args, expected, use_modifiers, use_callback} = {}) {
|
8 | let expected_data = {$token: this.token, $group_key: this.group_key, $group_id: this.group_id, ...expected};
|
9 | let callback;
|
10 |
|
11 | args = [this.group_key, this.group_id, ...(args ? args : [])];
|
12 |
|
13 | if (use_modifiers) {
|
14 | let modifiers = {
|
15 | '$ignore_alias': true,
|
16 | '$ignore_time': true,
|
17 | '$ip': '1.2.3.4',
|
18 | '$time': 1234567890
|
19 | };
|
20 | Object.assign(expected_data, modifiers);
|
21 | args.push(modifiers);
|
22 | }
|
23 | if (use_callback) {
|
24 | callback = function() {};
|
25 | args.push(callback);
|
26 | }
|
27 |
|
28 | this.mixpanel.groups[func](...args);
|
29 |
|
30 | const expectedSendRequestArgs = [{ method: 'GET', endpoint: this.endpoint, data: expected_data }];
|
31 | test.ok(
|
32 | this.send_request.calledWithMatch(...expectedSendRequestArgs),
|
33 | `groups.${func} didn't call send_request with correct arguments.
|
34 | Actual arguments: ${JSON.stringify(this.send_request.getCall(0).args)}
|
35 | expected to include: ${JSON.stringify(expectedSendRequestArgs)}`
|
36 | );
|
37 | if (use_callback) {
|
38 | test.ok(
|
39 | this.send_request.args[0][1] === callback,
|
40 | `groups.${func} didn't call send_request with a callback`
|
41 | );
|
42 | }
|
43 | test.done();
|
44 | };
|
45 |
|
46 |
|
47 | exports.groups = {
|
48 | setUp: function(next) {
|
49 | this.endpoint = '/groups';
|
50 | this.group_key = 'company';
|
51 | this.group_id = 'Acme Inc.';
|
52 | this.token = 'token';
|
53 |
|
54 | this.send_request = Sinon.stub();
|
55 |
|
56 | this.mixpanel = Mixpanel.init(this.token);
|
57 | this.mixpanel.send_request = this.send_request;
|
58 |
|
59 | this.test_send_request_args = test_send_request_args;
|
60 |
|
61 | next();
|
62 | },
|
63 |
|
64 | tearDown: function(next) {
|
65 | next();
|
66 | },
|
67 |
|
68 | _set: {
|
69 | "handles set_once correctly": function(test){
|
70 | this.test_send_request_args(test, 'set_once', {
|
71 | args: ['key1', 'val1'],
|
72 | expected: {$set_once: {'key1': 'val1'}},
|
73 | });
|
74 | },
|
75 |
|
76 | "calls send_request with correct endpoint and data": function(test) {
|
77 | this.test_send_request_args(test, 'set', {
|
78 | args: ['key1', 'val1'],
|
79 | expected: {$set: {'key1': 'val1'}},
|
80 | });
|
81 | },
|
82 |
|
83 | "supports being called with a property object": function(test) {
|
84 | this.test_send_request_args(test, 'set', {
|
85 | args: [{'key1': 'val1', 'key2': 'val2'}],
|
86 | expected: {$set: {'key1': 'val1', 'key2': 'val2'}},
|
87 | });
|
88 | },
|
89 |
|
90 | "supports being called with a property object (set_once)": function(test) {
|
91 | this.test_send_request_args(test, 'set_once', {
|
92 | args: [{'key1': 'val1', 'key2': 'val2'}],
|
93 | expected: {$set_once: {'key1': 'val1', 'key2': 'val2'}},
|
94 | });
|
95 | },
|
96 |
|
97 | "supports being called with a modifiers argument": function(test) {
|
98 | this.test_send_request_args(test, 'set', {
|
99 | args: ['key1', 'val1'],
|
100 | expected: {$set: {'key1': 'val1'}},
|
101 | use_modifiers: true,
|
102 | });
|
103 | },
|
104 |
|
105 | "supports being called with a modifiers argument (set_once)": function(test) {
|
106 | this.test_send_request_args(test, 'set_once', {
|
107 | args: ['key1', 'val1'],
|
108 | expected: {$set_once: {'key1': 'val1'}},
|
109 | use_modifiers: true,
|
110 | });
|
111 | },
|
112 |
|
113 | "supports being called with a properties object and a modifiers argument": function(test) {
|
114 | this.test_send_request_args(test, 'set', {
|
115 | args: [{'key1': 'val1', 'key2': 'val2'}],
|
116 | expected: {$set: {'key1': 'val1', 'key2': 'val2'}},
|
117 | use_modifiers: true,
|
118 | });
|
119 | },
|
120 |
|
121 | "supports being called with a properties object and a modifiers argument (set_once)": function(test) {
|
122 | this.test_send_request_args(test, 'set_once', {
|
123 | args: [{'key1': 'val1', 'key2': 'val2'}],
|
124 | expected: {$set_once: {'key1': 'val1', 'key2': 'val2'}},
|
125 | use_modifiers: true,
|
126 | });
|
127 | },
|
128 |
|
129 | "handles the ip property in a property object properly": function(test) {
|
130 | this.test_send_request_args(test, 'set', {
|
131 | args: [{'ip': '1.2.3.4', 'key1': 'val1', 'key2': 'val2'}],
|
132 | expected: {
|
133 | $ip: '1.2.3.4',
|
134 | $set: {'key1': 'val1', 'key2': 'val2'},
|
135 | },
|
136 | });
|
137 | },
|
138 |
|
139 | "handles the $ignore_time property in a property object properly": function(test) {
|
140 | this.test_send_request_args(test, 'set', {
|
141 | args: [{'$ignore_time': true, 'key1': 'val1', 'key2': 'val2'}],
|
142 | expected: {
|
143 | $ignore_time: true,
|
144 | $set: {'key1': 'val1', 'key2': 'val2'},
|
145 | },
|
146 | });
|
147 | },
|
148 |
|
149 | "supports being called with a callback": function(test) {
|
150 | this.test_send_request_args(test, 'set', {
|
151 | args: ['key1', 'val1'],
|
152 | expected: {$set: {'key1': 'val1'}},
|
153 | use_callback: true,
|
154 | });
|
155 | },
|
156 |
|
157 | "supports being called with a callback (set_once)": function(test) {
|
158 | this.test_send_request_args(test, 'set_once', {
|
159 | args: ['key1', 'val1'],
|
160 | expected: {$set_once: {'key1': 'val1'}},
|
161 | use_callback: true,
|
162 | });
|
163 | },
|
164 |
|
165 | "supports being called with a properties object and a callback": function(test) {
|
166 | this.test_send_request_args(test, 'set', {
|
167 | args: [{'key1': 'val1', 'key2': 'val2'}],
|
168 | expected: {$set: {'key1': 'val1', 'key2': 'val2'}},
|
169 | use_callback: true,
|
170 | });
|
171 | },
|
172 |
|
173 | "supports being called with a properties object and a callback (set_once)": function(test) {
|
174 | this.test_send_request_args(test, 'set_once', {
|
175 | args: [{'key1': 'val1', 'key2': 'val2'}],
|
176 | expected: {$set_once: {'key1': 'val1', 'key2': 'val2'}},
|
177 | use_callback: true,
|
178 | });
|
179 | },
|
180 |
|
181 | "supports being called with a modifiers argument and a callback": function(test) {
|
182 | this.test_send_request_args(test, 'set', {
|
183 | args: ['key1', 'val1'],
|
184 | expected: {$set: {'key1': 'val1'}},
|
185 | use_callback: true,
|
186 | use_modifiers: true,
|
187 | });
|
188 | },
|
189 |
|
190 | "supports being called with a modifiers argument and a callback (set_once)": function(test) {
|
191 | this.test_send_request_args(test, 'set_once', {
|
192 | args: ['key1', 'val1'],
|
193 | expected: {$set_once: {'key1': 'val1'}},
|
194 | use_callback: true,
|
195 | use_modifiers: true,
|
196 | });
|
197 | },
|
198 |
|
199 | "supports being called with a properties object, a modifiers argument and a callback": function(test) {
|
200 | this.test_send_request_args(test, 'set', {
|
201 | args: [{'key1': 'val1', 'key2': 'val2'}],
|
202 | expected: {$set: {'key1': 'val1', 'key2': 'val2'}},
|
203 | use_callback: true,
|
204 | use_modifiers: true,
|
205 | });
|
206 | },
|
207 |
|
208 | "supports being called with a properties object, a modifiers argument and a callback (set_once)": function(test) {
|
209 | this.test_send_request_args(test, 'set_once', {
|
210 | args: [{'key1': 'val1', 'key2': 'val2'}],
|
211 | expected: {$set_once: {'key1': 'val1', 'key2': 'val2'}},
|
212 | use_callback: true,
|
213 | use_modifiers: true,
|
214 | });
|
215 | },
|
216 | },
|
217 |
|
218 | delete_group: {
|
219 | "calls send_request with correct endpoint and data": function(test) {
|
220 | this.test_send_request_args(test, 'delete_group', {
|
221 | expected: {$delete: ''},
|
222 | });
|
223 | },
|
224 |
|
225 | "supports being called with a modifiers argument": function(test) {
|
226 | this.test_send_request_args(test, 'delete_group', {
|
227 | expected: {$delete: ''},
|
228 | use_modifiers: true,
|
229 | });
|
230 | },
|
231 |
|
232 | "supports being called with a callback": function(test) {
|
233 | this.test_send_request_args(test, 'delete_group', {
|
234 | expected: {$delete: ''},
|
235 | use_callback: true,
|
236 | });
|
237 | },
|
238 |
|
239 | "supports being called with a modifiers argument and a callback": function(test) {
|
240 | this.test_send_request_args(test, 'delete_group', {
|
241 | expected: {$delete: ''},
|
242 | use_callback: true,
|
243 | use_modifiers: true,
|
244 | });
|
245 | },
|
246 | },
|
247 |
|
248 | remove: {
|
249 | "calls send_request with correct endpoint and data": function(test) {
|
250 | this.test_send_request_args(test, 'remove', {
|
251 | args: [{'key1': 'value1', 'key2': 'value2'}],
|
252 | expected: {$remove: {'key1': 'value1', 'key2': 'value2'}},
|
253 | });
|
254 | },
|
255 |
|
256 | "errors on non-scalar argument types": function(test) {
|
257 | this.mixpanel.groups.remove(this.group_key, this.group_id, {'key1': ['value1']});
|
258 | this.mixpanel.groups.remove(this.group_key, this.group_id, {key1: {key: 'val'}});
|
259 | this.mixpanel.groups.remove(this.group_key, this.group_id, 1231241.123);
|
260 | this.mixpanel.groups.remove(this.group_key, this.group_id, [5]);
|
261 | this.mixpanel.groups.remove(this.group_key, this.group_id, {key1: function() {}});
|
262 | this.mixpanel.groups.remove(this.group_key, this.group_id, {key1: [function() {}]});
|
263 |
|
264 | test.ok(
|
265 | !this.mixpanel.send_request.called,
|
266 | "groups.remove shouldn't call send_request on invalid arguments"
|
267 | );
|
268 | test.done();
|
269 | },
|
270 |
|
271 | "supports being called with a modifiers argument": function(test) {
|
272 | this.test_send_request_args(test, 'remove', {
|
273 | args: [{'key1': 'value1'}],
|
274 | expected: {$remove: {'key1': 'value1'}},
|
275 | use_modifiers: true,
|
276 | });
|
277 | },
|
278 |
|
279 | "supports being called with a callback": function(test) {
|
280 | this.test_send_request_args(test, 'remove', {
|
281 | args: [{'key1': 'value1'}],
|
282 | expected: {$remove: {'key1': 'value1'}},
|
283 | use_callback: true,
|
284 | });
|
285 | },
|
286 |
|
287 | "supports being called with a modifiers argument and a callback": function(test) {
|
288 | this.test_send_request_args(test, 'remove', {
|
289 | args: [{'key1': 'value1'}],
|
290 | expected: {$remove: {'key1': 'value1'}},
|
291 | use_callback: true,
|
292 | use_modifiers: true,
|
293 | });
|
294 | },
|
295 | },
|
296 |
|
297 | union: {
|
298 | "calls send_request with correct endpoint and data": function(test) {
|
299 | this.test_send_request_args(test, 'union', {
|
300 | args: [{'key1': ['value1', 'value2']}],
|
301 | expected: {$union: {'key1': ['value1', 'value2']}},
|
302 | });
|
303 | },
|
304 |
|
305 | "supports being called with a scalar value": function(test) {
|
306 | this.test_send_request_args(test, 'union', {
|
307 | args: [{'key1': 'value1'}],
|
308 | expected: {$union: {'key1': ['value1']}},
|
309 | });
|
310 | },
|
311 |
|
312 | "errors on other argument types": function(test) {
|
313 | this.mixpanel.groups.union(this.group_key, this.group_id, {key1: {key: 'val'}});
|
314 | this.mixpanel.groups.union(this.group_key, this.group_id, 1231241.123);
|
315 | this.mixpanel.groups.union(this.group_key, this.group_id, [5]);
|
316 | this.mixpanel.groups.union(this.group_key, this.group_id, {key1: function() {}});
|
317 | this.mixpanel.groups.union(this.group_key, this.group_id, {key1: [function() {}]});
|
318 |
|
319 | test.ok(
|
320 | !this.mixpanel.send_request.called,
|
321 | "groups.union shouldn't call send_request on invalid arguments"
|
322 | );
|
323 | test.done();
|
324 | },
|
325 |
|
326 | "supports being called with a modifiers argument": function(test) {
|
327 | this.test_send_request_args(test, 'union', {
|
328 | args: [{'key1': ['value1', 'value2']}],
|
329 | expected: {$union: {'key1': ['value1', 'value2']}},
|
330 | use_modifiers: true,
|
331 | });
|
332 | },
|
333 |
|
334 | "supports being called with a callback": function(test) {
|
335 | this.test_send_request_args(test, 'union', {
|
336 | args: [{'key1': ['value1', 'value2']}],
|
337 | expected: {$union: {'key1': ['value1', 'value2']}},
|
338 | use_callback: true,
|
339 | });
|
340 | },
|
341 |
|
342 | "supports being called with a modifiers argument and a callback": function(test) {
|
343 | this.test_send_request_args(test, 'union', {
|
344 | args: [{'key1': ['value1', 'value2']}],
|
345 | expected: {$union: {'key1': ['value1', 'value2']}},
|
346 | use_callback: true,
|
347 | use_modifiers: true,
|
348 | });
|
349 | },
|
350 | },
|
351 |
|
352 | unset: {
|
353 | "calls send_request with correct endpoint and data": function(test) {
|
354 | this.test_send_request_args(test, 'unset', {
|
355 | args: ['key1'],
|
356 | expected: {$unset: ['key1']},
|
357 | });
|
358 | },
|
359 |
|
360 | "supports being called with a property array": function(test) {
|
361 | this.test_send_request_args(test, 'unset', {
|
362 | args: [['key1', 'key2']],
|
363 | expected: {$unset: ['key1', 'key2']},
|
364 | });
|
365 | },
|
366 |
|
367 | "errors on other argument types": function(test) {
|
368 | this.mixpanel.groups.unset(this.group_key, this.group_id, { key1:'val1', key2:'val2' });
|
369 | this.mixpanel.groups.unset(this.group_key, this.group_id, 1231241.123);
|
370 |
|
371 | test.ok(
|
372 | !this.mixpanel.send_request.called,
|
373 | "groups.unset shouldn't call send_request on invalid arguments"
|
374 | );
|
375 | test.done();
|
376 | },
|
377 |
|
378 | "supports being called with a modifiers argument": function(test) {
|
379 | this.test_send_request_args(test, 'unset', {
|
380 | args: ['key1'],
|
381 | expected: {$unset: ['key1']},
|
382 | use_modifiers: true,
|
383 | });
|
384 | },
|
385 |
|
386 | "supports being called with a callback": function(test) {
|
387 | this.test_send_request_args(test, 'unset', {
|
388 | args: ['key1'],
|
389 | expected: {$unset: ['key1']},
|
390 | use_callback: true,
|
391 | });
|
392 | },
|
393 |
|
394 | "supports being called with a modifiers argument and a callback": function(test) {
|
395 | this.test_send_request_args(test, 'unset', {
|
396 | args: ['key1'],
|
397 | expected: {$unset: ['key1']},
|
398 | use_callback: true,
|
399 | use_modifiers: true,
|
400 | });
|
401 | },
|
402 | },
|
403 | };
|