Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 moakedState = { 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: () => moakedState,
canGoBack: jest.fn(),
addListener: jest.fn(),

addListener: jest.fn((event, callback) => {
expect(event).toBe('state');
callback(moakedState);
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