Skip to content

Commit 31e3930

Browse files
kholood-eaahmedAlaaInstabug
authored andcommitted
chore: enhance expo router tracking support (#1272)
1 parent d522fc1 commit 31e3930

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## [Unreleased](https://github.com/Instabug/Instabug-React-Native/compare/v13.3.0...dev)
44

5-
### Fixed
5+
### Added
66

7-
- Fix Omitted non-error objects when logging errors to ensure only error objects are supported.([#1279](https://github.com/Instabug/Instabug-React-Native/pull/1279)).
7+
- Add support for Expo Router navigation tracking ([#1270](https://github.com/Instabug/Instabug-React-Native/pull/1270)).
88

99
## [13.3.0](https://github.com/Instabug/Instabug-React-Native/compare/v13.2.0...v13.3.0) (August 4, 2024)
1010

examples/default/src/App.tsx

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

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

4143
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-
navigationRef.addListener('state', () => {
547+
return navigationRef.addListener('state', () => {
548548
onStateChange(navigationRef.getRootState());
549549
});
550550
};

test/modules/Instabug.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,39 @@ 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+
241243
const mockNavigationContainerRef = {
242244
current: null,
243245
navigate: jest.fn(),
244246
reset: jest.fn(),
245247
goBack: jest.fn(),
246248
dispatch: jest.fn(),
247-
getRootState: jest.fn(),
249+
getRootState: () => mockedState,
248250
canGoBack: jest.fn(),
249-
addListener: jest.fn(),
251+
252+
addListener: jest.fn((event, callback) => {
253+
expect(event).toBe('state');
254+
callback(mockedState);
255+
return jest.fn();
256+
}),
250257
removeListener: jest.fn(),
251258
} as unknown as NavigationContainerRefWithCurrent<ReactNavigation.RootParamList>;
252259

260+
const onStateChangeMock = jest.fn();
261+
262+
jest.spyOn(Instabug, 'onStateChange').mockImplementation(onStateChangeMock);
263+
253264
Instabug.setNavigationListener(mockNavigationContainerRef);
265+
254266
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());
255274
});
256275

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

0 commit comments

Comments
 (0)