Skip to content

Commit

Permalink
Merge pull request #87 from wednesday-solutions/feature/services-tests
Browse files Browse the repository at this point in the history
feat: Added Tests for services
  • Loading branch information
alichherawalla authored Apr 16, 2024
2 parents 0209dde + 3f66ccb commit efa2b17
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
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 '@services/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 }
});
});
});
37 changes: 37 additions & 0 deletions app/services/tests/navigateAndReset.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { StackActions } from '@react-navigation/compat';
import {
setTopLevelNavigator,
navigateAndReset
} from '@services/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 }
});
});
});
11 changes: 11 additions & 0 deletions app/services/tests/setTopLevelNavigator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { setTopLevelNavigator } from '@services/navigationService'; // Import the function to be tested

describe('setTopLevelNavigator', () => {
it('should update navigatorObject with the provided navigatorRef using mocked Object.assign', () => {
const mockNavigatorRef = { navigator: 'test_navigator' };
const mockAssign = jest.spyOn(Object, 'assign');
setTopLevelNavigator(mockNavigatorRef);
expect(mockAssign).toHaveBeenCalledTimes(1);
mockAssign.mockRestore();
});
});
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

1 comment on commit efa2b17

@github-actions
Copy link

Choose a reason for hiding this comment

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

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 95.15% 196/206
🟢 Branches 87.8% 36/41
🟢 Functions 93.59% 73/78
🟢 Lines 96.09% 172/179

Test suite run success

52 tests passing in 24 suites.

Report generated by 🧪jest coverage report action from efa2b17

Please sign in to comment.