Skip to content

Commit

Permalink
feat: Navigation Service Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-wedensday committed Apr 15, 2024
1 parent 9f18cdc commit e32c64f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
4 changes: 2 additions & 2 deletions app/navigators/AppNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createStackNavigator } from '@react-navigation/stack';
import SplashScreen from '@scenes/SplashScreen/';
import ExampleScreen from '@scenes/ExampleScreen';
import { NavigationContainer } from '@react-navigation/native';
import NavigationService from '../services/NavigationService';
import { setTopLevelNavigator } from '@app/services/NavigationService';
const Stack = createStackNavigator();
/**
* The root screen contains the application's navigation.
Expand All @@ -12,7 +12,7 @@ const Stack = createStackNavigator();
*/
export default function AppNavigator() {
return (
<NavigationContainer ref={NavigationService.setTopLevelNavigator}>
<NavigationContainer ref={setTopLevelNavigator}>
<Stack.Navigator headerMode="none" initialRouteName="SplashScreen">
<Stack.Screen name="SplashScreen" component={SplashScreen} />
<Stack.Screen name="MainScreen" component={ExampleScreen} />
Expand Down
24 changes: 12 additions & 12 deletions app/scenes/RootScreen/tests/saga.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@
/* eslint-disable redux-saga/yield-effects */

import { takeLatest } from 'redux-saga/effects';
import NavigationService from 'app/services/NavigationService';
import { navigateAndReset } from '@app/services/NavigationService';
import { timeout } from 'app/utils/testUtils';
import rootScreenSaga, { startup } from '../saga';
import { rootScreenTypes } from '../reducer';

jest.mock('@app/services/NavigationService', () => ({
...jest.requireActual('@app/services/NavigationService'),
navigateAndReset: jest.fn()
}));
describe('Tests for RootScreen sagas', () => {
let generator;
let submitSpy;

beforeEach(() => {
generator = rootScreenSaga();
submitSpy = jest.fn();
afterEach(() => {
jest.clearAllMocks();
});

it('should start task to watch for STARTUP action', () => {
const generator = rootScreenSaga();
expect(generator.next().value).toEqual(
takeLatest(rootScreenTypes.STARTUP, startup)
);
});

Check failure on line 28 in app/scenes/RootScreen/tests/saga.test.js

View workflow job for this annotation

GitHub Actions / Tests' annotations (🧪 jest-coverage-report-action)

Tests for RootScreen sagas > should ensure that the navigation service is called after waiting for 1000ms

TypeError: (0 , _NavigationService.navigateAndReset) is not a function at Timeout._onTimeout (/home/runner/work/react-native-template/react-native-template/app/scenes/RootScreen/saga.js:9:20) at listOnTimeout (node:internal/timers:569:17) at processTimers (node:internal/timers:512:7) Error: expect(jest.fn()).toHaveBeenCalled() Expected number of calls: >= 1 Received number of calls: 0 at call (/home/runner/work/react-native-template/react-native-template/app/scenes/RootScreen/tests/saga.test.js:33:23) at tryCatch (/home/runner/work/react-native-template/react-native-template/node_modules/regenerator-runtime/runtime.js:63:40) at Generator._invoke (/home/runner/work/react-native-template/react-native-template/node_modules/regenerator-runtime/runtime.js:293:22) at Generator.call (/home/runner/work/react-native-template/react-native-template/node_modules/regenerator-runtime/runtime.js:118:21) at tryCatch (/home/runner/work/react-native-template/react-native-template/node_modules/regenerator-runtime/runtime.js:63:40) at invoke (/home/runner/work/react-native-template/react-native-template/node_modules/regenerator-runtime/runtime.js:154:20) at fn (/home/runner/work/react-native-template/react-native-template/node_modules/regenerator-runtime/runtime.js:164:13) at tryCallOne (/home/runner/work/react-native-template/react-native-template/node_modules/react-native/node_modules/promise/lib/core.js:37:12) at call (/home/runner/work/react-native-template/react-native-template/node_modules/react-native/node_modules/promise/lib/core.js:123:15) at flush (/home/runner/work/react-native-template/react-native-template/node_modules/asap/raw.js:50:29)
it('should ensure that the navigation service is called after waiting for 1000ms', async () => {
const method = startup();
NavigationService.navigateAndReset = submitSpy;
method.next();
await timeout(1000);
expect(submitSpy).toHaveBeenCalled();
expect(navigateAndReset).toHaveBeenCalled();
expect(navigateAndReset).toHaveBeenCalledWith('MainScreen');
});

it('should ensure that the navigation service is called after waiting for 1000ms', async () => {
const method = startup();
NavigationService.navigateAndReset = submitSpy;
method.next();
await timeout(650);
expect(submitSpy).not.toHaveBeenCalled();
expect(navigateAndReset).not.toHaveBeenCalled();
await timeout(200);
expect(submitSpy).not.toHaveBeenCalled();
expect(navigateAndReset).not.toHaveBeenCalled();
});
});
21 changes: 9 additions & 12 deletions app/services/NavigationService.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { NavigationActions, StackActions } from '@react-navigation/compat';

/**
* The navigation is implemented as a service so that it can be used outside of components, for example in sagas.
*
* @see https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html
*/

let navigator;
const navigatorObject = {
navigator: null
};

/**
* This function is called when the RootScreen is created to set the navigator instance to use.
*/
function setTopLevelNavigator(navigatorRef) {
navigator = navigatorRef;
}
const setTopLevelNavigator = navigatorRef => {
Object.assign(navigatorObject, { navigator: navigatorRef });
};

/**
* Call this function when you want to navigate to a specific route.
Expand All @@ -22,7 +23,7 @@ function setTopLevelNavigator(navigatorRef) {
* @param params Route parameters.
*/

Check warning on line 24 in app/services/NavigationService.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹 Function is not covered

Warning! Not covered function
function navigate(routeName, params) {
navigator.dispatch(
navigatorObject.navigator.dispatch(
NavigationActions.navigate({
routeName,
params
Expand All @@ -40,16 +41,12 @@ function navigate(routeName, params) {
* @param params Route parameters.
*/

Check warning on line 42 in app/services/NavigationService.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹 Function is not covered

Warning! Not covered function
function navigateAndReset(routeName, params) {
navigator.dispatch(
navigatorObject.navigator.dispatch(
StackActions.replace({
routeName,
params
})

Check warning on line 48 in app/services/NavigationService.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
);
}

export default {
navigate,
navigateAndReset,
setTopLevelNavigator
};
export { navigate, navigateAndReset, setTopLevelNavigator };

0 comments on commit e32c64f

Please sign in to comment.