Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enhance expo router tracking support #1272

Merged
merged 9 commits into from
Aug 13, 2024
Merged
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