Skip to content

Commit 7de4651

Browse files
a7medevAli Abdelfattah
andauthored
[MOB-8359] Add Expirements APIs (#671)
* Add Android Experiments API methods * Add iOS Experiments API methods * Add JavaScript Experiments APIs methods * Add Experiments types * Add Experiments APIs to iOS native header file * Test Experiments API methods * Test Android Experiments APIs * Test iOS Experiments APIs Co-authored-by: Ali Abdelfattah <[email protected]>
1 parent 24812d1 commit 7de4651

File tree

10 files changed

+206
-0
lines changed

10 files changed

+206
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Bumps Instabug iOS SDK to v10.11.8
44
* Bumps Instabug Android SDK to v10.13.0
5+
* Adds Instabug Experiments APIs
56
* Adds defensive type checking in Instabug logging APIs
67
* Excludes unnecessary files from the published npm package
78
* Fixes an issue with GraphQL requests not being grouped correctly

InstabugSample/ios/InstabugSampleTests/InstabugSampleTests.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,29 @@ - (void)testClearLogs {
397397
OCMVerify([mock clearAllLogs]);
398398
}
399399

400+
- (void)testAddExperiments {
401+
id mock = OCMClassMock([Instabug class]);
402+
NSArray *experiments = @[@"exp1", @"exp2"];
403+
404+
OCMStub([mock addExperiments:experiments]);
405+
[self.instabugBridge addExperiments:experiments];
406+
OCMVerify([mock addExperiments:experiments]);
407+
}
408+
409+
- (void)testRemoveExperiments {
410+
id mock = OCMClassMock([Instabug class]);
411+
NSArray *experiments = @[@"exp1", @"exp2"];
412+
413+
OCMStub([mock removeExperiments:experiments]);
414+
[self.instabugBridge removeExperiments:experiments];
415+
OCMVerify([mock removeExperiments:experiments]);
416+
}
417+
418+
- (void)testClearAllExperiments {
419+
id mock = OCMClassMock([Instabug class]);
420+
OCMStub([mock clearAllExperiments]);
421+
[self.instabugBridge clearAllExperiments];
422+
OCMVerify([mock clearAllExperiments]);
423+
}
424+
400425
@end

__tests__/index.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ describe('Instabug Module', () => {
6666
const sendHandledJSCrash = sinon.spy(NativeModules.Instabug, 'sendHandledJSCrash');
6767
const sendJSCrash = sinon.spy(NativeModules.Instabug, 'sendJSCrash');
6868
const reportScreenChange = sinon.spy(NativeModules.Instabug, 'reportScreenChange');
69+
const addExperiments = sinon.spy(NativeModules.Instabug, 'addExperiments');
70+
const removeExperiments = sinon.spy(NativeModules.Instabug, 'removeExperiments');
71+
const clearAllExperiments = sinon.spy(NativeModules.Instabug, 'clearAllExperiments');
6972

7073
beforeEach(() => {
7174
startWithToken.resetHistory();
@@ -699,4 +702,20 @@ describe('Instabug Module', () => {
699702

700703
});
701704

705+
it('should call native addExperiments method', () => {
706+
const experiments = ['exp1', 'exp2'];
707+
Instabug.addExperiments(experiments);
708+
expect(addExperiments.calledOnceWithExactly(experiments)).toBeTruthy();
709+
});
710+
711+
it('should call native removeExperiments method', () => {
712+
const experiments = ['exp1', 'exp2'];
713+
Instabug.removeExperiments(experiments);
714+
expect(removeExperiments.calledOnceWithExactly(experiments)).toBeTruthy();
715+
});
716+
717+
it('should call native clearAllExperiments method', () => {
718+
Instabug.clearAllExperiments();
719+
expect(clearAllExperiments.calledOnce).toBeTruthy();
720+
});
702721
});

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,6 +2361,52 @@ private void sendEvent(ReactApplicationContext reactContext,
23612361
.emit(eventName, params);
23622362
}
23632363

2364+
@ReactMethod
2365+
public void addExperiments(final ReadableArray experiments) {
2366+
MainThreadHandler.runOnMainThread(new Runnable() {
2367+
@Override
2368+
public void run() {
2369+
try {
2370+
Object[] objectArray = ArrayUtil.toArray(experiments);
2371+
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
2372+
Instabug.addExperiments(Arrays.asList(stringArray));
2373+
} catch (Exception e) {
2374+
e.printStackTrace();
2375+
}
2376+
}
2377+
});
2378+
}
2379+
2380+
@ReactMethod
2381+
public void removeExperiments(final ReadableArray experiments) {
2382+
MainThreadHandler.runOnMainThread(new Runnable() {
2383+
@Override
2384+
public void run() {
2385+
try {
2386+
Object[] objectArray = ArrayUtil.toArray(experiments);
2387+
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
2388+
Instabug.removeExperiments(Arrays.asList(stringArray));
2389+
} catch (Exception e) {
2390+
e.printStackTrace();
2391+
}
2392+
}
2393+
});
2394+
}
2395+
2396+
@ReactMethod
2397+
public void clearAllExperiments() {
2398+
MainThreadHandler.runOnMainThread(new Runnable() {
2399+
@Override
2400+
public void run() {
2401+
try {
2402+
Instabug.clearAllExperiments();
2403+
} catch (Exception e) {
2404+
e.printStackTrace();
2405+
}
2406+
}
2407+
});
2408+
}
2409+
23642410
@Override
23652411
public Map<String, Object> getConstants() {
23662412
final Map<String, Object> constants = new HashMap<>();

android/src/test/java/com/instabug/reactlibrary/RNInstabugReactnativeModuleTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333

3434
import java.lang.reflect.Field;
3535
import java.lang.reflect.Method;
36+
import java.sql.Array;
3637
import java.util.ArrayList;
3738
import java.util.HashMap;
39+
import java.util.List;
3840
import java.util.Locale;
3941
import java.util.Map;
4042
import java.util.concurrent.Executors;
@@ -570,4 +572,51 @@ public void tearDown() {
570572

571573
}
572574

575+
@Test
576+
public void givenArg$addExperiments_whenQuery_thenShouldCallNativeApiWithArg() {
577+
// given
578+
JavaOnlyArray array = new JavaOnlyArray();
579+
array.pushString("exp1");
580+
array.pushString("exp2");
581+
582+
// when
583+
rnModule.addExperiments(array);
584+
585+
// then
586+
verify(Instabug.class,times(1));
587+
List<String> expectedList = new ArrayList<String>();
588+
expectedList.add("exp1");
589+
expectedList.add("exp2");
590+
Instabug.addExperiments(expectedList);
591+
}
592+
593+
@Test
594+
public void givenArg$removeExperiments_whenQuery_thenShouldCallNativeApiWithArg() {
595+
// given
596+
JavaOnlyArray array = new JavaOnlyArray();
597+
array.pushString("exp1");
598+
array.pushString("exp2");
599+
600+
// when
601+
rnModule.removeExperiments(array);
602+
603+
// then
604+
verify(Instabug.class,times(1));
605+
List<String> expectedList = new ArrayList<String>();
606+
expectedList.add("exp1");
607+
expectedList.add("exp2");
608+
Instabug.removeExperiments(expectedList);
609+
}
610+
611+
@Test
612+
public void given$clearAllExperiments_whenQuery_thenShouldCallNativeApi() {
613+
// given
614+
615+
// when
616+
rnModule.clearAllExperiments();
617+
618+
// then
619+
verify(Instabug.class,times(1));
620+
Instabug.clearAllExperiments();
621+
}
573622
}

index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,24 @@ export function onStateChange(
11731173
export function reportScreenChange(
11741174
screenName: string
11751175
): void;
1176+
1177+
/**
1178+
* Add experiments to next report.
1179+
* @param {string[]} experiments An array of experiments to add to the next report.
1180+
*/
1181+
export function addExperiments(experiments:string[]): void;
1182+
1183+
/**
1184+
* Remove experiments from next report.
1185+
* @param {string[]} experiments An array of experiments to remove from the next report.
1186+
*/
1187+
export function removeExperiments(experiments: string[]): void;
1188+
1189+
/**
1190+
* Clear all experiments
1191+
*/
1192+
export function clearAllExperiments(): void;
1193+
11761194
export function componentDidAppearListener(componentObj:
11771195
{ componentId: any, componentName: any, passProps: any }
11781196
): void;

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,29 @@ const InstabugModule = {
835835
Instabug.reportScreenChange(screenName);
836836
},
837837

838+
/**
839+
* Add experiments to next report.
840+
* @param {string[]} experiments An array of experiments to add to the next report.
841+
*/
842+
addExperiments(experiments) {
843+
Instabug.addExperiments(experiments);
844+
},
845+
846+
/**
847+
* Remove experiments from next report.
848+
* @param {string[]} experiments An array of experiments to remove from the next report.
849+
*/
850+
removeExperiments(experiments) {
851+
Instabug.removeExperiments(experiments);
852+
},
853+
854+
/**
855+
* Clear all experiments
856+
*/
857+
clearAllExperiments() {
858+
Instabug.clearAllExperiments();
859+
},
860+
838861
componentDidAppearListener({ componentId, componentName, passProps }) {
839862
if (_isFirstScreen) {
840863
_lastScreen = componentName;

ios/RNInstabug/InstabugReactBridge.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,14 @@
108108

109109
- (void)setNetworkLoggingEnabled:(BOOL)isEnabled;
110110

111+
/*
112+
+------------------------------------------------------------------------+
113+
| Experiments |
114+
+------------------------------------------------------------------------+
115+
*/
116+
117+
- (void)addExperiments:(NSArray *)experiments;
118+
- (void)removeExperiments:(NSArray *)experiments;
119+
- (void)clearAllExperiments;
120+
111121
@end

ios/RNInstabug/InstabugReactBridge.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,18 @@ - (dispatch_queue_t)methodQueue {
434434
}
435435
}
436436

437+
RCT_EXPORT_METHOD(addExperiments:(NSArray *)experiments) {
438+
[Instabug addExperiments:experiments];
439+
}
440+
441+
RCT_EXPORT_METHOD(removeExperiments:(NSArray *)experiments) {
442+
[Instabug removeExperiments:experiments];
443+
}
444+
445+
RCT_EXPORT_METHOD(clearAllExperiments) {
446+
[Instabug clearAllExperiments];
447+
}
448+
437449
- (NSDictionary *)constantsToExport
438450
{
439451
return @{ @"invocationEventNone" : @(IBGInvocationEventNone),

jest/mockInstabug.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jest.mock("react-native", () => {
4949
sendHandledJSCrash: jest.fn(),
5050
sendJSCrash: jest.fn(),
5151
reportScreenChange: jest.fn(),
52+
addExperiments: jest.fn(),
53+
removeExperiments: jest.fn(),
54+
clearAllExperiments: jest.fn(),
5255
};
5356
RN.NativeModules.IBGBugReporting = {
5457
setFloatingButtonEdge: jest.fn(),

0 commit comments

Comments
 (0)