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

Refactor:src/screens/UserPortal/Events from Jest to Vitest #2639

Merged

Conversation

abbi4code
Copy link

@abbi4code abbi4code commented Dec 11, 2024

What kind of change does this PR introduce?
This PR migrates the test cases in src/screens/UserPortal/Events from Jest to Vitest, ensuring compatibility with Vitest .

✅ Replace Jest-specific functions and mocks with Vitest equivalents
✅ Ensure all tests in src/screens/UserPortal/Events pass after migration using npm run test:vitest
✅ Maintain the test coverage for the file as 100% after migration
✅ Upload a video or photo for this specific file coverage is 100% in the PR description

Issue Number:

Fixes #2574

Did you add tests for your changes?
Yes

Snapshots/Videos:

Screenshot from 2024-12-11 19-50-31

Other information

  • Replaced hardcoded date strings
    Screenshot from 2024-12-11 19-57-46

  • With dynamic dates using new Date().toLocaleDateString() to ensure the test uses the current date.
    Screenshot from 2024-12-11 19-52-58

  • Updated hardcoded startTime and endTime values from '02:00 PM' and '06:00 PM' to '08:00 AM' and '10:00 AM'
    Screenshot from 2024-12-11 20-05-14
    Screenshot from 2024-12-11 19-59-47

  • To align with the default values set in Events.tsx
    Screenshot from 2024-12-11 20-03-31

Summary by CodeRabbit

  • Tests
    • Transitioned testing framework from Jest to Vitest for the Events component.
    • Updated mock implementations for various dependencies to improve functionality.
    • Enhanced readability of test functions by changing test to it and adding clarifying comments.
    • Adjusted date and time handling in tests to reflect current locale-specific formats.

Copy link

coderabbitai bot commented Dec 11, 2024

Walkthrough

The pull request focuses on refactoring the Events.spec.tsx test file from Jest to Vitest. The changes involve updating the testing framework's syntax, replacing Jest-specific functions like jest.mock() with Vitest equivalents such as vi.mock(), and modifying test function names from test() to it(). The core test cases and their logic remain largely unchanged, ensuring that the component's functionality is preserved during the migration.

Changes

File Change Summary
src/screens/UserPortal/Events/Events.spec.tsx - Added import { vi } from 'vitest'
- Replaced jest.mock() with vi.mock() for multiple modules
- Changed test functions from test() to it()
- Updated window.matchMedia implementation to use vi.fn()

Assessment against linked issues

Objective Addressed Explanation
Replace Jest-specific functions with Vitest equivalents [#2574]
Ensure tests pass using npm run test:vitest [#2574] Requires actual test run verification.
Maintain 100% test coverage [#2574] Requires coverage report verification.

Possibly related issues

Possibly related PRs

Suggested labels

refactor

Suggested reviewers

  • palisadoes

Poem

🐰 In the realm of tests, a rabbit's delight,
Jest transforms to Vitest, shining bright!
Mocks and functions dance with glee,
From old to new, we set them free.
Code evolves, with each small change,
Hopping forward, no longer strange! 🧪


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 50b0f8b and c246c41.

📒 Files selected for processing (1)
  • src/screens/UserPortal/Events/Events.spec.tsx (9 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/screens/UserPortal/Events/Events.spec.tsx

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

Our Pull Request Approval Process

Thanks for contributing!

Testing Your Code

Remember, your PRs won't be reviewed until these criteria are met:

  1. We don't merge PRs with poor code quality.
    1. Follow coding best practices such that CodeRabbit.ai approves your PR.
  2. We don't merge PRs with failed tests.
    1. When tests fail, click on the Details link to learn more.
    2. Write sufficient tests for your changes (CodeCov Patch Test). Your testing level must be better than the target threshold of the repository
    3. Tests may fail if you edit sensitive files. Ask to add the ignore-sensitive-files-pr label if the edits are necessary.
  3. We cannot merge PRs with conflicting files. These must be fixed.

Our policies make our code better.

Reviewers

Do not assign reviewers. Our Queue Monitors will review your PR and assign them.
When your PR has been assigned reviewers contact them to get your code reviewed and approved via:

  1. comments in this PR or
  2. our slack channel

Reviewing Your Code

Your reviewer(s) will have the following roles:

  1. arbitrators of future discussions with other contributors about the validity of your changes
  2. point of contact for evaluating the validity of your work
  3. person who verifies matching issues by others that should be closed.
  4. person who gives general guidance in fixing your tests

CONTRIBUTING.md

Read our CONTRIBUTING.md file. Most importantly:

  1. PRs with issues not assigned to you will be closed by the reviewer
  2. Fix the first comment in the PR so that each issue listed automatically closes

Other

  1. 🎯 Please be considerate of our volunteers' time. Contacting the person who assigned the reviewers is not advised unless they ask for your input. Do not @ the person who did the assignment otherwise.
  2. Read the CONTRIBUTING.md file make

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: 0

🧹 Outside diff range and nitpick comments (2)
src/screens/UserPortal/Volunteer/UpcomingEvents/UpcomingEvents.spec.tsx (1)

93-99: Consider using async setup in beforeAll.

The async mock setup in beforeAll could potentially cause race conditions. Consider using a more explicit setup.

 beforeAll(() => {
-    vi.mock('react-router-dom', async () => {
-      const actual = await vi.importActual('react-router-dom');
-      return {
-        ...actual,
-        useParams: () => ({ orgId: 'orgId' }),
-      };
-    });
+    vi.mock('react-router-dom', () => {
+      return {
+        ...vi.importActual('react-router-dom'),
+        useParams: () => ({ orgId: 'orgId' }),
+      };
+    });
 });
src/screens/UserPortal/Events/Events.spec.tsx (1)

271-279: Consider extracting matchMedia mock.

The matchMedia mock could be moved to a shared test utilities file for reuse across test suites.

Consider creating a testUtils.ts file:

// testUtils.ts
export const setupMatchMedia = () => {
  Object.defineProperty(window, 'matchMedia', {
    writable: true,
    value: vi.fn().mockImplementation((query) => ({
      matches: false,
      media: query,
      onchange: null,
      addListener: vi.fn(),
      removeListener: vi.fn(),
      addEventListener: vi.fn(),
      removeEventListener: vi.fn(),
      dispatchEvent: vi.fn(),
    })),
  });
};
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0630cff and ec5e746.

📒 Files selected for processing (2)
  • src/screens/UserPortal/Events/Events.spec.tsx (8 hunks)
  • src/screens/UserPortal/Volunteer/UpcomingEvents/UpcomingEvents.spec.tsx (2 hunks)
🔇 Additional comments (5)
src/screens/UserPortal/Volunteer/UpcomingEvents/UpcomingEvents.spec.tsx (2)

24-24: LGTM! Well-documented test suite.

The added documentation clearly describes the purpose and scope of the test suite.

Also applies to: 26-32


34-38: LGTM! Clean migration to Vitest mocking.

The toast mock has been correctly migrated from Jest to Vitest.

src/screens/UserPortal/Events/Events.spec.tsx (3)

24-30: LGTM! Comprehensive documentation.

The added documentation clearly outlines the test suite's purpose and scenarios.


34-66: LGTM! Well-structured mock setup.

The mock setup for various dependencies is clean and follows Vitest best practices.


450-454: LGTM! Dynamic date handling.

The hardcoded dates have been replaced with dynamic dates using toLocaleDateString(), which aligns with the PR objectives.

Copy link

codecov bot commented Dec 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.04%. Comparing base (4b45d85) to head (9931a8e).
Report is 3 commits behind head on develop-postgres.

Additional details and impacted files
@@                 Coverage Diff                  @@
##           develop-postgres    #2639      +/-   ##
====================================================
- Coverage             88.35%   85.04%   -3.31%     
====================================================
  Files                   295      312      +17     
  Lines                  7048     8130    +1082     
  Branches               1521     1778     +257     
====================================================
+ Hits                   6227     6914     +687     
- Misses                  617     1045     +428     
+ Partials                204      171      -33     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@palisadoes
Copy link
Contributor

  1. Please make sure coderabbit.ai approves your PR
  2. Make sure all tests pass (except introspection)
  3. Please make sure that the code coverage for your patch reaches at least 95%

Copy link

coderabbitai bot commented Dec 13, 2024

@abbi4code: I will review the changes.

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@abbi4code
Copy link
Author

@CodeRabbit start review

Copy link

coderabbitai bot commented Dec 13, 2024

@abbi4code: I will start a full review of your PR.

✅ Actions performed

Full review triggered.

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

🧹 Outside diff range and nitpick comments (3)
src/screens/UserPortal/Volunteer/UpcomingEvents/UpcomingEvents.spec.tsx (1)

107-107: Consider enhancing mock cleanup

While vi.clearAllMocks() is correct, consider adding vi.restoreAllMocks() to ensure all mocks are properly restored to their original state.

- vi.clearAllMocks();
+ vi.clearAllMocks();
+ vi.restoreAllMocks();
src/screens/UserPortal/Events/Events.spec.tsx (2)

271-279: Consider moving matchMedia mock to test utils

The window.matchMedia mock implementation is correctly migrated to Vitest, but it could be moved to a shared test utils file since it's likely needed across multiple test files.

Consider creating a src/utils/testUtils.ts:

export const setupMatchMedia = () => {
  Object.defineProperty(window, 'matchMedia', {
    writable: true,
    value: vi.fn().mockImplementation((query) => ({
      matches: false,
      media: query,
      onchange: null,
      addListener: vi.fn(),
      removeListener: vi.fn(),
      addEventListener: vi.fn(),
      removeEventListener: vi.fn(),
      dispatchEvent: vi.fn(),
    })),
  });
};

Line range hint 413-417: Remove or restore commented test code

The commented-out test code should either be removed if it's no longer needed or restored if it's required for testing the calendar view functionality.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0630cff and ec5e746.

📒 Files selected for processing (2)
  • src/screens/UserPortal/Events/Events.spec.tsx (8 hunks)
  • src/screens/UserPortal/Volunteer/UpcomingEvents/UpcomingEvents.spec.tsx (2 hunks)
🔇 Additional comments (2)
src/screens/UserPortal/Volunteer/UpcomingEvents/UpcomingEvents.spec.tsx (2)

26-32: Well-documented test suite!

The documentation header clearly describes the purpose and scope of the tests.


34-39: Correctly migrated mocks to Vitest!

The mock implementations have been properly migrated:

  • Replaced jest.mock with vi.mock
  • Using vi.fn() for mock functions
  • Correctly importing actual router implementation with vi.importActual

Also applies to: 93-99

src/screens/UserPortal/Events/Events.spec.tsx Outdated Show resolved Hide resolved
@palisadoes
Copy link
Contributor

Please make the appropriate changes so that coderabbit.ai approves your PR

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 15, 2024
@abbi4code
Copy link
Author

@CodeRabbit start review

Copy link

coderabbitai bot commented Dec 15, 2024

@abbi4code: I will start a full review of your PR.

✅ Actions performed

Full review triggered.

@palisadoes
Copy link
Contributor

  1. Please fix the failing tests. We cannot merge failing code into the code base. If a failing test doesn't appear to be related to your changes, it may be. Our contributors don't always test for all possible outcomes and your changes may have triggered the failure. Make valid adjustments to the code to make it fully tested.
  2. Please also ensure CodeRabbit.ai approves your changes

@abbi4code
Copy link
Author

@CodeRabbit review

Copy link

coderabbitai bot commented Dec 17, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@palisadoes
Copy link
Contributor

  1. Please fix the failing tests. Click on the Details links for more information.
  2. Please make sure that CodeRabbit.ai approves your changes

This was referenced Dec 23, 2024
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