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

Feature/services refactoring #76

Closed
wants to merge 8 commits into from
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 '../services/NavigationService';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use aliases while importing.

So, "@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
22 changes: 10 additions & 12 deletions app/services/NavigationService.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import { NavigationActions, StackActions } from '@react-navigation/compat';

import set from 'lodash/set';
/**
* 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 => {
set(navigatorObject, 'navigator', navigatorRef);
};

/**
* Call this function when you want to navigate to a specific route.
*
* @param routeName The name of the route to navigate to. Routes are defined in RootScreen using createStackNavigator()
* @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

Check warning on line 30 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
himanshu-wedensday marked this conversation as resolved.
Show resolved Hide resolved
})
);
}
Expand All @@ -37,19 +39,15 @@
* the main screen: the user should not be able to go back to the splashscreen.
*
* @param routeName The name of the route to navigate to. Routes are defined in RootScreen using createStackNavigator()
* @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 };
32 changes: 32 additions & 0 deletions app/services/tests/navigate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NavigationActions } from '@react-navigation/compat';
import { navigate, setTopLevelNavigator } from '../NavigationService';
jest.mock('@react-navigation/compat', () => ({
NavigationActions: {
navigate: jest.fn()
}
}));
const navigatorRef = { goBack: 'goBack', dispatch: jest.fn() };
setTopLevelNavigator(navigatorRef);
describe('navigate', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('dispatches navigation action with the correct routeName and params', () => {
const routeName = '/test';
const params = { screen: 'MainScreen' };
NavigationActions.navigate.mockReturnValueOnce({
type: 'NAVIGATE_ACTION',
payload: { routeName, params }
});
navigate(routeName, params);
expect(NavigationActions.navigate).toHaveBeenCalledWith({
routeName,
params
});
expect(navigatorRef.dispatch).toHaveBeenCalledWith({
type: 'NAVIGATE_ACTION',
payload: { routeName, params }
});
});
});
34 changes: 34 additions & 0 deletions app/services/tests/navigateAndReset.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { StackActions } from '@react-navigation/compat';
import { setTopLevelNavigator, navigateAndReset } from '../NavigationService';

jest.mock('@react-navigation/compat', () => ({
StackActions: {
replace: jest.fn()
}
}));
const navigatorRef = { goBack: 'goBack', dispatch: jest.fn() };
setTopLevelNavigator(navigatorRef);
describe('test navigateAndReset', () => {
afterEach(() => {
// Reset mocks after each test
jest.clearAllMocks();
});

it('dispatches stack action with the correct routeName and params', () => {
const routeName = '/test';
const params = { screen: 'MainScreen' };
StackActions.replace.mockReturnValueOnce({
type: 'NAVIGATE_ACTION',
payload: { routeName, params }
});
navigateAndReset(routeName, params);
expect(StackActions.replace).toHaveBeenCalledWith({
routeName,
params
});
expect(navigatorRef.dispatch).toHaveBeenCalledWith({
type: 'NAVIGATE_ACTION',
payload: { routeName, params }
});
});
});
20 changes: 20 additions & 0 deletions app/services/tests/setTopLevelNavigation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import set from 'lodash/set';
import { setTopLevelNavigator } from '../NavigationService';
jest.mock('lodash/set', () => jest.fn());
describe('setTopLevelNavigator', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('sets the navigator object with the provided reference', () => {
const navigatorObject = {
navigator: null
};
const navigatorRef = { goBack: 'goBack' };
setTopLevelNavigator(navigatorRef);
expect(set).toHaveBeenCalledWith(
navigatorObject,
'navigator',
navigatorRef
);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MockAdapter from 'axios-mock-adapter';
import { getApiClient } from 'app/utils/apiUtils';
import { getUser } from './UserService';
import { getUser } from '../UserService';

describe('UserService tests', () => {
it('should make the api call to "/quotes?count=1"', async () => {
Expand Down
Loading