Skip to content

Commit 8a8a122

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 7b10e65 commit 8a8a122

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type React from 'react';
22
import { Platform, findNodeHandle, processColor } from 'react-native';
33

4-
import type { NavigationState as NavigationStateV5 } from '@react-navigation/native';
4+
import type {
5+
NavigationContainerRefWithCurrent,
6+
NavigationState as NavigationStateV5,
7+
} from '@react-navigation/native';
58
import type { ComponentDidAppearEvent } from 'react-native-navigation';
69
import type { NavigationAction, NavigationState as NavigationStateV4 } from 'react-navigation';
710

@@ -533,6 +536,19 @@ export const onStateChange = (state?: NavigationStateV5) => {
533536
}, 1000);
534537
};
535538

539+
/**
540+
* Sets a listener for screen change
541+
* @param navigationRef a refrence of a navigation container
542+
*
543+
*/
544+
export const setNavigationListener = (
545+
navigationRef: NavigationContainerRefWithCurrent<ReactNavigation.RootParamList>,
546+
) => {
547+
navigationRef.addListener('state', () => {
548+
onStateChange(navigationRef.getRootState());
549+
});
550+
};
551+
536552
export const reportScreenChange = (screenName: string) => {
537553
NativeInstabug.reportScreenChange(screenName);
538554
};

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)