Skip to content

Commit aa0ee5a

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 02fa2f5 commit aa0ee5a

File tree

3 files changed

+4
-25
lines changed

3 files changed

+4
-25
lines changed

examples/default/src/App.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export const App: React.FC = () => {
3535
}, []);
3636

3737
useEffect(() => {
38-
const unregisterListener = Instabug.setNavigationListener(navigationRef);
39-
40-
return unregisterListener;
38+
Instabug.setNavigationListener(navigationRef);
4139
}, [navigationRef]);
4240

4341
return (

src/modules/Instabug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ export const onStateChange = (state?: NavigationStateV5) => {
544544
export const setNavigationListener = (
545545
navigationRef: NavigationContainerRefWithCurrent<ReactNavigation.RootParamList>,
546546
) => {
547-
return navigationRef.addListener('state', () => {
547+
navigationRef.addListener('state', () => {
548548
onStateChange(navigationRef.getRootState());
549549
});
550550
};

test/modules/Instabug.spec.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,39 +238,20 @@ describe('Instabug Module', () => {
238238
});
239239

240240
it('setNavigationListener should call the onStateChange on a screen change', async () => {
241-
const mockedState = { routes: [{ name: 'ScreenName' }], index: 0 };
242-
243241
const mockNavigationContainerRef = {
244242
current: null,
245243
navigate: jest.fn(),
246244
reset: jest.fn(),
247245
goBack: jest.fn(),
248246
dispatch: jest.fn(),
249-
getRootState: () => mockedState,
247+
getRootState: jest.fn(),
250248
canGoBack: jest.fn(),
251-
252-
addListener: jest.fn((event, callback) => {
253-
expect(event).toBe('state');
254-
callback(mockedState);
255-
return jest.fn();
256-
}),
249+
addListener: jest.fn(),
257250
removeListener: jest.fn(),
258251
} as unknown as NavigationContainerRefWithCurrent<ReactNavigation.RootParamList>;
259252

260-
const onStateChangeMock = jest.fn();
261-
262-
jest.spyOn(Instabug, 'onStateChange').mockImplementation(onStateChangeMock);
263-
264253
Instabug.setNavigationListener(mockNavigationContainerRef);
265-
266254
expect(mockNavigationContainerRef.addListener).toBeCalledTimes(1);
267-
expect(mockNavigationContainerRef.addListener).toHaveBeenCalledWith(
268-
'state',
269-
expect.any(Function),
270-
);
271-
272-
expect(onStateChangeMock).toBeCalledTimes(1);
273-
expect(onStateChangeMock).toHaveBeenCalledWith(mockNavigationContainerRef.getRootState());
274255
});
275256

276257
it('should call the native method init', () => {

0 commit comments

Comments
 (0)