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

Conversation

himanshu-wedensday
Copy link
Contributor

@himanshu-wedensday himanshu-wedensday commented Apr 11, 2024

Ticket Link


Related Links


Description


Steps to Reproduce / Test


GIF's


Summary by CodeRabbit

  • Refactor

    • Updated import statements and refactored navigation handling in the app.
  • Tests

    • Added tests for navigation functionalities to ensure robust and error-free navigation actions.
    • Refactored test setup and cases to accommodate changes in navigation logic.
    • Introduced a new test case for the setTopLevelNavigator function.

Copy link

coderabbitai bot commented Apr 11, 2024

Walkthrough

The changes involve refining the navigation system in a React Native application, focusing on import optimizations, named exports utilization, and restructuring the navigation service for enhanced navigator management. Robust testing practices have been integrated to ensure the reliability and functionality of the navigation features.

Changes

File Path Change Summary
app/navigators/AppNavigator.js Updated import to use a named import for setTopLevelNavigator from NavigationService.
app/scenes/ExampleScreen/saga.js Updated import for getUser from @app/services/UserService to @app/services/userService.
app/scenes/RootScreen/index.js Updated import for NavigationService to destructure and import setTopLevelNavigator specifically.
app/scenes/RootScreen/saga.js Replaced NavigationService.navigateAndReset with navigateAndReset from NavigationService.
app/scenes/RootScreen/tests/saga.test.js Updated test setup and cases to directly use navigateAndReset function.
app/services/tests/setTopLevelNavigation.test.js Added a new test case for setTopLevelNavigator function using lodash's set.

🐰✨
A hop, a skip, a code deploy,
The rabbit tweaks with utmost joy.
Navigation flows, like streams so clear,
With tests to check, nothing to fear.
Oh how the digital burrows grow,
With every line, the pathways show! 🌟🌿


Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 28cabca and 6d47b4e.
Files selected for processing (2)
  • app/scenes/RootScreen/tests/saga.test.js (1 hunks)
  • app/services/tests/setTopLevelNavigation.test.js (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/services/tests/setTopLevelNavigation.test.js
Additional Context Used
GitHub Check Runs (1)
Tests' annotations (🧪 jest-coverage-report-action) failure (1)

app/scenes/RootScreen/tests/saga.test.js: [failure] 28-28: 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)

Additional comments not posted (5)
app/scenes/RootScreen/tests/saga.test.js (5)

8-8: Direct import of navigateAndReset enhances clarity and modularity.


13-16: Mock setup for NavigationService correctly uses jest.requireActual to maintain module integrity while mocking specific functions.


33-34: Ensure that navigateAndReset is correctly implemented and recognized as a function in the saga.

Verification successful

The verification script has successfully located the implementation of the navigateAndReset function within the NavigationService.js file. The function is defined to take two parameters (routeName and params) and uses these to dispatch a navigation action. This confirms that navigateAndReset is indeed implemented as a function and is used correctly in the context of navigation.

Given this information, the original review comment suggesting a potential issue with the recognition of navigateAndReset as a function in the saga seems to be incorrect. The function is properly defined and utilized according to the script output.

Conclusion:

The navigateAndReset function is correctly implemented and recognized as a function in the saga. The test case expectations in saga.test.js are aligned with the actual implementation of the function, thus no issues are found in this regard.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the implementation of `navigateAndReset` in the saga
ast-grep --lang javascript --pattern $'function navigateAndReset($_, $_) {
  $$$
}'

Length of output: 552


41-43: Test correctly ensures that premature navigation does not occur.


18-19: Overall test structure is well-organized and follows best practices.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Apr 11, 2024

Coverage report

Total coverage

Status Category Percentage Covered / Total
🟢 Statements 93.17% 191/205
🟡 Branches 87.8% 36/41
🟡 Functions 89.74% 70/78
🟢 Lines 94.88% 389/410

Status of coverage: 🟢 - ok, 🟡 - slightly more than threshold, 🔴 - under the threshold

Report generated by 🧪jest coverage report action from 6d47b4e

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

app/services/NavigationService.js Show resolved Hide resolved
@@ -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"

/**
* Test sagas
*/

/* eslint-disable redux-saga/yield-effects */

import { takeLatest } from 'redux-saga/effects';
import NavigationService from 'app/services/NavigationService';
// import NavigationService from 'app/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.

let's remove unused imports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this has been resolved in rootscreen refactor pr

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

app/scenes/RootScreen/saga.js Outdated Show resolved Hide resolved
app/scenes/RootScreen/index.js Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Comment on lines 15 to 19
const NavigationService = '@app/services/NavigationService';
jest.mock('@app/services/NavigationService', () => ({
...jest.requireActual('@app/services/NavigationService'),
navigateAndReset: jest.fn()
}));
Copy link

Choose a reason for hiding this comment

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

Mock setup for NavigationService is potentially incorrect.

The mock setup for NavigationService uses a string instead of the actual module. This could lead to issues where the mock does not behave as expected because it's not linked to the actual module being tested. Consider importing the module and then mocking it.

- const NavigationService = '@app/services/NavigationService';
+ import NavigationService from '@app/services/NavigationService';

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const NavigationService = '@app/services/NavigationService';
jest.mock('@app/services/NavigationService', () => ({
...jest.requireActual('@app/services/NavigationService'),
navigateAndReset: jest.fn()
}));
import NavigationService from '@app/services/NavigationService';
jest.mock('@app/services/NavigationService', () => ({
...jest.requireActual('@app/services/NavigationService'),
navigateAndReset: jest.fn()
}));

const method = startup();
NavigationService.navigateAndReset = submitSpy;
set(NavigationService, 'navigateAndReset', submitSpy);
Copy link

Choose a reason for hiding this comment

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

Use of set from Lodash to modify NavigationService might not affect the mock.

The use of set to modify NavigationService in the test might not have the intended effect because the NavigationService is a mock and its properties might not be settable in this way. Consider directly assigning the spy to the mocked function.

- set(NavigationService, 'navigateAndReset', submitSpy);
+ NavigationService.navigateAndReset = submitSpy;

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
set(NavigationService, 'navigateAndReset', submitSpy);
NavigationService.navigateAndReset = submitSpy;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants