diff --git a/CHANGELOG.md b/CHANGELOG.md index 9400332650..acb003486f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased](https://github.com/Instabug/Instabug-React-Native/compare/v13.3.0...dev) + +### Added + +- Add support for Expo Router navigation tracking ([#1270](https://github.com/Instabug/Instabug-React-Native/pull/1270)). + ## [13.3.0](https://github.com/Instabug/Instabug-React-Native/compare/v13.2.0...v13.3.0) (August 4, 2024) ### Added diff --git a/examples/default/src/App.tsx b/examples/default/src/App.tsx index ae548f97c5..33a3c34f94 100644 --- a/examples/default/src/App.tsx +++ b/examples/default/src/App.tsx @@ -35,7 +35,9 @@ export const App: React.FC = () => { }, []); useEffect(() => { - Instabug.setNavigationListener(navigationRef); + const unregisterListener = Instabug.setNavigationListener(navigationRef); + + return unregisterListener; }, [navigationRef]); return ( diff --git a/src/modules/Instabug.ts b/src/modules/Instabug.ts index b0a05032c4..29ff1d27c9 100644 --- a/src/modules/Instabug.ts +++ b/src/modules/Instabug.ts @@ -544,7 +544,7 @@ export const onStateChange = (state?: NavigationStateV5) => { export const setNavigationListener = ( navigationRef: NavigationContainerRefWithCurrent, ) => { - navigationRef.addListener('state', () => { + return navigationRef.addListener('state', () => { onStateChange(navigationRef.getRootState()); }); }; diff --git a/test/modules/Instabug.spec.ts b/test/modules/Instabug.spec.ts index 518e53ce93..fdc1b91b24 100644 --- a/test/modules/Instabug.spec.ts +++ b/test/modules/Instabug.spec.ts @@ -238,20 +238,39 @@ describe('Instabug Module', () => { }); it('setNavigationListener should call the onStateChange on a screen change', async () => { + const mockedState = { routes: [{ name: 'ScreenName' }], index: 0 }; + const mockNavigationContainerRef = { current: null, navigate: jest.fn(), reset: jest.fn(), goBack: jest.fn(), dispatch: jest.fn(), - getRootState: jest.fn(), + getRootState: () => mockedState, canGoBack: jest.fn(), - addListener: jest.fn(), + + addListener: jest.fn((event, callback) => { + expect(event).toBe('state'); + callback(mockedState); + return jest.fn(); + }), removeListener: jest.fn(), } as unknown as NavigationContainerRefWithCurrent; + const onStateChangeMock = jest.fn(); + + jest.spyOn(Instabug, 'onStateChange').mockImplementation(onStateChangeMock); + Instabug.setNavigationListener(mockNavigationContainerRef); + expect(mockNavigationContainerRef.addListener).toBeCalledTimes(1); + expect(mockNavigationContainerRef.addListener).toHaveBeenCalledWith( + 'state', + expect.any(Function), + ); + + expect(onStateChangeMock).toBeCalledTimes(1); + expect(onStateChangeMock).toHaveBeenCalledWith(mockNavigationContainerRef.getRootState()); }); it('should call the native method init', () => {