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

feat: Added Tests for services #87

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 }
});
});
Comment on lines +15 to +31
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Review Summary

The review comment asked to ensure that the mocked navigate function and the navigatorRef.dispatch method are called with the correct parameters in the navigate.test.js file.

Verification Process and Findings

  1. Verification of navigate function call:

    • The navigate function is indeed called with the parameters routeName and params as shown in the test file snippet and confirmed by the script output. This matches the expected behavior described in the review comment.
  2. Verification of navigatorRef.dispatch method call:

    • The script aimed to verify the call to navigatorRef.dispatch did not return any results. This suggests that there might be an issue with the method call or its presence in the codebase as expected.

Conclusion

The navigate function call is verified and behaves as expected. However, there is an issue with the navigatorRef.dispatch method call, as it does not appear in the expected context. This needs further investigation or correction in the codebase.

Analysis chain

Ensure that the mocked navigate function is called with the correct parameters and that the dispatch method of navigatorRef is invoked as expected.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the navigate function and navigatorRef.dispatch are called with the correct parameters.
ast-grep --lang javascript --pattern $'navigate($_, $_)'
ast-grep --lang javascript --pattern $'navigatorRef.dispatch($_)'

Length of output: 198

});
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