forked from Iterable/react-native-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterableApi.test.ts
More file actions
1143 lines (947 loc) · 36.7 KB
/
IterableApi.test.ts
File metadata and controls
1143 lines (947 loc) · 36.7 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Platform } from 'react-native';
import { MockRNIterableAPI } from '../../__mocks__/MockRNIterableAPI';
import { IterableApi } from './IterableApi';
import { IterableConfig } from './IterableConfig';
import { IterableAttributionInfo } from './IterableAttributionInfo';
import { IterableCommerceItem } from './IterableCommerceItem';
import { IterableInAppMessage } from '../../inApp/classes/IterableInAppMessage';
import { IterableInAppTrigger } from '../../inApp/classes/IterableInAppTrigger';
import { IterableInAppTriggerType } from '../../inApp/enums/IterableInAppTriggerType';
import { IterableInAppLocation } from '../../inApp/enums/IterableInAppLocation';
import { IterableInAppCloseSource } from '../../inApp/enums/IterableInAppCloseSource';
import { IterableInAppDeleteSource } from '../../inApp/enums/IterableInAppDeleteSource';
import { IterableInAppShowResponse } from '../../inApp/enums/IterableInAppShowResponse';
import { type IterableInboxImpressionRowInfo } from '../../inbox/types/IterableInboxImpressionRowInfo';
// Mock the RNIterableAPI module
jest.mock('../../api', () => ({
__esModule: true,
default: MockRNIterableAPI,
}));
describe('IterableApi', () => {
beforeEach(() => {
jest.clearAllMocks();
});
// ====================================================== //
// ===================== INITIALIZE ===================== //
// ====================================================== //
describe('initializeWithApiKey', () => {
it('should call RNIterableAPI.initializeWithApiKey with correct parameters', async () => {
// GIVEN an API key, config, and version
const apiKey = 'test-api-key';
const config = new IterableConfig();
const version = '1.0.0';
// WHEN initializeWithApiKey is called
const result = await IterableApi.initializeWithApiKey(apiKey, {
config,
version,
});
// THEN RNIterableAPI.initializeWithApiKey is called with correct parameters
expect(MockRNIterableAPI.initializeWithApiKey).toBeCalledWith(
apiKey,
config.toDict(),
version
);
expect(result).toBe(true);
});
it('should use default config when not provided', async () => {
// GIVEN an API key and version
const apiKey = 'test-api-key';
const version = '1.0.0';
// WHEN initializeWithApiKey is called without config
const result = await IterableApi.initializeWithApiKey(apiKey, {
config: new IterableConfig(),
version,
});
// THEN RNIterableAPI.initializeWithApiKey is called with default config
expect(MockRNIterableAPI.initializeWithApiKey).toBeCalledWith(
apiKey,
expect.any(Object),
version
);
expect(result).toBe(true);
});
});
describe('initialize2WithApiKey', () => {
it('should call RNIterableAPI.initialize2WithApiKey with correct parameters', async () => {
// GIVEN an API key, config, version, and endpoint
const apiKey = 'test-api-key';
const config = new IterableConfig();
const version = '1.0.0';
const apiEndPoint = 'https://api.staging.iterable.com';
// WHEN initialize2WithApiKey is called
const result = await IterableApi.initialize2WithApiKey(apiKey, {
config,
version,
apiEndPoint,
});
// THEN RNIterableAPI.initialize2WithApiKey is called with correct parameters
expect(MockRNIterableAPI.initialize2WithApiKey).toBeCalledWith(
apiKey,
config.toDict(),
version,
apiEndPoint
);
expect(result).toBe(true);
});
it('should use default config when not provided', async () => {
// GIVEN an API key, version, and endpoint
const apiKey = 'test-api-key';
const version = '1.0.0';
const apiEndPoint = 'https://api.staging.iterable.com';
// WHEN initialize2WithApiKey is called without config
const result = await IterableApi.initialize2WithApiKey(apiKey, {
version,
apiEndPoint,
config: new IterableConfig(),
});
// THEN RNIterableAPI.initialize2WithApiKey is called with default config
expect(MockRNIterableAPI.initialize2WithApiKey).toBeCalledWith(
apiKey,
expect.any(Object),
version,
apiEndPoint
);
expect(result).toBe(true);
});
});
// ====================================================== //
// ===================== USER MANAGEMENT ================ //
// ====================================================== //
describe('setEmail', () => {
it('should call RNIterableAPI.setEmail with email only', () => {
// GIVEN an email
const email = 'user@example.com';
// WHEN setEmail is called
IterableApi.setEmail(email);
// THEN RNIterableAPI.setEmail is called with email
expect(MockRNIterableAPI.setEmail).toBeCalledWith(email, undefined);
});
it('should call RNIterableAPI.setEmail with email and auth token', () => {
// GIVEN an email and auth token
const email = 'user@example.com';
const authToken = 'jwt-token';
// WHEN setEmail is called
IterableApi.setEmail(email, authToken);
// THEN RNIterableAPI.setEmail is called with email and auth token
expect(MockRNIterableAPI.setEmail).toBeCalledWith(email, authToken);
});
it('should call RNIterableAPI.setEmail with null email', () => {
// GIVEN null email
const email = null;
// WHEN setEmail is called
IterableApi.setEmail(email);
// THEN RNIterableAPI.setEmail is called with null email
expect(MockRNIterableAPI.setEmail).toBeCalledWith(null, undefined);
});
});
describe('getEmail', () => {
it('should return the email from RNIterableAPI', async () => {
// GIVEN a mock email
const expectedEmail = 'user@example.com';
MockRNIterableAPI.email = expectedEmail;
// WHEN getEmail is called
const result = await IterableApi.getEmail();
// THEN the email is returned
expect(result).toBe(expectedEmail);
});
});
describe('setUserId', () => {
it('should call RNIterableAPI.setUserId with userId only', () => {
// GIVEN a userId
const userId = 'user123';
// WHEN setUserId is called
IterableApi.setUserId(userId);
// THEN RNIterableAPI.setUserId is called with userId
expect(MockRNIterableAPI.setUserId).toBeCalledWith(userId, undefined);
});
it('should call RNIterableAPI.setUserId with userId and auth token', () => {
// GIVEN a userId and auth token
const userId = 'user123';
const authToken = 'jwt-token';
// WHEN setUserId is called
IterableApi.setUserId(userId, authToken);
// THEN RNIterableAPI.setUserId is called with userId and auth token
expect(MockRNIterableAPI.setUserId).toBeCalledWith(userId, authToken);
});
it('should call RNIterableAPI.setUserId with null userId', () => {
// GIVEN null userId
const userId = null;
// WHEN setUserId is called
IterableApi.setUserId(userId);
// THEN RNIterableAPI.setUserId is called with null userId
expect(MockRNIterableAPI.setUserId).toBeCalledWith(null, undefined);
});
it('should call RNIterableAPI.setUserId with undefined userId', () => {
// GIVEN undefined userId
const userId = undefined;
// WHEN setUserId is called
IterableApi.setUserId(userId);
// THEN RNIterableAPI.setUserId is called with undefined userId
expect(MockRNIterableAPI.setUserId).toBeCalledWith(undefined, undefined);
});
});
describe('getUserId', () => {
it('should return the userId from RNIterableAPI', async () => {
// GIVEN a mock userId
const expectedUserId = 'user123';
MockRNIterableAPI.userId = expectedUserId;
// WHEN getUserId is called
const result = await IterableApi.getUserId();
// THEN the userId is returned
expect(result).toBe(expectedUserId);
});
});
describe('disableDeviceForCurrentUser', () => {
it('should call RNIterableAPI.disableDeviceForCurrentUser', () => {
// GIVEN no parameters
// WHEN disableDeviceForCurrentUser is called
IterableApi.disableDeviceForCurrentUser();
// THEN RNIterableAPI.disableDeviceForCurrentUser is called
expect(MockRNIterableAPI.disableDeviceForCurrentUser).toBeCalled();
});
});
describe('registerForPush', () => {
it('should call RNIterableAPI.registerForPush', () => {
// GIVEN no parameters
// WHEN registerForPush is called
IterableApi.registerForPush();
// THEN RNIterableAPI.registerForPush is called
expect(MockRNIterableAPI.registerForPush).toBeCalled();
});
});
describe('updateUser', () => {
it('should call RNIterableAPI.updateUser with data fields and merge flag', () => {
// GIVEN data fields and merge flag
const dataFields = { name: 'John', age: 30 };
const mergeNestedObjects = true;
// WHEN updateUser is called
IterableApi.updateUser(dataFields, mergeNestedObjects);
// THEN RNIterableAPI.updateUser is called with correct parameters
expect(MockRNIterableAPI.updateUser).toBeCalledWith(
dataFields,
mergeNestedObjects
);
});
it('should call RNIterableAPI.updateUser with mergeNestedObjects false', () => {
// GIVEN data fields and merge flag set to false
const dataFields = { name: 'Jane' };
const mergeNestedObjects = false;
// WHEN updateUser is called
IterableApi.updateUser(dataFields, mergeNestedObjects);
// THEN RNIterableAPI.updateUser is called with correct parameters
expect(MockRNIterableAPI.updateUser).toBeCalledWith(
dataFields,
mergeNestedObjects
);
});
});
describe('updateEmail', () => {
it('should call RNIterableAPI.updateEmail with email only', () => {
// GIVEN a new email
const email = 'newuser@example.com';
// WHEN updateEmail is called
IterableApi.updateEmail(email);
// THEN RNIterableAPI.updateEmail is called with email
expect(MockRNIterableAPI.updateEmail).toBeCalledWith(email, undefined);
});
it('should call RNIterableAPI.updateEmail with email and auth token', () => {
// GIVEN a new email and auth token
const email = 'newuser@example.com';
const authToken = 'new-jwt-token';
// WHEN updateEmail is called
IterableApi.updateEmail(email, authToken);
// THEN RNIterableAPI.updateEmail is called with email and auth token
expect(MockRNIterableAPI.updateEmail).toBeCalledWith(email, authToken);
});
it('should call RNIterableAPI.updateEmail with null auth token', () => {
// GIVEN a new email and null auth token
const email = 'newuser@example.com';
const authToken = null;
// WHEN updateEmail is called
IterableApi.updateEmail(email, authToken);
// THEN RNIterableAPI.updateEmail is called with email and null auth token
expect(MockRNIterableAPI.updateEmail).toBeCalledWith(email, null);
});
});
// ====================================================== //
// ===================== TRACKING ====================== //
// ====================================================== //
describe('trackPushOpenWithCampaignId', () => {
it('should call RNIterableAPI.trackPushOpenWithCampaignId with all parameters', () => {
// GIVEN push open parameters
const params = {
campaignId: 123,
templateId: 456,
messageId: 'msg123',
appAlreadyRunning: false,
dataFields: { source: 'push' },
};
// WHEN trackPushOpenWithCampaignId is called
IterableApi.trackPushOpenWithCampaignId(params);
// THEN RNIterableAPI.trackPushOpenWithCampaignId is called with correct parameters
expect(MockRNIterableAPI.trackPushOpenWithCampaignId).toBeCalledWith(
params.campaignId,
params.templateId,
params.messageId,
params.appAlreadyRunning,
params.dataFields
);
});
it('should call RNIterableAPI.trackPushOpenWithCampaignId without dataFields', () => {
// GIVEN push open parameters without dataFields
const params = {
campaignId: 123,
templateId: 456,
messageId: 'msg123',
appAlreadyRunning: true,
};
// WHEN trackPushOpenWithCampaignId is called
IterableApi.trackPushOpenWithCampaignId(params);
// THEN RNIterableAPI.trackPushOpenWithCampaignId is called with correct parameters
expect(MockRNIterableAPI.trackPushOpenWithCampaignId).toBeCalledWith(
params.campaignId,
params.templateId,
params.messageId,
params.appAlreadyRunning,
undefined
);
});
it('should call RNIterableAPI.trackPushOpenWithCampaignId with null messageId', () => {
// GIVEN push open parameters with null messageId
const params = {
campaignId: 123,
templateId: 456,
messageId: null,
appAlreadyRunning: false,
};
// WHEN trackPushOpenWithCampaignId is called
IterableApi.trackPushOpenWithCampaignId(params);
// THEN RNIterableAPI.trackPushOpenWithCampaignId is called with correct parameters
expect(MockRNIterableAPI.trackPushOpenWithCampaignId).toBeCalledWith(
params.campaignId,
params.templateId,
null,
params.appAlreadyRunning,
undefined
);
});
});
describe('trackPurchase', () => {
it('should call RNIterableAPI.trackPurchase with all parameters', () => {
// GIVEN purchase parameters
const total = 99.99;
const items = [
new IterableCommerceItem('item1', 'Product 1', 49.99, 1),
new IterableCommerceItem('item2', 'Product 2', 49.99, 1),
];
const dataFields = { currency: 'USD', discount: 10 };
// WHEN trackPurchase is called
IterableApi.trackPurchase({ total, items, dataFields });
// THEN RNIterableAPI.trackPurchase is called with correct parameters
expect(MockRNIterableAPI.trackPurchase).toBeCalledWith(
total,
items,
dataFields
);
});
it('should call RNIterableAPI.trackPurchase without dataFields', () => {
// GIVEN purchase parameters without dataFields
const total = 50.0;
const items = [new IterableCommerceItem('item1', 'Product 1', 50.0, 1)];
// WHEN trackPurchase is called
IterableApi.trackPurchase({ total, items });
// THEN RNIterableAPI.trackPurchase is called with correct parameters
expect(MockRNIterableAPI.trackPurchase).toBeCalledWith(
total,
items,
undefined
);
});
});
describe('trackInAppOpen', () => {
it('should call RNIterableAPI.trackInAppOpen with message and location', () => {
// GIVEN an in-app message and location
const message = new IterableInAppMessage(
'msg123',
456,
new IterableInAppTrigger(IterableInAppTriggerType.immediate),
new Date(),
new Date(),
false,
undefined,
undefined,
false,
0
);
const location = IterableInAppLocation.inApp;
// WHEN trackInAppOpen is called
IterableApi.trackInAppOpen({ message, location });
// THEN RNIterableAPI.trackInAppOpen is called with correct parameters
expect(MockRNIterableAPI.trackInAppOpen).toBeCalledWith(
message.messageId,
location
);
});
});
describe('trackInAppClick', () => {
it('should call RNIterableAPI.trackInAppClick with message, location, and clickedUrl', () => {
// GIVEN an in-app message, location, and clicked URL
const message = new IterableInAppMessage(
'msg123',
456,
new IterableInAppTrigger(IterableInAppTriggerType.immediate),
new Date(),
new Date(),
false,
undefined,
undefined,
false,
0
);
const location = IterableInAppLocation.inApp;
const clickedUrl = 'https://example.com';
// WHEN trackInAppClick is called
IterableApi.trackInAppClick({ message, location, clickedUrl });
// THEN RNIterableAPI.trackInAppClick is called with correct parameters
expect(MockRNIterableAPI.trackInAppClick).toBeCalledWith(
message.messageId,
location,
clickedUrl
);
});
});
describe('trackInAppClose', () => {
it('should call RNIterableAPI.trackInAppClose with message, location, and source', () => {
// GIVEN an in-app message, location, and source
const message = new IterableInAppMessage(
'msg123',
456,
new IterableInAppTrigger(IterableInAppTriggerType.immediate),
new Date(),
new Date(),
false,
undefined,
undefined,
false,
0
);
const location = IterableInAppLocation.inApp;
const source = IterableInAppCloseSource.back;
// WHEN trackInAppClose is called
IterableApi.trackInAppClose({ message, location, source });
// THEN RNIterableAPI.trackInAppClose is called with correct parameters
expect(MockRNIterableAPI.trackInAppClose).toBeCalledWith(
message.messageId,
location,
source,
undefined
);
});
it('should call RNIterableAPI.trackInAppClose with clickedUrl when provided', () => {
// GIVEN an in-app message, location, source, and clicked URL
const message = new IterableInAppMessage(
'msg123',
456,
new IterableInAppTrigger(IterableInAppTriggerType.immediate),
new Date(),
new Date(),
false,
undefined,
undefined,
false,
0
);
const location = IterableInAppLocation.inApp;
const source = IterableInAppCloseSource.link;
const clickedUrl = 'https://example.com';
// WHEN trackInAppClose is called
IterableApi.trackInAppClose({ message, location, source, clickedUrl });
// THEN RNIterableAPI.trackInAppClose is called with correct parameters
expect(MockRNIterableAPI.trackInAppClose).toBeCalledWith(
message.messageId,
location,
source,
clickedUrl
);
});
});
describe('trackEvent', () => {
it('should call RNIterableAPI.trackEvent with name and dataFields', () => {
// GIVEN event name and data fields
const name = 'customEvent';
const dataFields = { category: 'user_action', value: 100 };
// WHEN trackEvent is called
IterableApi.trackEvent({ name, dataFields });
// THEN RNIterableAPI.trackEvent is called with correct parameters
expect(MockRNIterableAPI.trackEvent).toBeCalledWith(name, dataFields);
});
it('should call RNIterableAPI.trackEvent with name only', () => {
// GIVEN event name only
const name = 'simpleEvent';
// WHEN trackEvent is called
IterableApi.trackEvent({ name });
// THEN RNIterableAPI.trackEvent is called with correct parameters
expect(MockRNIterableAPI.trackEvent).toBeCalledWith(name, undefined);
});
});
// ====================================================== //
// ======================= AUTH ======================= //
// ====================================================== //
describe('pauseAuthRetries', () => {
it('should call RNIterableAPI.pauseAuthRetries with true', () => {
// GIVEN pauseRetry is true
const pauseRetry = true;
// WHEN pauseAuthRetries is called
IterableApi.pauseAuthRetries(pauseRetry);
// THEN RNIterableAPI.pauseAuthRetries is called with true
expect(MockRNIterableAPI.pauseAuthRetries).toBeCalledWith(true);
});
it('should call RNIterableAPI.pauseAuthRetries with false', () => {
// GIVEN pauseRetry is false
const pauseRetry = false;
// WHEN pauseAuthRetries is called
IterableApi.pauseAuthRetries(pauseRetry);
// THEN RNIterableAPI.pauseAuthRetries is called with false
expect(MockRNIterableAPI.pauseAuthRetries).toBeCalledWith(false);
});
});
describe('passAlongAuthToken', () => {
it('should call RNIterableAPI.passAlongAuthToken with valid token', () => {
// GIVEN a valid auth token
const authToken = 'valid-jwt-token';
// WHEN passAlongAuthToken is called
IterableApi.passAlongAuthToken(authToken);
// THEN RNIterableAPI.passAlongAuthToken is called with the token
expect(MockRNIterableAPI.passAlongAuthToken).toBeCalledWith(authToken);
});
it('should call RNIterableAPI.passAlongAuthToken with null token', () => {
// GIVEN a null auth token
const authToken = null;
// WHEN passAlongAuthToken is called
IterableApi.passAlongAuthToken(authToken);
// THEN RNIterableAPI.passAlongAuthToken is called with null
expect(MockRNIterableAPI.passAlongAuthToken).toBeCalledWith(null);
});
it('should call RNIterableAPI.passAlongAuthToken with undefined token', () => {
// GIVEN an undefined auth token
const authToken = undefined;
// WHEN passAlongAuthToken is called
IterableApi.passAlongAuthToken(authToken);
// THEN RNIterableAPI.passAlongAuthToken is called with undefined
expect(MockRNIterableAPI.passAlongAuthToken).toBeCalledWith(undefined);
});
});
// ====================================================== //
// ======================= IN-APP ======================= //
// ====================================================== //
describe('inAppConsume', () => {
it('should call RNIterableAPI.inAppConsume with message, location, and source', () => {
// GIVEN an in-app message, location, and delete source
const message = new IterableInAppMessage(
'msg123',
456,
new IterableInAppTrigger(IterableInAppTriggerType.immediate),
new Date(),
new Date(),
false,
undefined,
undefined,
false,
0
);
const location = IterableInAppLocation.inApp;
const source = IterableInAppDeleteSource.deleteButton;
// WHEN inAppConsume is called
IterableApi.inAppConsume(message, location, source);
// THEN RNIterableAPI.inAppConsume is called with correct parameters
expect(MockRNIterableAPI.inAppConsume).toBeCalledWith(
message.messageId,
location,
source
);
});
});
describe('getInAppMessages', () => {
it('should return in-app messages from RNIterableAPI', async () => {
// GIVEN mock in-app messages
const mockMessages = [
new IterableInAppMessage(
'msg1',
123,
new IterableInAppTrigger(IterableInAppTriggerType.immediate),
new Date(),
new Date(),
false,
undefined,
undefined,
false,
0
),
new IterableInAppMessage(
'msg2',
456,
new IterableInAppTrigger(IterableInAppTriggerType.event),
new Date(),
new Date(),
true,
undefined,
undefined,
false,
0
),
];
MockRNIterableAPI.messages = mockMessages;
// WHEN getInAppMessages is called
const result = await IterableApi.getInAppMessages();
// THEN the messages are returned
expect(result).toBe(mockMessages);
});
});
describe('getInboxMessages', () => {
it('should return inbox messages from RNIterableAPI', async () => {
// GIVEN mock inbox messages
const mockMessages = [
new IterableInAppMessage(
'msg1',
123,
new IterableInAppTrigger(IterableInAppTriggerType.immediate),
new Date(),
new Date(),
true, // saveToInbox
undefined,
undefined,
false,
0
),
];
MockRNIterableAPI.messages = mockMessages;
// WHEN getInboxMessages is called
const result = await IterableApi.getInboxMessages();
// THEN the messages are returned
expect(result).toStrictEqual(mockMessages);
});
});
describe('showMessage', () => {
it('should call RNIterableAPI.showMessage with messageId and consume flag', async () => {
// GIVEN a message ID and consume flag
const messageId = 'msg123';
const consume = true;
const expectedUrl = 'https://example.com';
MockRNIterableAPI.clickedUrl = expectedUrl;
// WHEN showMessage is called
const result = await IterableApi.showMessage(messageId, consume);
// THEN RNIterableAPI.showMessage is called with correct parameters
expect(MockRNIterableAPI.showMessage).toBeCalledWith(messageId, consume);
expect(result).toBe(expectedUrl);
});
it('should call RNIterableAPI.showMessage with consume set to false', async () => {
// GIVEN a message ID and consume flag set to false
const messageId = 'msg123';
const consume = false;
// WHEN showMessage is called
await IterableApi.showMessage(messageId, consume);
// THEN RNIterableAPI.showMessage is called with consume set to false
expect(MockRNIterableAPI.showMessage).toBeCalledWith(messageId, false);
});
});
describe('removeMessage', () => {
it('should call RNIterableAPI.removeMessage with messageId, location, and source', () => {
// GIVEN a message ID, location, and source
const messageId = 'msg123';
const location = 1; // IterableInAppLocation.inApp
const source = 2; // IterableInAppDeleteSource.deleteButton
// WHEN removeMessage is called
IterableApi.removeMessage(messageId, location, source);
// THEN RNIterableAPI.removeMessage is called with correct parameters
expect(MockRNIterableAPI.removeMessage).toBeCalledWith(
messageId,
location,
source
);
});
});
describe('setReadForMessage', () => {
it('should call RNIterableAPI.setReadForMessage with messageId and read status', () => {
// GIVEN a message ID and read status
const messageId = 'msg123';
const read = true;
// WHEN setReadForMessage is called
IterableApi.setReadForMessage(messageId, read);
// THEN RNIterableAPI.setReadForMessage is called with correct parameters
expect(MockRNIterableAPI.setReadForMessage).toBeCalledWith(
messageId,
read
);
});
it('should call RNIterableAPI.setReadForMessage with read set to false', () => {
// GIVEN a message ID and read status set to false
const messageId = 'msg123';
const read = false;
// WHEN setReadForMessage is called
IterableApi.setReadForMessage(messageId, read);
// THEN RNIterableAPI.setReadForMessage is called with read set to false
expect(MockRNIterableAPI.setReadForMessage).toBeCalledWith(
messageId,
false
);
});
});
describe('setAutoDisplayPaused', () => {
it('should call RNIterableAPI.setAutoDisplayPaused with true', () => {
// GIVEN autoDisplayPaused is true
const autoDisplayPaused = true;
// WHEN setAutoDisplayPaused is called
IterableApi.setAutoDisplayPaused(autoDisplayPaused);
// THEN RNIterableAPI.setAutoDisplayPaused is called with true
expect(MockRNIterableAPI.setAutoDisplayPaused).toBeCalledWith(true);
});
it('should call RNIterableAPI.setAutoDisplayPaused with false', () => {
// GIVEN autoDisplayPaused is false
const autoDisplayPaused = false;
// WHEN setAutoDisplayPaused is called
IterableApi.setAutoDisplayPaused(autoDisplayPaused);
// THEN RNIterableAPI.setAutoDisplayPaused is called with false
expect(MockRNIterableAPI.setAutoDisplayPaused).toBeCalledWith(false);
});
});
describe('getHtmlInAppContentForMessage', () => {
it('should call RNIterableAPI.getHtmlInAppContentForMessage with messageId', async () => {
// GIVEN a message ID
const messageId = 'msg123';
const mockContent = { html: '<div>Test content</div>' };
MockRNIterableAPI.getHtmlInAppContentForMessage = jest
.fn()
.mockResolvedValue(mockContent);
// WHEN getHtmlInAppContentForMessage is called
const result = await IterableApi.getHtmlInAppContentForMessage(messageId);
// THEN RNIterableAPI.getHtmlInAppContentForMessage is called with messageId
expect(MockRNIterableAPI.getHtmlInAppContentForMessage).toBeCalledWith(
messageId
);
expect(result).toBe(mockContent);
});
});
describe('setInAppShowResponse', () => {
it('should call RNIterableAPI.setInAppShowResponse with response', () => {
// GIVEN an in-app show response
const response = IterableInAppShowResponse.show;
// WHEN setInAppShowResponse is called
IterableApi.setInAppShowResponse(response);
// THEN RNIterableAPI.setInAppShowResponse is called with response
expect(MockRNIterableAPI.setInAppShowResponse).toBeCalledWith(response);
});
});
describe('startSession', () => {
it('should call RNIterableAPI.startSession with visible rows', () => {
// GIVEN visible rows
const visibleRows: IterableInboxImpressionRowInfo[] = [
{ messageId: 'msg1', silentInbox: true },
{ messageId: 'msg2', silentInbox: false },
];
// WHEN startSession is called
IterableApi.startSession(visibleRows);
// THEN RNIterableAPI.startSession is called with visible rows
expect(MockRNIterableAPI.startSession).toBeCalledWith(visibleRows);
});
});
describe('endSession', () => {
it('should call RNIterableAPI.endSession', () => {
// GIVEN no parameters
// WHEN endSession is called
IterableApi.endSession();
// THEN RNIterableAPI.endSession is called
expect(MockRNIterableAPI.endSession).toBeCalled();
});
});
describe('updateVisibleRows', () => {
it('should call RNIterableAPI.updateVisibleRows with visible rows', () => {
// GIVEN visible rows
const visibleRows: IterableInboxImpressionRowInfo[] = [
{ messageId: 'msg1', silentInbox: true },
];
// WHEN updateVisibleRows is called
IterableApi.updateVisibleRows(visibleRows);
// THEN RNIterableAPI.updateVisibleRows is called with visible rows
expect(MockRNIterableAPI.updateVisibleRows).toBeCalledWith(visibleRows);
});
it('should call RNIterableAPI.updateVisibleRows with empty array when no rows provided', () => {
// GIVEN no visible rows
// WHEN updateVisibleRows is called without parameters
IterableApi.updateVisibleRows();
// THEN RNIterableAPI.updateVisibleRows is called with empty array
expect(MockRNIterableAPI.updateVisibleRows).toBeCalledWith([]);
});
});
// ====================================================== //
// ======================= MOSC ======================= //
// ====================================================== //
describe('updateCart', () => {
it('should call RNIterableAPI.updateCart with items', () => {
// GIVEN cart items
const items = [
new IterableCommerceItem('item1', 'Product 1', 25.99, 2),
new IterableCommerceItem('item2', 'Product 2', 15.99, 1),
];
// WHEN updateCart is called
IterableApi.updateCart(items);
// THEN RNIterableAPI.updateCart is called with items
expect(MockRNIterableAPI.updateCart).toBeCalledWith(items);
});
});
describe('wakeApp', () => {
it('should call RNIterableAPI.wakeApp on Android', () => {
// GIVEN Android platform
const originalPlatform = Platform.OS;
Object.defineProperty(Platform, 'OS', {
value: 'android',
writable: true,
});
// WHEN wakeApp is called
IterableApi.wakeApp();
// THEN RNIterableAPI.wakeApp is called
expect(MockRNIterableAPI.wakeApp).toBeCalled();
// Restore original platform
Object.defineProperty(Platform, 'OS', {
value: originalPlatform,
writable: true,
});
});
it('should not call RNIterableAPI.wakeApp on iOS', () => {
// GIVEN iOS platform
const originalPlatform = Platform.OS;
Object.defineProperty(Platform, 'OS', {
value: 'ios',
writable: true,
});
// WHEN wakeApp is called
IterableApi.wakeApp();
// THEN RNIterableAPI.wakeApp is not called
expect(MockRNIterableAPI.wakeApp).not.toBeCalled();
// Restore original platform
Object.defineProperty(Platform, 'OS', {
value: originalPlatform,
writable: true,
});
});
});
describe('handleAppLink', () => {