Skip to content

Commit df66578

Browse files
abdelhamid-f-nasserHeshamMegid
authored andcommitted
fix(android): remove current view update delay (#1080)
Jira ID: MOB-13396
1 parent 9201beb commit df66578

File tree

7 files changed

+152
-1
lines changed

7 files changed

+152
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Bump Instabug iOS SDK to v12.5.0 ([#1085](https://github.com/Instabug/Instabug-React-Native/pull/1085)). [See release notes](https://github.com/instabug/instabug-ios/releases/tag/12.5.0).
88
- Bump Instabug Android SDK to v12.5.1 ([#1088](https://github.com/Instabug/Instabug-React-Native/pull/1085)). See release notes for [v12.5.0](https://github.com/Instabug/android/releases/tag/v12.5.0) and [v12.5.1](https://github.com/Instabug/android/releases/tag/v12.5.1).
99

10+
### Fixed
11+
12+
- Fix a delay issue in reporting the 'Current View' that resulted in displaying outdated values ([#1080](https://github.com/Instabug/Instabug-React-Native/pull/1080)).
13+
1014
## [12.4.0](https://github.com/Instabug/Instabug-React-Native/compare/v12.2.0...v12.4.0) (December 7, 2023)
1115

1216
### Changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,29 @@ public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
916916
});
917917
}
918918

919+
/**
920+
* Reports that the screen name been changed (Current View).
921+
*
922+
* @param screenName string containing the screen name
923+
*
924+
*/
925+
@ReactMethod
926+
public void reportCurrentViewChange(final String screenName) {
927+
MainThreadHandler.runOnMainThread(new Runnable() {
928+
@Override
929+
public void run() {
930+
try {
931+
Method method = getMethod(Class.forName("com.instabug.library.Instabug"), "reportCurrentViewChange", String.class);
932+
if (method != null) {
933+
method.invoke(null, screenName);
934+
}
935+
} catch (Exception e) {
936+
e.printStackTrace();
937+
}
938+
}
939+
});
940+
}
941+
919942
/**
920943
* Reports that the screen has been changed (Repro Steps) the screen sent to this method will be the 'current view' on the dashboard
921944
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,18 @@ public void tearDown() {
482482
}
483483
}
484484

485+
@Test
486+
public void givenString$reportCurrentViewChange_whenQuery_thenShouldCallNativeApiWithString() throws Exception {
487+
// when
488+
rnModule.reportCurrentViewChange("screen");
489+
Method privateStringMethod = getMethod(Class.forName("com.instabug.library.Instabug"), "reportCurrentViewChange", String.class);
490+
privateStringMethod.setAccessible(true);
491+
492+
// then
493+
verify(Instabug.class, VerificationModeFactory.times(1));
494+
privateStringMethod.invoke("reportCurrentViewChange","screen");
495+
}
496+
485497
@Test
486498
public void givenString$reportScreenChange_whenQuery_thenShouldCallNativeApiWithString() throws Exception {
487499
// when

src/modules/Instabug.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ export const setEnabled = (isEnabled: boolean) => {
3434
NativeInstabug.setEnabled(isEnabled);
3535
};
3636

37+
/**
38+
* Reports that the screen name been changed (Current View field on dashboard).
39+
* only for android.
40+
*
41+
* Normally reportScreenChange handles taking a screenshot for reproduction
42+
* steps and the Current View field on the dashboard. But we've faced issues
43+
* in android where we needed to separate them, that's why we only call it
44+
* for android.
45+
*
46+
* @param screenName string containing the screen name
47+
*/
48+
function reportCurrentViewForAndroid(screenName: string | null) {
49+
if (Platform.OS === 'android' && screenName != null) {
50+
NativeInstabug.reportCurrentViewChange(screenName);
51+
}
52+
}
53+
3754
/**
3855
* Initializes the SDK.
3956
* This is the main SDK method that does all the magic. This is the only
@@ -55,6 +72,7 @@ export const init = (config: InstabugConfig) => {
5572
_isFirstScreen = true;
5673
_currentScreen = firstScreen;
5774

75+
reportCurrentViewForAndroid(firstScreen);
5876
setTimeout(() => {
5977
if (_currentScreen === firstScreen) {
6078
NativeInstabug.reportScreenChange(firstScreen);
@@ -458,6 +476,7 @@ export const onNavigationStateChange = (
458476
const prevScreen = InstabugUtils.getActiveRouteName(prevState);
459477

460478
if (prevScreen !== currentScreen) {
479+
reportCurrentViewForAndroid(currentScreen);
461480
if (_currentScreen != null && _currentScreen !== firstScreen) {
462481
NativeInstabug.reportScreenChange(_currentScreen);
463482
_currentScreen = null;
@@ -478,6 +497,7 @@ export const onStateChange = (state?: NavigationStateV5) => {
478497
}
479498

480499
const currentScreen = InstabugUtils.getFullRoute(state);
500+
reportCurrentViewForAndroid(currentScreen);
481501
if (_currentScreen !== null && _currentScreen !== firstScreen) {
482502
NativeInstabug.reportScreenChange(_currentScreen);
483503
_currentScreen = null;

src/native/NativeInstabug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface InstabugNativeModule extends NativeModule {
4444
): void;
4545
setTrackUserSteps(isEnabled: boolean): void;
4646
reportScreenChange(firstScreen: string): void;
47+
reportCurrentViewChange(screenName: string): void;
4748
addPrivateView(nativeTag: number | null): void;
4849
removePrivateView(nativeTag: number | null): void;
4950

test/mocks/mockInstabug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const mockInstabug: InstabugNativeModule = {
4747
show: jest.fn(),
4848
setPreSendingHandler: jest.fn(),
4949
reportScreenChange: jest.fn(),
50+
reportCurrentViewChange: jest.fn(),
5051
addExperiments: jest.fn(),
5152
removeExperiments: jest.fn(),
5253
clearAllExperiments: jest.fn(),

test/modules/Instabug.spec.ts

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,33 @@ describe('Instabug Module', () => {
109109
});
110110
});
111111

112+
// eslint-disable-next-line jest/no-disabled-tests
113+
it.skip('onNavigationStateChange should call the native method reportCurrentViewChange on Android Platform', async () => {
114+
Platform.OS = 'android';
115+
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
116+
117+
// @ts-ignore
118+
Instabug.onNavigationStateChange('home', 'settings');
119+
120+
await waitForExpect(() => {
121+
expect(NativeInstabug.reportCurrentViewChange).toBeCalledTimes(1);
122+
expect(NativeInstabug.reportCurrentViewChange).toBeCalledWith('settings');
123+
});
124+
});
125+
126+
// eslint-disable-next-line jest/no-disabled-tests
127+
it.skip('onNavigationStateChange should not call the native method reportCurrentViewChange on iOS Platform', async () => {
128+
Platform.OS = 'ios';
129+
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
130+
131+
// @ts-ignore
132+
Instabug.onNavigationStateChange('home', 'settings');
133+
134+
await waitForExpect(() => {
135+
expect(NativeInstabug.reportCurrentViewChange).not.toBeCalled();
136+
});
137+
});
138+
112139
it('onNavigationStateChange should not call the native method reportScreenChange if screen is the same', (done) => {
113140
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
114141

@@ -122,7 +149,20 @@ describe('Instabug Module', () => {
122149
}, 1500);
123150
});
124151

125-
it('onNavigationStateChange should call the native method reportScreenChange immediatly if _currentScreen is set', async () => {
152+
it('onNavigationStateChange should not call the native method reportCurrentViewChange if screen is the same', (done) => {
153+
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
154+
155+
// @ts-ignore
156+
Instabug.onNavigationStateChange('home', 'home');
157+
158+
// Wait for 1.5s as reportScreenChange is delayed by 1s
159+
setTimeout(() => {
160+
expect(NativeInstabug.reportCurrentViewChange).not.toBeCalled();
161+
done();
162+
}, 1500);
163+
});
164+
165+
it('onNavigationStateChange should call the native method reportScreenChange immediately if _currentScreen is set', async () => {
126166
InstabugUtils.getActiveRouteName = jest.fn().mockImplementation((screenName) => screenName);
127167

128168
// sets _currentScreen and waits for 1s as _currentScreen is null
@@ -150,6 +190,31 @@ describe('Instabug Module', () => {
150190
});
151191
});
152192

193+
// eslint-disable-next-line jest/no-disabled-tests
194+
it.skip('onStateChange should call the native method reportCurrentViewChange on Android Platform', async () => {
195+
Platform.OS = 'android';
196+
const state = { routes: [{ name: 'ScreenName' }], index: 0 };
197+
// @ts-ignore
198+
Instabug.onStateChange(state);
199+
200+
await waitForExpect(() => {
201+
expect(NativeInstabug.reportCurrentViewChange).toBeCalledTimes(1);
202+
expect(NativeInstabug.reportCurrentViewChange).toBeCalledWith('ScreenName');
203+
});
204+
});
205+
206+
// eslint-disable-next-line jest/no-disabled-tests
207+
it.skip('onStateChange should not call the native method reportCurrentViewChange on iOS Platform', async () => {
208+
Platform.OS = 'ios';
209+
const state = { routes: [{ name: 'ScreenName' }], index: 0 };
210+
// @ts-ignore
211+
Instabug.onStateChange(state);
212+
213+
await waitForExpect(() => {
214+
expect(NativeInstabug.reportCurrentViewChange).not.toBeCalled();
215+
});
216+
});
217+
153218
it('onStateChange should call the native method reportScreenChange immediately if _currentScreen is set', async () => {
154219
// sets _currentScreen and waits for 1s as _currentScreen is null
155220
const state = { routes: [{ name: 'ScreenName' }], index: 0 };
@@ -195,6 +260,31 @@ describe('Instabug Module', () => {
195260
});
196261
});
197262

263+
it('init should call reportCurrentViewChange on Android Platform', async () => {
264+
Platform.OS = 'android';
265+
Instabug.init({
266+
token: 'some-token',
267+
invocationEvents: [InvocationEvent.none],
268+
});
269+
270+
await waitForExpect(() => {
271+
expect(NativeInstabug.reportCurrentViewChange).toBeCalledTimes(1);
272+
expect(NativeInstabug.reportCurrentViewChange).toBeCalledWith('Initial Screen');
273+
});
274+
});
275+
276+
it('init should not call reportCurrentViewChange on ios Platform', async () => {
277+
Platform.OS = 'ios';
278+
Instabug.init({
279+
token: 'some-token',
280+
invocationEvents: [InvocationEvent.none],
281+
});
282+
283+
await waitForExpect(() => {
284+
expect(NativeInstabug.reportCurrentViewChange).not.toBeCalled();
285+
});
286+
});
287+
198288
it('should call the native method setUserData', () => {
199289
const userData = 'userData';
200290
Instabug.setUserData(userData);

0 commit comments

Comments
 (0)