Skip to content

Commit

Permalink
chore: enhance expo router tracking support (#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
kholood-ea authored and ahmedAlaaInstabug committed Sep 15, 2024
1 parent 99bf412 commit 6e29a7c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion examples/default/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const App: React.FC = () => {
}, []);

useEffect(() => {
Instabug.setNavigationListener(navigationRef);
const unregisterListener = Instabug.setNavigationListener(navigationRef);

return unregisterListener;
}, [navigationRef]);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Instabug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ export const onStateChange = (state?: NavigationStateV5) => {
export const setNavigationListener = (
navigationRef: NavigationContainerRefWithCurrent<ReactNavigation.RootParamList>,
) => {
navigationRef.addListener('state', () => {
return navigationRef.addListener('state', () => {
onStateChange(navigationRef.getRootState());
});
};
Expand Down
23 changes: 21 additions & 2 deletions test/modules/Instabug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReactNavigation.RootParamList>;

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', () => {
Expand Down

0 comments on commit 6e29a7c

Please sign in to comment.