-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathInstabug.spec.ts
882 lines (691 loc) · 29.4 KB
/
Instabug.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
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
import '../mocks/mockInstabugUtils';
import '../mocks/mockNetworkLogger';
import { Platform, findNodeHandle, processColor } from 'react-native';
import type { NavigationContainerRefWithCurrent } from '@react-navigation/native'; // Import the hook
import { mocked } from 'jest-mock';
import waitForExpect from 'wait-for-expect';
import Report from '../../src/models/Report';
import * as Instabug from '../../src/modules/Instabug';
import * as NetworkLogger from '../../src/modules/NetworkLogger';
import { NativeEvents, NativeInstabug, emitter } from '../../src/native/NativeInstabug';
import {
ColorTheme,
InvocationEvent,
Locale,
LogLevel,
NetworkInterceptionMode,
ReproStepsMode,
StringKey,
WelcomeMessageMode,
} from '../../src/utils/Enums';
import InstabugUtils from '../../src/utils/InstabugUtils';
import type { FeatureFlag } from '../../src/models/FeatureFlag';
import InstabugConstants from '../../src/utils/InstabugConstants';
import { Logger } from '../../src/utils/logger';
describe('Instabug Module', () => {
beforeEach(() => {
const events = Object.values(NativeEvents);
events.forEach((event) => {
emitter.removeAllListeners(event);
});
});
it('should call the native method setEnabled', () => {
Instabug.setEnabled(true);
expect(NativeInstabug.setEnabled).toBeCalledTimes(1);
expect(NativeInstabug.setEnabled).toBeCalledWith(true);
});
it('reportScreenChange should call the native method reportScreenChange', () => {
const screenName = 'some-screen';
Instabug.reportScreenChange(screenName);
expect(NativeInstabug.reportScreenChange).toBeCalledTimes(1);
expect(NativeInstabug.reportScreenChange).toBeCalledWith(screenName);
});
it('componentDidAppearListener should call the native method reportScreenChange', () => {
const screenName = 'some-screen';
Instabug.componentDidAppearListener({
componentId: '1',
componentName: screenName,
componentType: 'Component',
});
expect(NativeInstabug.reportScreenChange).toBeCalledTimes(1);
expect(NativeInstabug.reportScreenChange).toBeCalledWith(screenName);
});
it("componentDidAppearListener shouldn't call the native method reportScreenChange if first screen", async () => {
Instabug.init({
token: 'some-token',
invocationEvents: [InvocationEvent.none],
});
Instabug.componentDidAppearListener({
componentId: '1',
componentName: 'screen',
componentType: 'Component',
});
await waitForExpect(() => {
// Only first screen should be reported
expect(NativeInstabug.reportScreenChange).toBeCalledTimes(1);
expect(NativeInstabug.reportScreenChange).toBeCalledWith('Initial Screen');
});
});
it("componentDidAppearListener shouldn't call the native method reportScreenChange twice if same screen", (done) => {
Array(5).forEach(() => {
Instabug.componentDidAppearListener({
componentId: '1',
componentName: 'screen',
componentType: 'Component',
});
});
setTimeout(() => {
// It won't report any screen change, here's why:
// 1. First call:
// It's the first screen so:
// 1. It doesn't report a screen change
// 2. It sets _isFirstScreen to false
// 3. It sets _lastScreen to "screen"
// 2. Second+ calls:
// The screen name is the same as _lastScreen (stored in 1st call)
// so it doesn't report a screen change
expect(NativeInstabug.reportScreenChange).not.toBeCalled();
done();
}, 1500);
});
it('onNavigationStateChange should call the native method reportScreenChange', async () => {
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
// @ts-ignore
Instabug.onNavigationStateChange('home', 'settings');
await waitForExpect(() => {
expect(NativeInstabug.reportScreenChange).toBeCalledTimes(1);
expect(NativeInstabug.reportScreenChange).toBeCalledWith('settings');
});
});
// eslint-disable-next-line jest/no-disabled-tests
it.skip('onNavigationStateChange should call the native method reportCurrentViewChange on Android Platform', async () => {
Platform.OS = 'android';
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
// @ts-ignore
Instabug.onNavigationStateChange('home', 'settings');
await waitForExpect(() => {
expect(NativeInstabug.reportCurrentViewChange).toBeCalledTimes(1);
expect(NativeInstabug.reportCurrentViewChange).toBeCalledWith('settings');
});
});
// eslint-disable-next-line jest/no-disabled-tests
it.skip('onNavigationStateChange should not call the native method reportCurrentViewChange on iOS Platform', async () => {
Platform.OS = 'ios';
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
// @ts-ignore
Instabug.onNavigationStateChange('home', 'settings');
await waitForExpect(() => {
expect(NativeInstabug.reportCurrentViewChange).not.toBeCalled();
});
});
it('onNavigationStateChange should not call the native method reportScreenChange if screen is the same', (done) => {
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
// @ts-ignore
Instabug.onNavigationStateChange('home', 'home');
// Wait for 1.5s as reportScreenChange is delayed by 1s
setTimeout(() => {
expect(NativeInstabug.reportScreenChange).not.toBeCalled();
done();
}, 1500);
});
it('onNavigationStateChange should not call the native method reportCurrentViewChange if screen is the same', (done) => {
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
// @ts-ignore
Instabug.onNavigationStateChange('home', 'home');
// Wait for 1.5s as reportScreenChange is delayed by 1s
setTimeout(() => {
expect(NativeInstabug.reportCurrentViewChange).not.toBeCalled();
done();
}, 1500);
});
it('onNavigationStateChange should call the native method reportScreenChange immediately if _currentScreen is set', async () => {
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
// sets _currentScreen and waits for 1s as _currentScreen is null
// @ts-ignore
Instabug.onNavigationStateChange('home', 'settings');
// _currentScreen already set in prev call so it reports a screen change immediately
// @ts-ignore
Instabug.onNavigationStateChange('home', 'settings');
expect(NativeInstabug.reportScreenChange).toBeCalledTimes(1);
expect(NativeInstabug.reportScreenChange).toBeCalledWith('settings');
await waitForExpect(() => expect(NativeInstabug.reportScreenChange).toBeCalledTimes(2));
});
it('onStateChange should call the native method reportScreenChange', async () => {
const state = { routes: [{ name: 'ScreenName' }], index: 0 };
// @ts-ignore
Instabug.onStateChange(state);
await waitForExpect(() => {
expect(NativeInstabug.reportScreenChange).toBeCalledTimes(1);
expect(NativeInstabug.reportScreenChange).toBeCalledWith('ScreenName');
});
});
// eslint-disable-next-line jest/no-disabled-tests
it.skip('onStateChange should call the native method reportCurrentViewChange on Android Platform', async () => {
Platform.OS = 'android';
const state = { routes: [{ name: 'ScreenName' }], index: 0 };
// @ts-ignore
Instabug.onStateChange(state);
await waitForExpect(() => {
expect(NativeInstabug.reportCurrentViewChange).toBeCalledTimes(1);
expect(NativeInstabug.reportCurrentViewChange).toBeCalledWith('ScreenName');
});
});
// eslint-disable-next-line jest/no-disabled-tests
it.skip('onStateChange should not call the native method reportCurrentViewChange on iOS Platform', async () => {
Platform.OS = 'ios';
const state = { routes: [{ name: 'ScreenName' }], index: 0 };
// @ts-ignore
Instabug.onStateChange(state);
await waitForExpect(() => {
expect(NativeInstabug.reportCurrentViewChange).not.toBeCalled();
});
});
it('onStateChange should call the native method reportScreenChange immediately if _currentScreen is set', async () => {
// sets _currentScreen and waits for 1s as _currentScreen is null
const state = { routes: [{ name: 'ScreenName' }], index: 0 };
// @ts-ignore
Instabug.onStateChange(state);
// _currentScreen already set in prev call so it reports a screen change immediately
// @ts-ignore
Instabug.onStateChange(state);
expect(NativeInstabug.reportScreenChange).toBeCalledTimes(1);
expect(NativeInstabug.reportScreenChange).toBeCalledWith('ScreenName');
await waitForExpect(() => expect(NativeInstabug.reportScreenChange).toBeCalledTimes(2));
});
it('setNavigationListener should call the onStateChange on a screen change', async () => {
const mockedState = { routes: [{ name: 'ScreenName' }], index: 0 };
const mockNavigationContainerRef = {
current: null,
navigate: jest.fn(),
reset: jest.fn(),
goBack: jest.fn(),
dispatch: jest.fn(),
getRootState: () => mockedState,
canGoBack: jest.fn(),
addListener: jest.fn((event, callback) => {
expect(event).toBe('state');
callback(mockedState);
return jest.fn();
}),
removeListener: jest.fn(),
} as unknown as NavigationContainerRefWithCurrent<ReactNavigation.RootParamList>;
const onStateChangeMock = jest.fn();
jest.spyOn(Instabug, 'onStateChange').mockImplementation(onStateChangeMock);
Instabug.setNavigationListener(mockNavigationContainerRef);
expect(mockNavigationContainerRef.addListener).toBeCalledTimes(1);
expect(mockNavigationContainerRef.addListener).toHaveBeenCalledWith(
'state',
expect.any(Function),
);
expect(onStateChangeMock).toBeCalledTimes(1);
expect(onStateChangeMock).toHaveBeenCalledWith(mockNavigationContainerRef.getRootState());
});
it('should call the native method init', () => {
const instabugConfig = {
token: 'some-token',
invocationEvents: [InvocationEvent.floatingButton, InvocationEvent.shake],
debugLogsLevel: LogLevel.debug,
codePushVersion: '1.1.0',
};
const usesNativeNetworkInterception = false;
Instabug.init(instabugConfig);
expect(NetworkLogger.setEnabled).toBeCalledWith(true);
expect(NativeInstabug.init).toBeCalledTimes(1);
expect(NativeInstabug.init).toBeCalledWith(
instabugConfig.token,
instabugConfig.invocationEvents,
instabugConfig.debugLogsLevel,
usesNativeNetworkInterception,
instabugConfig.codePushVersion,
);
});
it('setCodePushVersion should call native method setCodePushVersion', () => {
const codePushVersion = '123';
Instabug.setCodePushVersion(codePushVersion);
expect(NativeInstabug.setCodePushVersion).toBeCalledTimes(1);
expect(NativeInstabug.setCodePushVersion).toBeCalledWith(codePushVersion);
});
it('init should disable JavaScript interceptor when using native interception mode', () => {
const instabugConfig = {
token: 'some-token',
invocationEvents: [InvocationEvent.floatingButton, InvocationEvent.shake],
debugLogsLevel: LogLevel.debug,
networkInterceptionMode: NetworkInterceptionMode.native,
codePushVersion: '1.1.0',
};
Instabug.init(instabugConfig);
expect(NetworkLogger.setEnabled).not.toBeCalled();
expect(NativeInstabug.init).toBeCalledTimes(1);
expect(NativeInstabug.init).toBeCalledWith(
instabugConfig.token,
instabugConfig.invocationEvents,
instabugConfig.debugLogsLevel,
// usesNativeNetworkInterception should be true when using native interception mode
true,
instabugConfig.codePushVersion,
);
});
it('should report the first screen on SDK initialization', async () => {
Instabug.init({
token: 'some-token',
invocationEvents: [InvocationEvent.none],
});
await waitForExpect(() => {
expect(NativeInstabug.reportScreenChange).toBeCalledTimes(1);
expect(NativeInstabug.reportScreenChange).toBeCalledWith('Initial Screen');
});
});
it('init should call reportCurrentViewChange on Android Platform', async () => {
Platform.OS = 'android';
Instabug.init({
token: 'some-token',
invocationEvents: [InvocationEvent.none],
});
await waitForExpect(() => {
expect(NativeInstabug.reportCurrentViewChange).toBeCalledTimes(1);
expect(NativeInstabug.reportCurrentViewChange).toBeCalledWith('Initial Screen');
});
});
it('init should not call reportCurrentViewChange on ios Platform', async () => {
Platform.OS = 'ios';
Instabug.init({
token: 'some-token',
invocationEvents: [InvocationEvent.none],
});
await waitForExpect(() => {
expect(NativeInstabug.reportCurrentViewChange).not.toBeCalled();
});
});
it('should call the native method setUserData', () => {
const userData = 'userData';
Instabug.setUserData(userData);
expect(NativeInstabug.setUserData).toBeCalledTimes(1);
expect(NativeInstabug.setUserData).toBeCalledWith(userData);
});
it('should call the native method setTrackUserSteps', () => {
Instabug.setTrackUserSteps(true);
expect(NativeInstabug.setTrackUserSteps).toBeCalledTimes(1);
expect(NativeInstabug.setTrackUserSteps).toBeCalledWith(true);
});
it('should call the native method setIBGLogPrintsToConsole', () => {
Platform.OS = 'ios';
Instabug.setIBGLogPrintsToConsole(true);
expect(NativeInstabug.setIBGLogPrintsToConsole).toBeCalledTimes(1);
expect(NativeInstabug.setIBGLogPrintsToConsole).toBeCalledWith(true);
});
it('should not call the native method setIBGLogPrintsToConsole when platform is android', () => {
Platform.OS = 'android';
Instabug.setIBGLogPrintsToConsole(true);
expect(NativeInstabug.setIBGLogPrintsToConsole).not.toBeCalled();
});
it('should call the native method setSessionProfilerEnabled', () => {
Instabug.setSessionProfilerEnabled(true);
expect(NativeInstabug.setSessionProfilerEnabled).toBeCalledTimes(1);
expect(NativeInstabug.setSessionProfilerEnabled).toBeCalledWith(true);
});
it('should call the native method setLocale', () => {
const locale = Locale.english;
Instabug.setLocale(locale);
expect(NativeInstabug.setLocale).toBeCalledTimes(1);
expect(NativeInstabug.setLocale).toBeCalledWith(locale);
});
it('should call the native method setColorTheme', () => {
const theme = ColorTheme.dark;
Instabug.setColorTheme(theme);
expect(NativeInstabug.setColorTheme).toBeCalledTimes(1);
expect(NativeInstabug.setColorTheme).toBeCalledWith(theme);
});
it('should call the native method setPrimaryColor', () => {
const color = '#fff';
Instabug.setPrimaryColor(color);
expect(NativeInstabug.setPrimaryColor).toBeCalledTimes(1);
expect(NativeInstabug.setPrimaryColor).toBeCalledWith(processColor(color));
});
it('should call the native method appendTags', () => {
const tags = ['tag1', 'tag2'];
Instabug.appendTags(tags);
expect(NativeInstabug.appendTags).toBeCalledTimes(1);
expect(NativeInstabug.appendTags).toBeCalledWith(tags);
});
it('should call the native method resetTags', () => {
Instabug.resetTags();
expect(NativeInstabug.resetTags).toBeCalledTimes(1);
});
it('should call native method getTags', async () => {
const expected = ['tag1', 'tag2'];
mocked(NativeInstabug).getTags.mockResolvedValueOnce(expected);
const actual = await Instabug.getTags();
expect(actual).toBe(expected);
expect(NativeInstabug.getTags).toBeCalledTimes(1);
});
it('should call the native method setString', () => {
const string = 'report an issue';
const key = StringKey.reportBug;
Instabug.setString(key, string);
expect(NativeInstabug.setString).toBeCalledTimes(1);
expect(NativeInstabug.setString).toBeCalledWith(string, key);
});
it('should suffix the repro steps list item numbering title string on Android', () => {
Platform.OS = 'android';
const key = StringKey.reproStepsListItemNumberingTitle;
const string = 'Page';
const expected = 'Page #';
Instabug.setString(key, string);
expect(NativeInstabug.setString).toBeCalledTimes(1);
expect(NativeInstabug.setString).toBeCalledWith(expected, key);
});
it('should call the native method identifyUser', () => {
const email = '[email protected]';
const name = 'Instabug';
Instabug.identifyUser(email, name);
expect(NativeInstabug.identifyUser).toBeCalledTimes(1);
expect(NativeInstabug.identifyUser).toBeCalledWith(email, name, undefined);
});
it('identifyUser when id is defined should call the native method identifyUser', () => {
const email = '[email protected]';
const name = 'Instabug';
const id = 'instabug-id';
Instabug.identifyUser(email, name, id);
expect(NativeInstabug.identifyUser).toBeCalledTimes(1);
expect(NativeInstabug.identifyUser).toBeCalledWith(email, name, id);
});
it('should call the native method logOut', () => {
Instabug.logOut();
expect(NativeInstabug.logOut).toBeCalledTimes(1);
});
it('should call the native method logUserEvent', () => {
const event = 'click';
Instabug.logUserEvent(event);
expect(NativeInstabug.logUserEvent).toBeCalledTimes(1);
expect(NativeInstabug.logUserEvent).toBeCalledWith(event);
});
it('should call the native method logVerbose', () => {
const message = 'log';
Instabug.logVerbose(message);
expect(NativeInstabug.logVerbose).toBeCalledTimes(1);
});
it('should not call the native method logVerbose when no message', () => {
// @ts-ignore
Instabug.logVerbose(null);
expect(NativeInstabug.logVerbose).not.toBeCalled();
});
it('should call the native method logDebug', () => {
const message = 'log';
Instabug.logDebug(message);
expect(NativeInstabug.logDebug).toBeCalledTimes(1);
});
it('should not call the native method logDebug when no message', () => {
// @ts-ignore
Instabug.logDebug(null);
expect(NativeInstabug.logDebug).not.toBeCalled();
});
it('should call the native method logInfo', () => {
const message = 'log';
Instabug.logInfo(message);
expect(NativeInstabug.logInfo).toBeCalledTimes(1);
});
it('should not call the native method logInfo when no message', () => {
// @ts-ignore
Instabug.logInfo(null);
expect(NativeInstabug.logInfo).not.toBeCalled();
});
it('should call the native method logWarn', () => {
const message = 'log';
Instabug.logWarn(message);
expect(NativeInstabug.logWarn).toBeCalledTimes(1);
});
it('should not call the native method logWarn when no message', () => {
// @ts-ignore
Instabug.logWarn(null);
expect(NativeInstabug.logWarn).not.toBeCalled();
});
it('should call the native method logError', () => {
const message = 'log';
Instabug.logError(message);
expect(NativeInstabug.logError).toBeCalledTimes(1);
});
it('should not call the native method logError when no message', () => {
// @ts-ignore
Instabug.logError(null);
expect(NativeInstabug.logError).not.toBeCalled();
});
it('should call the native method clearLogs', () => {
Instabug.clearLogs();
expect(NativeInstabug.clearLogs).toBeCalledTimes(1);
});
it('setReproStepsConfig should call the native setReproStepsConfig', () => {
Platform.OS = 'android';
const bug = ReproStepsMode.disabled;
const crash = ReproStepsMode.enabled;
const sessionReplay = ReproStepsMode.enabledWithNoScreenshots;
const config = { bug, crash, sessionReplay };
Instabug.setReproStepsConfig(config);
expect(NativeInstabug.setReproStepsConfig).toBeCalledTimes(1);
expect(NativeInstabug.setReproStepsConfig).toBeCalledWith(bug, crash, sessionReplay);
});
it('setReproStepsConfig should prioritize `all` over `bug`, `crash`, and `sessionReplay`', () => {
Platform.OS = 'android';
const bug = ReproStepsMode.disabled;
const crash = ReproStepsMode.enabled;
const sessionReplay = ReproStepsMode.enabledWithNoScreenshots;
const all = ReproStepsMode.enabledWithNoScreenshots;
const config = { all, bug, crash, sessionReplay };
Instabug.setReproStepsConfig(config);
expect(NativeInstabug.setReproStepsConfig).toBeCalledTimes(1);
expect(NativeInstabug.setReproStepsConfig).toBeCalledWith(all, all, all);
});
it('setReproStepsConfig should use defaults for `bug`, `crash`, and `sessionReplay`', () => {
Platform.OS = 'android';
const config = {};
Instabug.setReproStepsConfig(config);
expect(NativeInstabug.setReproStepsConfig).toBeCalledTimes(1);
expect(NativeInstabug.setReproStepsConfig).toBeCalledWith(
ReproStepsMode.enabled,
ReproStepsMode.enabledWithNoScreenshots,
ReproStepsMode.enabled,
);
});
it.each([
['key', null],
[null, 'value'],
[null, null],
[{}, 'value'],
['key', []],
])("should fail if key and value aren't strings when calling setUserAttribute", (key, value) => {
const logSpy = jest.spyOn(Logger, 'error');
// @ts-ignore
Instabug.setUserAttribute(key, value);
expect(NativeInstabug.setUserAttribute).not.toBeCalled();
expect(logSpy).toHaveBeenCalledWith(InstabugConstants.SET_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE);
logSpy.mockRestore();
});
it('should call the native method setUserAttribute', () => {
const key = 'age';
const value = '24';
Instabug.setUserAttribute(key, value);
expect(NativeInstabug.setUserAttribute).toBeCalledTimes(1);
expect(NativeInstabug.setUserAttribute).toBeCalledWith(key, value);
});
it('should call native method getUserAttribute', async () => {
const key = 'age';
const expected = '21';
mocked(NativeInstabug).getUserAttribute.mockResolvedValueOnce(expected);
const actual = await Instabug.getUserAttribute(key);
expect(actual).toBe(expected);
expect(NativeInstabug.getUserAttribute).toBeCalledTimes(1);
expect(NativeInstabug.getUserAttribute).toBeCalledWith(key);
});
it('should call the native method removeUserAttribute', () => {
const key = 'age';
Instabug.removeUserAttribute(key);
expect(NativeInstabug.removeUserAttribute).toBeCalledTimes(1);
expect(NativeInstabug.removeUserAttribute).toBeCalledWith(key);
});
it.each([[null]])("should fail if key isn't a string when calling removeUserAttribute", (key) => {
const logSpy = jest.spyOn(console, 'error');
// @ts-ignore
Instabug.removeUserAttribute(key);
expect(NativeInstabug.removeUserAttribute).not.toBeCalled();
expect(logSpy).toHaveBeenCalledWith(
InstabugConstants.REMOVE_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE,
);
logSpy.mockRestore();
});
it('should call native method getAllUserAttributes', async () => {
const expected = { type: 'guest' };
mocked(NativeInstabug).getAllUserAttributes.mockResolvedValueOnce(expected);
const actual = await Instabug.getAllUserAttributes();
expect(actual).toBe(expected);
expect(NativeInstabug.getAllUserAttributes).toBeCalledTimes(1);
expect(NativeInstabug.getAllUserAttributes).toBeCalledWith();
});
it('should call the native method clearAllUserAttributes', () => {
Instabug.clearAllUserAttributes();
expect(NativeInstabug.clearAllUserAttributes).toBeCalledTimes(1);
});
it('should call the native method showWelcomeMessageWithMode', () => {
const mode = WelcomeMessageMode.beta;
Instabug.showWelcomeMessage(mode);
expect(NativeInstabug.showWelcomeMessageWithMode).toBeCalledTimes(1);
expect(NativeInstabug.showWelcomeMessageWithMode).toBeCalledWith(mode);
});
it('should call the native method setWelcomeMessageMode', () => {
const mode = WelcomeMessageMode.beta;
Instabug.setWelcomeMessageMode(mode);
expect(NativeInstabug.setWelcomeMessageMode).toBeCalledTimes(1);
expect(NativeInstabug.setWelcomeMessageMode).toBeCalledWith(mode);
});
it('should call the native method setFileAttachment with filePath when platform is ios', () => {
Platform.OS = 'ios';
const path = '~/path';
Instabug.addFileAttachment(path, '');
expect(NativeInstabug.setFileAttachment).toBeCalledTimes(1);
expect(NativeInstabug.setFileAttachment).toBeCalledWith(path);
});
it('should call the native method setFileAttachment with filePath and fileName when platform is android', () => {
Platform.OS = 'android';
const path = '~/path';
const name = 'file';
Instabug.addFileAttachment(path, name);
expect(NativeInstabug.setFileAttachment).toBeCalledTimes(1);
expect(NativeInstabug.setFileAttachment).toBeCalledWith(path, name);
});
it('should call the native method addPrivateView', () => {
Instabug.addPrivateView(0);
expect(NativeInstabug.addPrivateView).toBeCalledTimes(1);
expect(NativeInstabug.addPrivateView).toBeCalledWith(findNodeHandle(0));
});
it('should call the native method removePrivateView', () => {
Instabug.removePrivateView(0);
expect(NativeInstabug.removePrivateView).toBeCalledTimes(1);
expect(NativeInstabug.removePrivateView).toBeCalledWith(findNodeHandle(0));
});
it('should call the native method show', () => {
Instabug.show();
expect(NativeInstabug.show).toBeCalledTimes(1);
});
it('should call the native method setPreSendingHandler with a function', () => {
const callback = jest.fn();
Instabug.onReportSubmitHandler(callback);
expect(NativeInstabug.setPreSendingHandler).toBeCalledTimes(1);
expect(NativeInstabug.setPreSendingHandler).toBeCalledWith(callback);
});
it('should invoke callback on emitting the event IBGpreSendingHandler', (done) => {
const report = {
tags: ['tag1', 'tag2'],
consoleLogs: ['consoleLog'],
instabugLogs: ['instabugLog'],
userAttributes: [{ age: '24' }],
fileAttachments: ['path'],
};
const callback = (rep: Report) => {
expect(rep).toBeInstanceOf(Report);
expect(rep.tags).toBe(report.tags);
expect(rep.consoleLogs).toBe(report.consoleLogs);
expect(rep.instabugLogs).toBe(report.instabugLogs);
expect(rep.userAttributes).toBe(report.userAttributes);
expect(rep.fileAttachments).toBe(report.fileAttachments);
done();
};
Instabug.onReportSubmitHandler(callback);
emitter.emit(NativeEvents.PRESENDING_HANDLER, report);
expect(emitter.listenerCount(NativeEvents.PRESENDING_HANDLER)).toBe(1);
});
it('should call native addExperiments method', () => {
const experiments = ['exp1', 'exp2'];
Instabug.addExperiments(experiments);
expect(NativeInstabug.addExperiments).toBeCalledTimes(1);
expect(NativeInstabug.addExperiments).toBeCalledWith(experiments);
});
it('should call native removeExperiments method', () => {
const experiments = ['exp1', 'exp2'];
Instabug.removeExperiments(experiments);
expect(NativeInstabug.removeExperiments).toBeCalledTimes(1);
expect(NativeInstabug.removeExperiments).toBeCalledWith(experiments);
});
it('should call native clearAllExperiments method', () => {
Instabug.clearAllExperiments();
expect(NativeInstabug.clearAllExperiments).toBeCalledTimes(1);
});
it('should call native addFeatureFlags method', () => {
const featureFlags: Array<FeatureFlag> = [
{
name: 'key1',
variant: 'variant1',
},
{
name: 'key2',
variant: 'variant2',
},
];
const expected: Record<string, string | undefined> = {};
expected.key1 = 'variant1';
expected.key2 = 'variant2';
Instabug.addFeatureFlags(featureFlags);
expect(NativeInstabug.addFeatureFlags).toBeCalledTimes(1);
expect(NativeInstabug.addFeatureFlags).toBeCalledWith(expected);
});
it('should call native addFeatureFlag method', () => {
const featureFlag: FeatureFlag = {
name: 'key1',
variant: 'variant2',
};
const expected: Record<string, string | undefined> = {};
expected.key1 = 'variant2';
Instabug.addFeatureFlag(featureFlag);
expect(NativeInstabug.addFeatureFlags).toBeCalledTimes(1);
expect(NativeInstabug.addFeatureFlags).toBeCalledWith(expected);
});
it('should call native removeFeatureFlags method', () => {
const featureFlags = ['exp1', 'exp2'];
Instabug.removeFeatureFlags(featureFlags);
expect(NativeInstabug.removeFeatureFlags).toBeCalledTimes(1);
expect(NativeInstabug.removeFeatureFlags).toBeCalledWith(featureFlags);
});
it('should call native removeFeatureFlag method', () => {
const featureFlag = 'exp1';
Instabug.removeFeatureFlag(featureFlag);
expect(NativeInstabug.removeFeatureFlags).toBeCalledTimes(1);
expect(NativeInstabug.removeFeatureFlags).toBeCalledWith([featureFlag]);
});
it('should call native removeAllFeatureFlags method', () => {
Instabug.removeAllFeatureFlags();
expect(NativeInstabug.removeAllFeatureFlags).toBeCalledTimes(1);
});
it('should call the native willRedirectToStore method', () => {
Instabug.willRedirectToStore();
expect(NativeInstabug.willRedirectToStore).toBeCalledTimes(1);
});
it('should register W3C flag listener', async () => {
const callback = jest.fn();
Instabug._registerW3CFlagsChangeListener(callback);
expect(NativeInstabug.registerW3CFlagsChangeListener).toBeCalledTimes(1);
});
it('should invoke callback on emitting the event IBGOnNewW3CFlagsUpdateReceivedCallback', () => {
const callback = jest.fn();
Instabug._registerW3CFlagsChangeListener(callback);
emitter.emit(NativeEvents.ON_W3C_FLAGS_CHANGE);
expect(emitter.listenerCount(NativeEvents.ON_W3C_FLAGS_CHANGE)).toBe(1);
expect(callback).toHaveBeenCalled();
});
});