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

Refactored src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.test.tsx from Jest to Vitest #2588

Open
wants to merge 4 commits into
base: develop-postgres
Choose a base branch
from

Conversation

bitbard3
Copy link

@bitbard3 bitbard3 commented Dec 2, 2024

…wn.test.tsx from Jest to Vitest #2493

What kind of change does this PR introduce?

Refactor

Issue Number:

Fixes #2493

Did you add tests for your changes?

Yes

Snapshots/Videos:
Screenshot 2024-12-02 at 9 24 03 AM

Summary

Migrated test for ChangeLanguageDropdown.tsx from jest to vitest.

Does this PR introduce a breaking change?

No

Have you read the contributing guide?

Yes

Summary by CodeRabbit

  • Tests
    • Improved the handling of asynchronous actions in the ChangeLanguageDropdown tests for better reliability and readability.
    • Updated test cases to ensure proper awaiting of click events, enhancing the clarity of asynchronous updates in the component state.

Copy link

coderabbitai bot commented Dec 2, 2024

Walkthrough

The pull request modifies the ChangeLanguageDropdown.spec.tsx test file to improve asynchronous handling. It removes the act wrapper around the wait function, allowing it to return a promise directly. The test cases are updated to use await with userEvent.click, ensuring proper handling of asynchronous updates. The overall structure and logic of the tests remain unchanged, with no new functionality introduced.

Changes

File Change Summary
src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx Removed act wrapper around wait, updated tests to use await with userEvent.click.

Assessment against linked issues

Objective Addressed Explanation
Replace Jest-specific functions and mocks with Vitest equivalents and rename from test.tsx to spec.tsx (#2493)
Ensure all tests pass after migration using npm run test:vitest (#2493)
Maintain the test coverage for the file as 100% after migration (#2493)

Possibly related issues

Possibly related PRs

Suggested labels

refactor

Suggested reviewers

  • pranshugupta54
  • varshith257

Poem

In the land of code, where tests do play,
A rabbit hops in, brightening the day.
With promises clear and clicks that await,
Our tests now dance, no longer sedate.
So here's to the changes, both swift and bright,
In the world of testing, all feels just right! 🐇✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d3243f6 and 1b6d7a6.

📒 Files selected for processing (1)
  • src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.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 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

github-actions bot commented Dec 2, 2024

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

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Congratulations on making your first PR! 🎊 If you haven't already, check out our Contributing Guidelines and PR Reporting Guidelines to ensure that you are following our guidelines for contributing and creating 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

🧹 Outside diff range and nitpick comments (1)
src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx (1)

20-21: Consider enhancing the wait utility function

While the implementation is functional, consider these improvements:

  1. Add JSDoc documentation to explain the purpose and usage
  2. Consider increasing the default timeout for more reliable tests
  3. Add input validation for negative timeout values
+/**
+ * Utility function to pause execution for a specified duration
+ * @param ms - The number of milliseconds to wait (default: 100)
+ * @throws {Error} If ms is negative
+ */
 async function wait(ms = 100): Promise<void> {
+  if (ms < 0) throw new Error('Timeout must be non-negative');
   await new Promise((resolve) => {
     setTimeout(resolve, ms);
   });
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c2630ae and d3243f6.

📒 Files selected for processing (1)
  • src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx (5 hunks)
🔇 Additional comments (2)
src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx (2)

1-2: LGTM: Proper migration to Vitest imports

The migration from Jest to Vitest testing functions has been implemented correctly.


Line range hint 1-158: Verify test coverage after suggested changes

While the migration to Vitest is successful, please ensure that the suggested optimizations don't affect the test coverage.

✅ Verification successful

Let me try a different approach to verify the test coverage by examining the test file structure and assertions.


Test coverage is comprehensive and well-maintained after migration

The test suite thoroughly covers all key functionality of the ChangeLanguageDropdown component:

  • Component rendering and presence of all UI elements
  • Props handling and style customization
  • Cookie handling for unset language
  • Language change functionality including both success (fr) and error (hi) cases
  • GraphQL mutation integration
  • User interactions via dropdown

The migration to Vitest preserved all test cases and their assertions without compromising coverage. No additional test coverage is needed.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify test coverage remains at 100% after applying changes
npm run test:vitest -- --coverage src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx

Length of output: 1017


Script:

#!/bin/bash
# Check the test file structure and assertions
ast-grep --pattern 'test($$$)' src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx

# Check the component implementation to verify test coverage against component features
cat src/components/ChangeLanguageDropdown/ChangeLanguageDropDown.tsx

Length of output: 14214

Comment on lines +139 to +149
await userEvent.click(getByTestId('language-dropdown-btn'));
await wait();
const changeLanguageBtn = getByTestId(`change-language-btn-fr`);
await wait();
expect(changeLanguageBtn).toBeInTheDocument();
await wait();
userEvent.click(changeLanguageBtn);
await userEvent.click(changeLanguageBtn);
await wait();
expect(cookies.get('i18next')).toBe('fr');
await wait();
userEvent.click(getByTestId('language-dropdown-btn'));
await userEvent.click(getByTestId('language-dropdown-btn'));
Copy link

@coderabbitai coderabbitai bot Dec 2, 2024

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Optimize test structure by reducing unnecessary waits

The current implementation has multiple consecutive wait() calls that could make the tests flaky and slower than necessary. Consider consolidating the waits and using more reliable ways to wait for state changes.

-    await userEvent.click(getByTestId('language-dropdown-btn'));
-    await wait();
-    const changeLanguageBtn = getByTestId(`change-language-btn-fr`);
-    await wait();
-    expect(changeLanguageBtn).toBeInTheDocument();
-    await wait();
-    await userEvent.click(changeLanguageBtn);
-    await wait();
-    expect(cookies.get('i18next')).toBe('fr');
-    await wait();
+    await userEvent.click(getByTestId('language-dropdown-btn'));
+    const changeLanguageBtn = getByTestId(`change-language-btn-fr`);
+    expect(changeLanguageBtn).toBeInTheDocument();
+    await userEvent.click(changeLanguageBtn);
+    // Wait for cookie to be set
+    await wait(200);
+    expect(cookies.get('i18next')).toBe('fr');
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
await userEvent.click(getByTestId('language-dropdown-btn'));
await wait();
const changeLanguageBtn = getByTestId(`change-language-btn-fr`);
await wait();
expect(changeLanguageBtn).toBeInTheDocument();
await wait();
userEvent.click(changeLanguageBtn);
await userEvent.click(changeLanguageBtn);
await wait();
expect(cookies.get('i18next')).toBe('fr');
await wait();
userEvent.click(getByTestId('language-dropdown-btn'));
await userEvent.click(getByTestId('language-dropdown-btn'));
await userEvent.click(getByTestId('language-dropdown-btn'));
const changeLanguageBtn = getByTestId(`change-language-btn-fr`);
expect(changeLanguageBtn).toBeInTheDocument();
await userEvent.click(changeLanguageBtn);
// Wait for cookie to be set
await wait(200);
expect(cookies.get('i18next')).toBe('fr');

Copy link
Author

Choose a reason for hiding this comment

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

Not relevant to this issue, This PR only refactors the test case to vitest

Copy link

Choose a reason for hiding this comment

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

Understood @bitbard3, thank you for the clarification. I'll keep that in mind for future reviews.


✏️ Learnings added
Learnt from: bitbard3
PR: PalisadoesFoundation/talawa-admin#2588
File: src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx:145-155
Timestamp: 2024-12-02T04:20:11.616Z
Learning: In PRs focused solely on refactoring test cases from Jest to Vitest, avoid suggesting optimizations or changes outside the migration scope.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Member

Choose a reason for hiding this comment

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

@bitbard3 Multiple wait calls make redundancy. Please apply the coderabbitai changes

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Comment on lines +149 to 156
await userEvent.click(getByTestId('language-dropdown-btn'));
await wait();
const changeLanguageBtnHi = getByTestId(`change-language-btn-hi`);
await wait();
expect(changeLanguageBtnHi).toBeInTheDocument();
await wait();
userEvent.click(changeLanguageBtnHi);
await userEvent.click(changeLanguageBtnHi);
await wait();
Copy link

@coderabbitai coderabbitai bot Dec 2, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add assertions for error handling

The test case for the Hindi language selection triggers an error (as per the mock), but doesn't verify how the error is handled. Consider adding assertions to validate the error state.

     await userEvent.click(getByTestId('language-dropdown-btn'));
-    await wait();
     const changeLanguageBtnHi = getByTestId(`change-language-btn-hi`);
-    await wait();
     expect(changeLanguageBtnHi).toBeInTheDocument();
-    await wait();
     await userEvent.click(changeLanguageBtnHi);
-    await wait();
+    await wait(200);
+    // Add assertions for error handling
+    expect(cookies.get('i18next')).not.toBe('hi');
+    // Verify error message is displayed (add appropriate test ID)
+    expect(getByTestId('error-message')).toBeInTheDocument();

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Author

Choose a reason for hiding this comment

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

Hello reviewer this changes are outside the scope of this issue, Do you still want me to add these changes?

Copy link

Choose a reason for hiding this comment

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

@bitbard3, understood. Since these changes are outside the scope of this issue, it's acceptable to leave them as is. Would you like me to open a new GitHub issue to track adding these error handling assertions in the future?


🧠 Learnings used
Learnt from: bitbard3
PR: PalisadoesFoundation/talawa-admin#2588
File: src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx:145-155
Timestamp: 2024-12-02T04:20:11.745Z
Learning: In PRs focused solely on refactoring test cases from Jest to Vitest, avoid suggesting optimizations or changes outside the migration scope.

Copy link
Author

Choose a reason for hiding this comment

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

Error handling has not been implemented in the component, so the test case does not cover error scenarios

Copy link

Choose a reason for hiding this comment

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

@bitbard3, understood. Thank you for the clarification. Since error handling is not implemented in the component, it's acceptable that the test case does not cover error scenarios.


✏️ Learnings added
Learnt from: bitbard3
PR: PalisadoesFoundation/talawa-admin#2588
File: src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx:155-162
Timestamp: 2024-12-03T05:52:37.748Z
Learning: In the `ChangeLanguageDropdown` component (`src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.tsx`), error handling has not been implemented. Therefore, test cases in `src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx` do not cover error scenarios related to error handling.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: bitbard3
PR: PalisadoesFoundation/talawa-admin#2588
File: src/components/ChangeLanguageDropdown/ChangeLanguageDropdown.spec.tsx:145-155
Timestamp: 2024-12-02T04:20:11.745Z
Learning: In PRs focused solely on refactoring test cases from Jest to Vitest, avoid suggesting optimizations or changes outside the migration scope.

Copy link
Member

Choose a reason for hiding this comment

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

Here too....

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link

codecov bot commented Dec 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.27%. Comparing base (dbe3387) to head (9ad3b98).

Additional details and impacted files
@@                  Coverage Diff                  @@
##           develop-postgres    #2588       +/-   ##
=====================================================
+ Coverage             70.01%   87.27%   +17.25%     
=====================================================
  Files                   296      313       +17     
  Lines                  7358     8204      +846     
  Branches               1606     1792      +186     
=====================================================
+ Hits                   5152     7160     +2008     
+ Misses                 1960      859     -1101     
+ Partials                246      185       -61     

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

@palisadoes
Copy link
Contributor

Please fix the failing tests. Our test code coverage system will fail if any of these conditions occur:

  1. The overall code coverage drops below the target threshold of the repository
  2. Any file in the pull request has code coverage levels below the repository threshold
  3. The code does not comply with our linting check or other related formatting requirements

We do this to ensure the overall reliability of the code base is maintained. This is a prerequisite for getting your PR approved. Assigned reviewers regularly review the PR queue and tend to focus on PRs that are passing.

Also ensure that CodeRabbit.ai approves your work

Copy link
Member

@varshith257 varshith257 left a comment

Choose a reason for hiding this comment

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

Looks good! just minor fixes needed for completion of this PR. Also can you try to increase patch coverage to 100%? you can also add new tests too

Comment on lines +139 to +149
await userEvent.click(getByTestId('language-dropdown-btn'));
await wait();
const changeLanguageBtn = getByTestId(`change-language-btn-fr`);
await wait();
expect(changeLanguageBtn).toBeInTheDocument();
await wait();
userEvent.click(changeLanguageBtn);
await userEvent.click(changeLanguageBtn);
await wait();
expect(cookies.get('i18next')).toBe('fr');
await wait();
userEvent.click(getByTestId('language-dropdown-btn'));
await userEvent.click(getByTestId('language-dropdown-btn'));
Copy link
Member

Choose a reason for hiding this comment

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

@bitbard3 Multiple wait calls make redundancy. Please apply the coderabbitai changes

Comment on lines +149 to 156
await userEvent.click(getByTestId('language-dropdown-btn'));
await wait();
const changeLanguageBtnHi = getByTestId(`change-language-btn-hi`);
await wait();
expect(changeLanguageBtnHi).toBeInTheDocument();
await wait();
userEvent.click(changeLanguageBtnHi);
await userEvent.click(changeLanguageBtnHi);
await wait();
Copy link
Member

Choose a reason for hiding this comment

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

Here too....

Copy link

This pull request did not get any activity in the past 10 days and will be closed in 180 days if no update occurs. Please verify it has no conflicts with the develop branch and rebase if needed. Mention it now if you need help or give permission to other people to finish your work.

@github-actions github-actions bot added the no-pr-activity No pull request activity label Dec 19, 2024
@raggettii
Copy link

Please assign this to me @palisadoes

@varshith257
Copy link
Member

@bitbard3 Any update on this

@varshith257
Copy link
Member

varshith257 commented Dec 19, 2024

@Cioppolo14 @palisadoes We can reassign this issue I guess if we don't have activity from the contributor or else that minor changes I will apply and merge this PR

@varshith257 varshith257 self-assigned this Dec 19, 2024
@github-actions github-actions bot removed the no-pr-activity No pull request activity label Dec 20, 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.

4 participants