mixpanel-react-native 2.4.1 → 3.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/.vscode/settings.json +4 -0
  2. package/CHANGELOG.md +84 -51
  3. package/README.md +9 -3
  4. package/Samples/MixpanelExpo/App.js +340 -0
  5. package/Samples/MixpanelExpo/app.json +30 -0
  6. package/Samples/MixpanelExpo/assets/adaptive-icon.png +0 -0
  7. package/Samples/MixpanelExpo/assets/favicon.png +0 -0
  8. package/Samples/MixpanelExpo/assets/icon.png +0 -0
  9. package/Samples/MixpanelExpo/assets/splash.png +0 -0
  10. package/Samples/MixpanelExpo/babel.config.js +6 -0
  11. package/Samples/MixpanelExpo/package-lock.json +22551 -0
  12. package/Samples/MixpanelExpo/package.json +25 -0
  13. package/__mocks__/@react-native-async-storage/async-storage.js +1 -0
  14. package/__tests__/core.test.js +135 -0
  15. package/__tests__/index.test.js +74 -42
  16. package/__tests__/jest_setup.js +23 -4
  17. package/__tests__/main.test.js +788 -0
  18. package/__tests__/network.test.js +72 -0
  19. package/__tests__/queue.test.js +65 -0
  20. package/docs/Mixpanel.html +42 -44
  21. package/docs/MixpanelGroup.html +7 -7
  22. package/docs/People.html +12 -12
  23. package/docs/index.html +5 -4
  24. package/docs/index.js.html +888 -793
  25. package/index.d.ts +75 -53
  26. package/index.js +89 -57
  27. package/javascript/mixpanel-config.js +102 -0
  28. package/javascript/mixpanel-constants.js +22 -0
  29. package/javascript/mixpanel-core.js +163 -0
  30. package/javascript/mixpanel-logger.js +35 -0
  31. package/javascript/mixpanel-main.js +550 -0
  32. package/javascript/mixpanel-network.js +86 -0
  33. package/javascript/mixpanel-persistent.js +312 -0
  34. package/javascript/mixpanel-queue.js +65 -0
  35. package/javascript/mixpanel-storage.js +36 -0
  36. package/javascript/mixpanel-utils.js +38 -0
  37. package/logs/.b11bf985d66a037ca5688a574653f3bf76a28dfa-audit.json +19 -0
  38. package/logs/.c366df74eeb671df60a57a2075ae40a3dae2af25-audit.json +19 -0
  39. package/package.json +11 -6
  40. package/release.py +5 -4
@@ -0,0 +1,788 @@
1
+ import {MixpanelType} from "mixpanel-react-native/javascript/mixpanel-constants";
2
+ import {exp} from "react-native/Libraries/Animated/src/Easing";
3
+ import {get} from "react-native/Libraries/Utilities/PixelRatio";
4
+
5
+ jest.mock("mixpanel-react-native/javascript/mixpanel-core", () => ({
6
+ MixpanelCore: jest.fn().mockImplementation(() => ({
7
+ initialize: jest.fn(),
8
+ startProcessingQueue: jest.fn(),
9
+ addToMixpanelQueue: jest.fn(),
10
+ flush: jest.fn(),
11
+ })),
12
+ }));
13
+
14
+ jest.mock("mixpanel-react-native/javascript/mixpanel-network", () => ({
15
+ MixpanelNetwork: {
16
+ sendRequest: jest.fn(),
17
+ },
18
+ }));
19
+
20
+ jest.mock("mixpanel-react-native/javascript/mixpanel-persistent", () => {
21
+ return {
22
+ MixpanelPersistent: {
23
+ getInstance: jest.fn().mockImplementation(() => {
24
+ return {
25
+ initializationCompletePromise: jest.fn(),
26
+ reset: jest.fn(),
27
+ updateOptedOut: jest.fn(),
28
+ persistOptedOut: jest.fn(),
29
+ getSuperProperties: jest.fn().mockReturnValue({
30
+ company_id: [222],
31
+ superProp1: "value1",
32
+ superProp2: "value2",
33
+ }),
34
+ getDistinctId: jest.fn().mockReturnValue("distinct-id-mock"),
35
+ getDeviceId: jest.fn().mockReturnValue("device-id-mock"),
36
+ getUserId: jest.fn().mockReturnValue("user-id-mock"),
37
+ getOptedOut: jest.fn(),
38
+ getQueue: jest.fn(),
39
+ saveQueue: jest.fn(),
40
+ loadQueue: jest.fn(),
41
+ loadDeviceId: jest.fn(),
42
+ updateDeviceId: jest.fn(),
43
+ persistDeviceId: jest.fn(),
44
+ loadDistinctId: jest.fn(),
45
+ updateDistinctId: jest.fn(),
46
+ persistDistinctId: jest.fn(),
47
+ loadUserId: jest.fn(),
48
+ updateUserId: jest.fn(),
49
+ persistUserId: jest.fn(),
50
+ loadSuperProperties: jest.fn(),
51
+ persistSuperProperties: jest.fn(),
52
+ loadOptedOut: jest.fn(),
53
+ persistOptedOut: jest.fn(),
54
+ loadIdentity: jest.fn(),
55
+ persistIdentity: jest.fn(),
56
+ getIdentity: jest.fn(),
57
+ resetIdentity: jest.fn(),
58
+ getTimeEvents: jest.fn().mockReturnValue({
59
+ "test-event": Math.round(Date.now() / 1000 - 1000),
60
+ }),
61
+ loadTimeEvents: jest.fn(),
62
+ updateTimeEvents: jest.fn(),
63
+ persistTimeEvents: jest.fn(),
64
+ updateSuperProperties: jest.fn(),
65
+ };
66
+ }),
67
+ },
68
+ };
69
+ });
70
+
71
+ jest.mock("mixpanel-react-native/javascript/mixpanel-config", () => ({
72
+ MixpanelConfig: {
73
+ getInstance: jest.fn().mockReturnValue({
74
+ getFlushInterval: jest.fn().mockReturnValue(1000),
75
+ getFlushBatchSize: jest.fn().mockReturnValue(50),
76
+ getServerURL: jest.fn(),
77
+ getUseIpAddressForGeolocation: jest.fn(),
78
+ setLoggingEnabled: jest.fn(),
79
+ getLoggingEnabled: jest.fn().mockReturnValue(true),
80
+ setServerURL: jest.fn(),
81
+ }),
82
+ },
83
+ }));
84
+
85
+ // jest.mock("mixpanel-react-native/javascript/mixpanel-logger", () => {
86
+ // return {
87
+ // MixpanelLogger: {
88
+ // log: jest.fn(),
89
+ // },
90
+ // };
91
+ // });
92
+
93
+ const {
94
+ MixpanelNetwork,
95
+ } = require("mixpanel-react-native/javascript/mixpanel-network");
96
+
97
+ const {
98
+ MixpanelCore,
99
+ } = require("mixpanel-react-native/javascript/mixpanel-core");
100
+
101
+ const {
102
+ MixpanelQueueManager,
103
+ } = require("mixpanel-react-native/javascript/mixpanel-queue");
104
+
105
+ const {
106
+ MixpanelPersistent,
107
+ } = require("mixpanel-react-native/javascript/mixpanel-persistent");
108
+
109
+ const {
110
+ MixpanelConfig,
111
+ } = require("mixpanel-react-native/javascript/mixpanel-config");
112
+
113
+ describe("MixpanelMain", () => {
114
+ let mixpanelMain;
115
+ const token = "test-token";
116
+
117
+ beforeEach(() => {
118
+ jest.clearAllMocks();
119
+ jest.isolateModules(async () => {
120
+ const MixpanelMain = require("mixpanel-react-native/javascript/mixpanel-main")
121
+ .default;
122
+ mixpanelMain = new MixpanelMain(token);
123
+ MixpanelConfig.getInstance().getLoggingEnabled.mockReturnValue(true);
124
+ });
125
+ jest.spyOn(console, "log").mockImplementation(() => {});
126
+ });
127
+
128
+ it("should initialize properly", async () => {
129
+ const trackAutomaticEvents = false;
130
+ const optOutTrackingDefault = false;
131
+ const superProperties = {superProp1: "value1", superProp2: "value2"};
132
+ const serverURL = "https://api.mixpanel.com";
133
+
134
+ await mixpanelMain.initialize(
135
+ token,
136
+ trackAutomaticEvents,
137
+ optOutTrackingDefault,
138
+ superProperties,
139
+ serverURL
140
+ );
141
+
142
+ expect(mixpanelMain.core.initialize).toHaveBeenCalledWith(token);
143
+
144
+ expect(
145
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
146
+ ).toHaveBeenCalledWith(token, {
147
+ superProp1: "value1",
148
+ superProp2: "value2",
149
+ company_id: [222],
150
+ });
151
+ expect(
152
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
153
+ ).toHaveBeenCalledWith(token);
154
+ });
155
+
156
+ it("register super properties should update properties", async () => {
157
+ mixpanelMain.registerSuperProperties(token, {superProp3: "value3"});
158
+ expect(
159
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
160
+ ).toHaveBeenCalledWith(token, {
161
+ superProp1: "value1",
162
+ superProp2: "value2",
163
+ superProp3: "value3",
164
+ company_id: [222],
165
+ });
166
+ expect(
167
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
168
+ ).toHaveBeenCalledWith(token);
169
+ });
170
+
171
+ it("register super properties once should update properties only once", async () => {
172
+ mixpanelMain.registerSuperPropertiesOnce(token, {superProp3: "value3"});
173
+ expect(
174
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
175
+ ).toHaveBeenCalledWith(token, {
176
+ superProp1: "value1",
177
+ superProp2: "value2",
178
+ superProp3: "value3",
179
+ company_id: [222],
180
+ });
181
+ expect(
182
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
183
+ ).toHaveBeenCalledWith(token);
184
+ mixpanelMain.registerSuperPropertiesOnce(token, {superProp3: "value4"});
185
+ expect(
186
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
187
+ ).toHaveBeenCalledWith(token, {
188
+ superProp1: "value1",
189
+ superProp2: "value2",
190
+ superProp3: "value3",
191
+ company_id: [222],
192
+ });
193
+ expect(
194
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
195
+ ).toHaveBeenCalledWith(token);
196
+ });
197
+
198
+ it("unregister super properties should update properties properly", async () => {
199
+ mixpanelMain.registerSuperPropertiesOnce(token, {superProp3: "value3"});
200
+ expect(
201
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
202
+ ).toHaveBeenCalledWith(token, {
203
+ superProp1: "value1",
204
+ superProp2: "value2",
205
+ superProp3: "value3",
206
+ company_id: [222],
207
+ });
208
+
209
+ mixpanelMain.unregisterSuperProperty(token, "superProp3");
210
+ expect(
211
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
212
+ ).toHaveBeenCalledWith(token, {
213
+ superProp1: "value1",
214
+ superProp2: "value2",
215
+ company_id: [222],
216
+ });
217
+ });
218
+
219
+ it("clear super properties should clear properties properly", async () => {
220
+ mixpanelMain.clearSuperProperties(token);
221
+ expect(
222
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
223
+ ).toHaveBeenCalledWith(token, {});
224
+ expect(
225
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
226
+ ).toHaveBeenCalledWith(token);
227
+ });
228
+
229
+ it("should send correct payload on track event", async () => {
230
+ const eventName = "Test Event";
231
+ const eventProperties = {prop1: "value1", prop2: "value2"};
232
+
233
+ await mixpanelMain.track(token, eventName, eventProperties);
234
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
235
+ token,
236
+ MixpanelType.EVENTS,
237
+ expect.objectContaining({
238
+ event: eventName,
239
+ properties: expect.objectContaining({
240
+ token: token,
241
+ time: expect.any(Number),
242
+ prop1: "value1",
243
+ prop2: "value2",
244
+ $device_id: "device-id-mock",
245
+ $user_id: "user-id-mock",
246
+ distinct_id: "distinct-id-mock",
247
+ superProp1: "value1", // include super properties
248
+ superProp2: "value2",
249
+ }),
250
+ })
251
+ );
252
+ });
253
+
254
+ it("should trigger the flush on the flush call", async () => {
255
+ mixpanelMain.flush(token);
256
+ expect(mixpanelMain.core.flush).toHaveBeenCalledWith(token);
257
+ });
258
+
259
+ it("setLoggingEnabled should work as expected", async () => {
260
+ mixpanelMain.setLoggingEnabled(token, true);
261
+ expect(mixpanelMain.config.setLoggingEnabled).toHaveBeenCalledWith(
262
+ token,
263
+ true
264
+ );
265
+ MixpanelConfig.getInstance().getLoggingEnabled.mockReturnValueOnce(true);
266
+ await mixpanelMain.track(token, "test-event");
267
+ expect(console.log).toHaveBeenCalled();
268
+ });
269
+
270
+ it("timeEvent should work as expected", async () => {
271
+ mixpanelMain.timeEvent(token, "test-event");
272
+ expect(
273
+ mixpanelMain.mixpanelPersistent.updateTimeEvents
274
+ ).toHaveBeenCalledWith(token, {"test-event": expect.any(Number)});
275
+ expect(
276
+ mixpanelMain.mixpanelPersistent.persistTimeEvents
277
+ ).toHaveBeenCalledWith(token);
278
+ });
279
+
280
+ it("eventElapsedTime should work as expected", async () => {
281
+ mixpanelMain.timeEvent(token, "test-event");
282
+ const elapsedTime = await mixpanelMain.eventElapsedTime(
283
+ token,
284
+ "test-event"
285
+ );
286
+ expect(elapsedTime).toBeGreaterThan(0);
287
+ });
288
+
289
+ it("should update the identity properties on identify", async () => {
290
+ jest.resetModules();
291
+ const newDistinctId = "new-distinct-id";
292
+ await mixpanelMain.identify(token, newDistinctId);
293
+ expect(
294
+ mixpanelMain.mixpanelPersistent.updateDistinctId
295
+ ).toHaveBeenCalledWith(token, newDistinctId);
296
+ expect(mixpanelMain.mixpanelPersistent.updateUserId).toHaveBeenCalledWith(
297
+ token,
298
+ newDistinctId
299
+ );
300
+ });
301
+
302
+ it("should not update the identity properties if the new distinctid is the save as before", async () => {
303
+ const newDistinctId = "distinct-id-mock";
304
+ await mixpanelMain.identify(token, newDistinctId);
305
+ expect(
306
+ mixpanelMain.mixpanelPersistent.updateDistinctId
307
+ ).toHaveBeenCalledTimes(0);
308
+ expect(mixpanelMain.mixpanelPersistent.updateUserId).toHaveBeenCalledTimes(
309
+ 0
310
+ );
311
+ });
312
+
313
+ it("should send correct payload on set profile properties", async () => {
314
+ const properties = {prop1: "value1", prop2: "value2"};
315
+
316
+ await mixpanelMain.set(token, properties);
317
+
318
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
319
+ token,
320
+ MixpanelType.USER,
321
+ expect.objectContaining({
322
+ $token: token,
323
+ $time: expect.any(Number),
324
+ $set: {prop1: "value1", prop2: "value2"},
325
+ $distinct_id: "distinct-id-mock",
326
+ $device_id: "device-id-mock",
327
+ $user_id: "user-id-mock",
328
+ })
329
+ );
330
+ });
331
+
332
+ it("should send correct payload on setOnce profile properties", async () => {
333
+ const properties = {prop1: "value1", prop2: "value2"};
334
+
335
+ await mixpanelMain.setOnce(token, properties);
336
+
337
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
338
+ token,
339
+ MixpanelType.USER,
340
+ expect.objectContaining({
341
+ $token: token,
342
+ $time: expect.any(Number),
343
+ $set_once: {prop1: "value1", prop2: "value2"},
344
+ $distinct_id: "distinct-id-mock",
345
+ $device_id: "device-id-mock",
346
+ $user_id: "user-id-mock",
347
+ })
348
+ );
349
+ });
350
+
351
+ it("should send correct payload on increment profile properties", async () => {
352
+ const properties = {prop1: 3};
353
+
354
+ await mixpanelMain.increment(token, properties);
355
+
356
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
357
+ token,
358
+ MixpanelType.USER,
359
+ expect.objectContaining({
360
+ $token: token,
361
+ $time: expect.any(Number),
362
+ $add: {prop1: 3},
363
+ $distinct_id: "distinct-id-mock",
364
+ $device_id: "device-id-mock",
365
+ $user_id: "user-id-mock",
366
+ })
367
+ );
368
+ });
369
+
370
+ it("should send correct payload on append profile properties", async () => {
371
+ const properties = {prop1: "value1"};
372
+
373
+ await mixpanelMain.append(token, properties);
374
+
375
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
376
+ token,
377
+ MixpanelType.USER,
378
+ expect.objectContaining({
379
+ $token: token,
380
+ $time: expect.any(Number),
381
+ $append: {prop1: "value1"},
382
+ $distinct_id: "distinct-id-mock",
383
+ $device_id: "device-id-mock",
384
+ $user_id: "user-id-mock",
385
+ })
386
+ );
387
+
388
+ await mixpanelMain.append(token, "testProp", "testValue");
389
+
390
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
391
+ token,
392
+ MixpanelType.USER,
393
+ expect.objectContaining({
394
+ $token: token,
395
+ $time: expect.any(Number),
396
+ $append: {testProp: "testValue"},
397
+ $distinct_id: "distinct-id-mock",
398
+ $device_id: "device-id-mock",
399
+ $user_id: "user-id-mock",
400
+ })
401
+ );
402
+ });
403
+
404
+ it("should send correct payload on union profile properties", async () => {
405
+ const properties = {prop1: "value1"};
406
+
407
+ await mixpanelMain.union(token, properties);
408
+
409
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
410
+ token,
411
+ MixpanelType.USER,
412
+ expect.objectContaining({
413
+ $token: token,
414
+ $time: expect.any(Number),
415
+ $union: {prop1: "value1"},
416
+ $distinct_id: "distinct-id-mock",
417
+ $device_id: "device-id-mock",
418
+ $user_id: "user-id-mock",
419
+ })
420
+ );
421
+
422
+ await mixpanelMain.union(token, "testProp", "testValue");
423
+
424
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
425
+ token,
426
+ MixpanelType.USER,
427
+ expect.objectContaining({
428
+ $token: token,
429
+ $time: expect.any(Number),
430
+ $union: {testProp: "testValue"},
431
+ $distinct_id: "distinct-id-mock",
432
+ $device_id: "device-id-mock",
433
+ $user_id: "user-id-mock",
434
+ })
435
+ );
436
+ });
437
+
438
+ it("should send correct payload on remove profile properties", async () => {
439
+ const properties = {prop1: "value1"};
440
+
441
+ await mixpanelMain.remove(token, properties);
442
+
443
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
444
+ token,
445
+ MixpanelType.USER,
446
+ expect.objectContaining({
447
+ $token: token,
448
+ $time: expect.any(Number),
449
+ $remove: {prop1: "value1"},
450
+ $distinct_id: "distinct-id-mock",
451
+ $device_id: "device-id-mock",
452
+ $user_id: "user-id-mock",
453
+ })
454
+ );
455
+
456
+ await mixpanelMain.remove(token, "testProp", "testValue");
457
+
458
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
459
+ token,
460
+ MixpanelType.USER,
461
+ expect.objectContaining({
462
+ $token: token,
463
+ $time: expect.any(Number),
464
+ $remove: {testProp: "testValue"},
465
+ $distinct_id: "distinct-id-mock",
466
+ $device_id: "device-id-mock",
467
+ $user_id: "user-id-mock",
468
+ })
469
+ );
470
+ });
471
+
472
+ it("should send correct payload on trackCharge", async () => {
473
+ const properties = {prop1: "value1"};
474
+ const charge = 100;
475
+
476
+ await mixpanelMain.trackCharge(token, charge, properties);
477
+
478
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
479
+ token,
480
+ MixpanelType.USER,
481
+ expect.objectContaining({
482
+ $token: token,
483
+ $time: expect.any(Number),
484
+ $append: {
485
+ $transactions: {
486
+ $amount: 100,
487
+ $time: expect.any(Number),
488
+ prop1: "value1",
489
+ },
490
+ },
491
+ $distinct_id: "distinct-id-mock",
492
+ $device_id: "device-id-mock",
493
+ $user_id: "user-id-mock",
494
+ })
495
+ );
496
+ });
497
+
498
+ it("should send correct payload on clearCharge", async () => {
499
+ await mixpanelMain.clearCharges(token);
500
+
501
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
502
+ token,
503
+ MixpanelType.USER,
504
+ expect.objectContaining({
505
+ $token: token,
506
+ $time: expect.any(Number),
507
+ $set: {
508
+ $transactions: [],
509
+ },
510
+ $distinct_id: "distinct-id-mock",
511
+ $device_id: "device-id-mock",
512
+ $user_id: "user-id-mock",
513
+ })
514
+ );
515
+ });
516
+
517
+ it("should send correct payload on unset profile properties", async () => {
518
+ const property = "prop1";
519
+
520
+ await mixpanelMain.unset(token, property);
521
+
522
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
523
+ token,
524
+ MixpanelType.USER,
525
+ expect.objectContaining({
526
+ $token: token,
527
+ $time: expect.any(Number),
528
+ $unset: [property],
529
+ $distinct_id: "distinct-id-mock",
530
+ $device_id: "device-id-mock",
531
+ $user_id: "user-id-mock",
532
+ })
533
+ );
534
+ });
535
+
536
+ it("should send correct payload on delete profile", async () => {
537
+ await mixpanelMain.deleteUser(token);
538
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
539
+ token,
540
+ MixpanelType.USER,
541
+ expect.objectContaining({
542
+ $token: token,
543
+ $time: expect.any(Number),
544
+ $delete: "null",
545
+ $distinct_id: "distinct-id-mock",
546
+ $device_id: "device-id-mock",
547
+ $user_id: "user-id-mock",
548
+ })
549
+ );
550
+ });
551
+
552
+ it("should send correct payload on trackWithGroups", async () => {
553
+ const properties = {prop1: "value1"};
554
+ const eventName = "event1";
555
+ const groups = {company_id: 111};
556
+ await mixpanelMain.trackWithGroups(token, eventName, properties, groups);
557
+
558
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
559
+ token,
560
+ MixpanelType.EVENTS,
561
+ expect.objectContaining({
562
+ event: "event1",
563
+ properties: expect.objectContaining({
564
+ token: token,
565
+ time: expect.any(Number),
566
+ prop1: "value1",
567
+ $device_id: "device-id-mock",
568
+ $user_id: "user-id-mock",
569
+ distinct_id: "distinct-id-mock",
570
+ superProp1: "value1", // include super properties
571
+ superProp2: "value2",
572
+ company_id: 111,
573
+ }),
574
+ })
575
+ );
576
+ });
577
+
578
+ it("should send correct payload on addGroup", async () => {
579
+ await mixpanelMain.addGroup(token, "company_id", 111);
580
+
581
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
582
+ token,
583
+ MixpanelType.USER,
584
+ expect.objectContaining({
585
+ $token: token,
586
+ $time: expect.any(Number),
587
+ $union: {company_id: [111]},
588
+ $distinct_id: "distinct-id-mock",
589
+ $device_id: "device-id-mock",
590
+ $user_id: "user-id-mock",
591
+ })
592
+ );
593
+
594
+ expect(
595
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
596
+ ).toHaveBeenCalledWith(token, {
597
+ company_id: [222, 111],
598
+ superProp1: "value1",
599
+ superProp2: "value2",
600
+ });
601
+ expect(
602
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
603
+ ).toHaveBeenCalledWith(token);
604
+ });
605
+
606
+ it("should send correct payload on setGroup", async () => {
607
+ await mixpanelMain.setGroup(token, "company_id", 333);
608
+
609
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
610
+ token,
611
+ MixpanelType.USER,
612
+ expect.objectContaining({
613
+ $token: token,
614
+ $time: expect.any(Number),
615
+ $set: {company_id: [333]},
616
+ $distinct_id: "distinct-id-mock",
617
+ $device_id: "device-id-mock",
618
+ $user_id: "user-id-mock",
619
+ })
620
+ );
621
+
622
+ expect(
623
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
624
+ ).toHaveBeenCalledWith(token, {
625
+ company_id: [333],
626
+ superProp1: "value1",
627
+ superProp2: "value2",
628
+ });
629
+ expect(
630
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
631
+ ).toHaveBeenCalledWith(token);
632
+ });
633
+
634
+ it("should send correct payload on removeGroup", async () => {
635
+ await mixpanelMain.addGroup(token, "company_id", 111);
636
+ // The company id has been added
637
+ expect(
638
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
639
+ ).toHaveBeenCalledWith(token, {
640
+ company_id: [222, 111],
641
+ superProp1: "value1",
642
+ superProp2: "value2",
643
+ });
644
+ expect(
645
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
646
+ ).toHaveBeenCalledWith(token);
647
+
648
+ await mixpanelMain.removeGroup(token, "company_id", 111);
649
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
650
+ token,
651
+ MixpanelType.USER,
652
+ expect.objectContaining({
653
+ $token: token,
654
+ $time: expect.any(Number),
655
+ $remove: {company_id: 111},
656
+ $distinct_id: "distinct-id-mock",
657
+ $device_id: "device-id-mock",
658
+ $user_id: "user-id-mock",
659
+ })
660
+ );
661
+
662
+ // The company id has been removed
663
+ expect(
664
+ mixpanelMain.mixpanelPersistent.updateSuperProperties
665
+ ).toHaveBeenCalledWith(token, {
666
+ company_id: [222],
667
+ superProp1: "value1",
668
+ superProp2: "value2",
669
+ });
670
+ expect(
671
+ mixpanelMain.mixpanelPersistent.persistSuperProperties
672
+ ).toHaveBeenCalledWith(token);
673
+ });
674
+
675
+ it("should send correct payload on deleteGroup", async () => {
676
+ await mixpanelMain.deleteGroup(token);
677
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
678
+ token,
679
+ MixpanelType.GROUPS,
680
+ expect.objectContaining({
681
+ $token: token,
682
+ $time: expect.any(Number),
683
+ $delete: "null",
684
+ })
685
+ );
686
+ });
687
+
688
+ it("should send correct payload on group set", async () => {
689
+ const properties = {prop1: "value1", prop2: "value2"};
690
+
691
+ await mixpanelMain.groupSetProperties(token, "company_id", 444, properties);
692
+
693
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
694
+ token,
695
+ MixpanelType.GROUPS,
696
+ expect.objectContaining({
697
+ $token: token,
698
+ $time: expect.any(Number),
699
+ $group_id: 444,
700
+ $group_key: "company_id",
701
+ $set: {prop1: "value1", prop2: "value2"},
702
+ })
703
+ );
704
+ });
705
+
706
+ it("should send correct payload on group set once", async () => {
707
+ const properties = {prop1: "value1", prop2: "value2"};
708
+
709
+ await mixpanelMain.groupSetPropertyOnce(
710
+ token,
711
+ "company_id",
712
+ 444,
713
+ properties
714
+ );
715
+
716
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
717
+ token,
718
+ MixpanelType.GROUPS,
719
+ expect.objectContaining({
720
+ $token: token,
721
+ $time: expect.any(Number),
722
+ $group_id: 444,
723
+ $group_key: "company_id",
724
+ $set_once: {prop1: "value1", prop2: "value2"},
725
+ })
726
+ );
727
+ });
728
+
729
+ it("should send correct payload on groupUnsetProperty", async () => {
730
+ await mixpanelMain.groupUnsetProperty(token, "company_id", 444, "prop1");
731
+
732
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
733
+ token,
734
+ MixpanelType.GROUPS,
735
+ expect.objectContaining({
736
+ $token: token,
737
+ $time: expect.any(Number),
738
+ $group_id: 444,
739
+ $group_key: "company_id",
740
+ $unset: ["prop1"],
741
+ })
742
+ );
743
+ });
744
+
745
+ it("should send correct payload on groupRemovePropertyValue", async () => {
746
+ await mixpanelMain.groupRemovePropertyValue(
747
+ token,
748
+ "company_id",
749
+ 444,
750
+ "prop1",
751
+ "value1"
752
+ );
753
+
754
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
755
+ token,
756
+ MixpanelType.GROUPS,
757
+ expect.objectContaining({
758
+ $token: token,
759
+ $time: expect.any(Number),
760
+ $group_id: 444,
761
+ $group_key: "company_id",
762
+ $remove: {prop1: "value1"},
763
+ })
764
+ );
765
+ });
766
+
767
+ it("should send correct payload on groupUnionProperty", async () => {
768
+ await mixpanelMain.groupUnionProperty(
769
+ token,
770
+ "company_id",
771
+ 444,
772
+ "prop1",
773
+ "value1"
774
+ );
775
+
776
+ expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalledWith(
777
+ token,
778
+ MixpanelType.GROUPS,
779
+ expect.objectContaining({
780
+ $token: token,
781
+ $time: expect.any(Number),
782
+ $group_id: 444,
783
+ $group_key: "company_id",
784
+ $union: {prop1: "value1"},
785
+ })
786
+ );
787
+ });
788
+ });