Skip to content

Commit f9e820c

Browse files
kholood-eaahmedAlaaInstabug
authored andcommitted
feat: navigation tracking support with expo router (#1270)
* feat: add screen tracker on screen change listener and tests * feat (example): add screen change listener
1 parent 289f1e5 commit f9e820c

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

examples/default/src/App.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect } from 'react';
22
import { StyleSheet } from 'react-native';
33

44
import { GestureHandlerRootView } from 'react-native-gesture-handler';
5-
import { NavigationContainer } from '@react-navigation/native';
5+
import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native';
66
import Instabug, {
77
CrashReporting,
88
InvocationEvent,
@@ -20,6 +20,7 @@ import { QueryClient, QueryClientProvider } from 'react-query';
2020
const queryClient = new QueryClient();
2121

2222
export const App: React.FC = () => {
23+
const navigationRef = useNavigationContainerRef();
2324
useEffect(() => {
2425
Instabug.init({
2526
token: 'deb1910a7342814af4e4c9210c786f35',
@@ -33,11 +34,15 @@ export const App: React.FC = () => {
3334
});
3435
}, []);
3536

37+
useEffect(() => {
38+
Instabug.setNavigationListener(navigationRef);
39+
}, [navigationRef]);
40+
3641
return (
3742
<GestureHandlerRootView style={styles.root}>
3843
<NativeBaseProvider theme={nativeBaseTheme}>
3944
<QueryClientProvider client={queryClient}>
40-
<NavigationContainer onStateChange={Instabug.onStateChange} theme={navigationTheme}>
45+
<NavigationContainer theme={navigationTheme} ref={navigationRef}>
4146
<RootTabNavigator />
4247
</NavigationContainer>
4348
</QueryClientProvider>

src/modules/Instabug.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type React from 'react';
2-
import { findNodeHandle, Platform, processColor } from 'react-native';
2+
import { Platform, findNodeHandle, processColor } from 'react-native';
33

44
import type {
55
NavigationContainerRefWithCurrent,
@@ -10,8 +10,7 @@ import type { NavigationAction, NavigationState as NavigationStateV4 } from 'rea
1010

1111
import type { InstabugConfig } from '../models/InstabugConfig';
1212
import Report from '../models/Report';
13-
import { emitter, NativeEvents, NativeInstabug } from '../native/NativeInstabug';
14-
import { registerW3CFlagsListener } from '../utils/FeatureFlags';
13+
import { NativeEvents, NativeInstabug, emitter } from '../native/NativeInstabug';
1514
import {
1615
ColorTheme,
1716
Locale,
@@ -68,8 +67,6 @@ export const init = (config: InstabugConfig) => {
6867
InstabugUtils.captureJsErrors();
6968
captureUnhandledRejections();
7069

71-
Platform.OS === 'android' && registerW3CFlagsListener();
72-
7370
// Default networkInterceptionMode to JavaScript
7471
if (config.networkInterceptionMode == null) {
7572
config.networkInterceptionMode = NetworkInterceptionMode.javascript;
@@ -547,7 +544,7 @@ export const onStateChange = (state?: NavigationStateV5) => {
547544
export const setNavigationListener = (
548545
navigationRef: NavigationContainerRefWithCurrent<ReactNavigation.RootParamList>,
549546
) => {
550-
return navigationRef.addListener('state', () => {
547+
navigationRef.addListener('state', () => {
551548
onStateChange(navigationRef.getRootState());
552549
});
553550
};
@@ -643,20 +640,3 @@ export const componentDidAppearListener = (event: ComponentDidAppearEvent) => {
643640
_lastScreen = event.componentName;
644641
}
645642
};
646-
647-
/**
648-
* Sets listener to W3ExternalTraceID flag changes
649-
* @param handler A callback that gets the update value of the flag
650-
*/
651-
export const _registerW3CFlagsChangeListener = (
652-
handler: (payload: {
653-
isW3ExternalTraceIDEnabled: boolean;
654-
isW3ExternalGeneratedHeaderEnabled: boolean;
655-
isW3CaughtHeaderEnabled: boolean;
656-
}) => void,
657-
) => {
658-
emitter.addListener(NativeEvents.ON_W3C_FLAGS_CHANGE, (payload) => {
659-
handler(payload);
660-
});
661-
NativeInstabug.registerW3CFlagsChangeListener();
662-
};

test/modules/Instabug.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import '../mocks/mockInstabugUtils';
22
import '../mocks/mockNetworkLogger';
33

44
import { Platform, findNodeHandle, processColor } from 'react-native';
5+
import type { NavigationContainerRefWithCurrent } from '@react-navigation/native'; // Import the hook
56

67
import { mocked } from 'jest-mock';
78
import waitForExpect from 'wait-for-expect';
@@ -236,6 +237,23 @@ describe('Instabug Module', () => {
236237
await waitForExpect(() => expect(NativeInstabug.reportScreenChange).toBeCalledTimes(2));
237238
});
238239

240+
it('setNavigationListener should call the onStateChange on a screen change', async () => {
241+
const mockNavigationContainerRef = {
242+
current: null,
243+
navigate: jest.fn(),
244+
reset: jest.fn(),
245+
goBack: jest.fn(),
246+
dispatch: jest.fn(),
247+
getRootState: jest.fn(),
248+
canGoBack: jest.fn(),
249+
addListener: jest.fn(),
250+
removeListener: jest.fn(),
251+
} as unknown as NavigationContainerRefWithCurrent<ReactNavigation.RootParamList>;
252+
253+
Instabug.setNavigationListener(mockNavigationContainerRef);
254+
expect(mockNavigationContainerRef.addListener).toBeCalledTimes(1);
255+
});
256+
239257
it('should call the native method init', () => {
240258
const instabugConfig = {
241259
token: 'some-token',

0 commit comments

Comments
 (0)