Skip to content

feat: add new repro steps api #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
- Add a new string (`StringKey.reproStepsListItemNumberingTitle`) for overriding the repro steps list item (screen) title for consistency between iOS and Android ([#1002](https://github.com/Instabug/Instabug-React-Native/pull/1002)).
- Add support for RN version 0.73 by updating the `build.gradle` file with the `namespace` ([#1004](https://github.com/Instabug/Instabug-React-Native/pull/1004))
- Add native-side init API which can be used to catch and report startup crashes on android. ([#1012](https://github.com/Instabug/Instabug-React-Native/pull/1012))
- Add the new repro steps configuration API `Instabug.setReproStepsConfig` ([#1024](https://github.com/Instabug/Instabug-React-Native/pull/1024)).

### Deprecated

- Deprecate the old `StringKey.discardAlertCancel` and `StringKey.discardAlertAction` string keys for overriding the discard alert buttons as they had incosistent behavior between iOS and Android ([#1001](https://github.com/Instabug/Instabug-React-Native/pull/1001)).
- Deprecate the old `StringKey.reproStepsListItemNumberingTitle` string key for overriding the repro steps list item (screen) title as it had incosistent behavior between iOS and Android ([#1002](https://github.com/Instabug/Instabug-React-Native/pull/1002)).
- Deprecate `Instabug.setReproStepsMode` in favor of the new `Instabug.setReproStepsConfig` ([#1024](https://github.com/Instabug/Instabug-React-Native/pull/1024)).

## [11.13.0](https://github.com/Instabug/Instabug-React-Native/compare/v11.12.0...v11.13.0) (July 10, 2023)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Instabug.reportScreenChange('screenName');
You can disable Repro Steps using the following API:

```javascript
Instabug.setReproStepsMode(Instabug.reproStepsMode.disabled);
Instabug.setReproStepsConfig({ all: ReproStepsMode.disabled });
```

## Documentation
Expand Down
10 changes: 10 additions & 0 deletions android/src/main/java/com/instabug/reactlibrary/ArgsRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.instabug.library.InstabugColorTheme;
import com.instabug.library.InstabugCustomTextPlaceHolder.Key;
import com.instabug.library.OnSdkDismissCallback.DismissType;
import com.instabug.library.ReproMode;
import com.instabug.library.core.plugin.PluginPromptOption;
import com.instabug.library.extendedbugreport.ExtendedBugReport;
import com.instabug.library.internal.module.InstabugLocale;
Expand Down Expand Up @@ -43,6 +44,7 @@ public ArrayList<T> getAll(ArrayList<String> keys) {
}
}

@SuppressWarnings("deprecation")
static Map<String, Object> getAll() {
return new HashMap<String, Object>() {{
putAll(logLevels);
Expand All @@ -57,6 +59,7 @@ static Map<String, Object> getAll() {
putAll(actionTypes);
putAll(extendedBugReportStates);
putAll(reproStates);
putAll(reproModes);
putAll(sdkLogLevels);
putAll(promptOptions);
putAll(locales);
Expand Down Expand Up @@ -140,12 +143,19 @@ static Map<String, Object> getAll() {
put("disabled", ExtendedBugReport.State.DISABLED);
}};

@Deprecated()
static final ArgsMap<State> reproStates = new ArgsMap<State>() {{
put("reproStepsEnabledWithNoScreenshots", State.ENABLED_WITH_NO_SCREENSHOTS);
put("reproStepsEnabled", State.ENABLED);
put("reproStepsDisabled", State.DISABLED);
}};

static final ArgsMap<Integer> reproModes = new ArgsMap<Integer>() {{
put("reproStepsEnabledWithNoScreenshots", ReproMode.EnableWithNoScreenshots);
put("reproStepsEnabled", ReproMode.EnableWithScreenshots);
put("reproStepsDisabled", ReproMode.Disable);
}};

static final ArgsMap<Integer> sdkLogLevels = new ArgsMap<Integer>() {{
put("sdkDebugLogsLevelNone", com.instabug.library.LogLevel.NONE);
put("sdkDebugLogsLevelError", com.instabug.library.LogLevel.ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.instabug.library.Instabug;
import com.instabug.library.InstabugColorTheme;
import com.instabug.library.InstabugCustomTextPlaceHolder;
import com.instabug.library.IssueType;
import com.instabug.library.LogLevel;
import com.instabug.library.ReproConfigurations;
import com.instabug.library.internal.module.InstabugLocale;
import com.instabug.library.invocation.InstabugInvocationEvent;
import com.instabug.library.logging.InstabugLog;
Expand All @@ -49,6 +51,8 @@
import java.util.Locale;
import java.util.Map;

import javax.annotation.Nullable;


/**
* The type Rn instabug reactnative module.
Expand Down Expand Up @@ -761,12 +765,14 @@ public void run() {
});
}

/**
/**
* Sets whether user steps tracking is visual, non visual or disabled.
*
* @param reproStepsMode A string to set user steps tracking to be
* enabled, non visual or disabled.
*/
@SuppressWarnings("deprecation")
@Deprecated()
@ReactMethod
public void setReproStepsMode(final String reproStepsMode) {
MainThreadHandler.runOnMainThread(new Runnable() {
Expand All @@ -782,6 +788,28 @@ public void run() {
});
}

@ReactMethod
public void setReproStepsConfig(final String bugMode, final String crashMode) {
MainThreadHandler.runOnMainThread(new Runnable() {
@Override
public void run() {
try {
final Integer resolvedBugMode = ArgsRegistry.reproModes.get(bugMode);
final Integer resolvedCrashMode = ArgsRegistry.reproModes.get(crashMode);

final ReproConfigurations config = new ReproConfigurations.Builder()
.setIssueMode(IssueType.Bug, resolvedBugMode)
.setIssueMode(IssueType.Crash, resolvedCrashMode)
.build();

Instabug.setReproConfigurations(config);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Shows the welcome message in a specific mode.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.instabug.library.Instabug;
import com.instabug.library.InstabugColorTheme;
import com.instabug.library.InstabugCustomTextPlaceHolder;
import com.instabug.library.IssueType;
import com.instabug.library.ReproConfigurations;
import com.instabug.library.ReproMode;
import com.instabug.library.internal.module.InstabugLocale;
import com.instabug.library.ui.onboarding.WelcomeMessage;
import com.instabug.library.visualusersteps.State;
Expand All @@ -27,6 +30,7 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.internal.verification.VerificationModeFactory;
Expand All @@ -45,8 +49,10 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -283,6 +289,28 @@ public void tearDown() {
}
}

@Test
public void givenArg$setReproStepsConfig_whenQuery_thenShouldCallNativeApiWithArg() {
String bug = "reproStepsEnabled";
String crash = "reproStepsDisabled";

ReproConfigurations config = mock(ReproConfigurations.class);
MockedConstruction<ReproConfigurations.Builder> mReproConfigurationsBuilder = mockConstruction(ReproConfigurations.Builder.class, (mock, context) -> {
when(mock.setIssueMode(anyInt(), anyInt())).thenReturn(mock);
when(mock.build()).thenReturn(config);
});

rnModule.setReproStepsConfig(bug, crash);

ReproConfigurations.Builder builder = mReproConfigurationsBuilder.constructed().get(0);

verify(builder).setIssueMode(IssueType.Bug, ReproMode.EnableWithScreenshots);
verify(builder).setIssueMode(IssueType.Crash, ReproMode.Disable);
verify(builder).build();

mockInstabug.verify(() -> Instabug.setReproConfigurations(config));
}

@Test
public void givenArg$showWelcomeMessageWithMode_whenQuery_thenShouldCallNativeApiWithArg() {
// given
Expand Down
11 changes: 11 additions & 0 deletions examples/default/ios/InstabugTests/InstabugSampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ - (void)testSetReproStepsMode {
OCMVerify([mock setReproStepsMode:reproStepsMode]);
}

- (void)testSetReproStepsConfig {
id mock = OCMClassMock([Instabug class]);
IBGUserStepsMode bugMode = IBGUserStepsModeDisable;
IBGUserStepsMode crashMode = IBGUserStepsModeEnable;

[self.instabugBridge setReproStepsConfig:bugMode :crashMode];

OCMVerify([mock setReproStepsFor:IBGIssueTypeBug withMode:bugMode]);
OCMVerify([mock setReproStepsFor:IBGIssueTypeCrash withMode:crashMode]);
}

- (void)testSetSdkDebugLogsLevel {
id mock = OCMClassMock([Instabug class]);
IBGSDKDebugLogsLevel sdkDebugLogsLevel = IBGSDKDebugLogsLevelVerbose;
Expand Down
2 changes: 2 additions & 0 deletions ios/RNInstabug/InstabugReactBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

- (void)setReproStepsMode:(IBGUserStepsMode)reproStepsMode;

- (void)setReproStepsConfig:(IBGUserStepsMode)bugMode:(IBGUserStepsMode)crashMode;

- (void)setUserAttribute:(NSString *)key withValue:(NSString *)value;

- (void)getUserAttribute:(NSString *)key
Expand Down
5 changes: 5 additions & 0 deletions ios/RNInstabug/InstabugReactBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ - (dispatch_queue_t)methodQueue {
[Instabug setReproStepsMode:reproStepsMode];
}

RCT_EXPORT_METHOD(setReproStepsConfig:(IBGUserStepsMode)bugMode :(IBGUserStepsMode)crashMode) {
[Instabug setReproStepsFor:IBGIssueTypeBug withMode:bugMode];
[Instabug setReproStepsFor:IBGIssueTypeCrash withMode:crashMode];
}

RCT_EXPORT_METHOD(setFileAttachment:(NSString *)fileLocation) {
NSURL *url = [NSURL URLWithString:fileLocation];
[Instabug addFileAttachmentWithURL:url];
Expand Down
24 changes: 24 additions & 0 deletions src/models/ReproConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { ReproStepsMode } from '../utils/Enums';

export interface ReproConfig {
/**
* Repro steps mode for bug reporting.
*
* @default ReproStepsMode.enabled
*/
bug?: ReproStepsMode;

/**
* Repro steps mode for crash reporting.
*
* @default ReproStepsMode.enabledWithNoScreenshots
*/
crash?: ReproStepsMode;

/**
* Repro steps mode for both bug and crash reporting.
*
* When this is set, `bug` and `crash` will be ignored.
*/
all?: ReproStepsMode;
}
37 changes: 37 additions & 0 deletions src/modules/Instabug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import InstabugUtils, {
} from '../utils/InstabugUtils';
import * as NetworkLogger from './NetworkLogger';
import { captureUnhandledRejections } from '../utils/UnhandledRejectionTracking';
import type { ReproConfig } from '../models/ReproConfig';

let _currentScreen: string | null = null;
let _lastScreen: string | null = null;
Expand Down Expand Up @@ -365,6 +366,8 @@ export const clearLogs = () => {
};

/**
* @deprecated Use {@link setReproStepsConfig} instead.
*
* Sets whether user steps tracking is visual, non visual or disabled.
* User Steps tracking is enabled by default if it's available
* in your current plan.
Expand All @@ -375,6 +378,40 @@ export const setReproStepsMode = (mode: reproStepsMode | ReproStepsMode) => {
NativeInstabug.setReproStepsMode(mode);
};

/**
* Sets the repro steps mode for bugs and crashes.
*
* @param config The repro steps config.
*
* @example
* ```js
* Instabug.setReproStepsConfig({
* bug: ReproStepsMode.enabled,
* crash: ReproStepsMode.disabled,
* });
* ```
*/
export const setReproStepsConfig = (config: ReproConfig) => {
let bug = config.bug ?? ReproStepsMode.enabled;
let crash = config.crash ?? ReproStepsMode.enabledWithNoScreenshots;

if (config.all != null) {
bug = config.all;
crash = config.all;
}

// There's an issue with crashes repro steps with screenshots in the iOS SDK
// at the moment, so we'll map enabled with screenshots to enabled with no
// screenshots to avoid storing the images on disk if it's not needed until
// this issue is fixed in a future version.
if (Platform.OS === 'ios' && crash === ReproStepsMode.enabled) {
/* istanbul ignore next */
crash = ReproStepsMode.enabledWithNoScreenshots;
}

NativeInstabug.setReproStepsConfig(bug, crash);
};

/**
* Sets user attribute to overwrite it's value or create a new one if it doesn't exist.
*
Expand Down
2 changes: 2 additions & 0 deletions src/native/NativeInstabug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export interface InstabugNativeModule extends NativeModule {
setNetworkLoggingEnabled(isEnabled: boolean): void;

// Repro Steps APIs //
/** @deprecated */
setReproStepsMode(mode: ReproStepsMode | reproStepsMode): void;
setReproStepsConfig(bugMode: ReproStepsMode, crashMode: ReproStepsMode): void;
setTrackUserSteps(isEnabled: boolean): void;
reportScreenChange(firstScreen: string): void;
addPrivateView(nativeTag: number | null): void;
Expand Down
12 changes: 9 additions & 3 deletions test/mocks/mockInstabug.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type { InstabugNativeModule } from '../../src/native/NativeInstabug';

/**
* A fake implementation of the NativeConstants object using `Proxy` that
* returns the name of the property as its value instead of hardcoding all
* the constants.
*/
const fakeNativeConstants = new Proxy({}, { get: (_, prop) => prop });

const mockInstabug: InstabugNativeModule = {
getConstants: jest.fn().mockReturnValue(fakeNativeConstants),
addListener: jest.fn(),
removeListeners: jest.fn(),
getConstants: jest.fn().mockReturnValue({
reproStepsListItemNumberingTitle: 'reproStepsListItemNumberingTitle',
}),
setEnabled: jest.fn(),
init: jest.fn(),
setUserData: jest.fn(),
Expand All @@ -29,6 +34,7 @@ const mockInstabug: InstabugNativeModule = {
logDebug: jest.fn(),
clearLogs: jest.fn(),
setReproStepsMode: jest.fn(),
setReproStepsConfig: jest.fn(),
setSdkDebugLogsLevel: jest.fn(),
setUserAttribute: jest.fn(),
getUserAttribute: jest.fn(),
Expand Down
41 changes: 41 additions & 0 deletions test/modules/Instabug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,47 @@ describe('Instabug Module', () => {
expect(NativeInstabug.setReproStepsMode).toBeCalledWith(mode);
});

it('setReproStepsConfig should call the native setReproStepsConfig', () => {
Platform.OS = 'android';

const bug = ReproStepsMode.disabled;
const crash = ReproStepsMode.enabled;
const config = { bug, crash };

Instabug.setReproStepsConfig(config);

expect(NativeInstabug.setReproStepsConfig).toBeCalledTimes(1);
expect(NativeInstabug.setReproStepsConfig).toBeCalledWith(bug, crash);
});

it('setReproStepsConfig should prioritize `all` over `bug` and `crash`', () => {
Platform.OS = 'android';

const bug = ReproStepsMode.disabled;
const crash = ReproStepsMode.enabled;
const all = ReproStepsMode.enabledWithNoScreenshots;
const config = { all, bug, crash };

Instabug.setReproStepsConfig(config);

expect(NativeInstabug.setReproStepsConfig).toBeCalledTimes(1);
expect(NativeInstabug.setReproStepsConfig).toBeCalledWith(all, all);
});

it('setReproStepsConfig should use defaults for `bug` and `crash`', () => {
Platform.OS = 'android';

const config = {};

Instabug.setReproStepsConfig(config);

expect(NativeInstabug.setReproStepsConfig).toBeCalledTimes(1);
expect(NativeInstabug.setReproStepsConfig).toBeCalledWith(
ReproStepsMode.enabled,
ReproStepsMode.enabledWithNoScreenshots,
);
});

it('should call the native method setSdkDebugLogsLevel on iOS', () => {
const debugLevel = Instabug.sdkDebugLogsLevel.sdkDebugLogsLevelVerbose;

Expand Down