Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update WooPosItems Tests - default to happy path #13728

Merged
merged 2 commits into from
Mar 17, 2025

Conversation

malinajirka
Copy link
Contributor

@malinajirka malinajirka commented Mar 12, 2025

@coderabbitai review

do not merge label - this PR is dependant on #13724 which needs to be merged first. Feel free to remove the label and merge the PR when the parent PR gets merged.

Description

This PR doesn't introduce any changes, it only refactors unit tests of WooPosItemsViewModel. I noticed the tests are quite repetitive and more importantly, the given section setups items that are not related to the code path the code is testing. This makes tests a bit tricky to read and adds friction when introducing new tests. I've refactored the code so the happy path (items successfully loaded) is the default setup, while allowing each test to modify this behavior only when needed (only when it affects the code path the test is verifying).

cc @kidinov @samiuelson @ThomazFB In case you'd disagree with this approach of setting up tests in a way that the default setup is the happy path.

Testing information

CI run is enough

The tests that have been performed

  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on big (tablet) and small (phone) in case of UI changes, and no regressions are added.

@malinajirka malinajirka added status: do not merge Dependent on another PR, ready for review but not ready for merge. category: unit tests Related to unit testing. labels Mar 12, 2025
@malinajirka malinajirka added this to the 22.0 milestone Mar 12, 2025
@malinajirka malinajirka requested a review from AnirudhBhat March 12, 2025 07:16
@malinajirka malinajirka changed the title Update tests - setup only values affecting the result Update WooPosItems Tests - setup only values affecting the result Mar 12, 2025
@malinajirka malinajirka changed the title Update WooPosItems Tests - setup only values affecting the result Update WooPosItems Tests - default to happy path Mar 12, 2025
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Mar 12, 2025

📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
App Name WooCommerce-Wear Android
Platform⌚️ Wear OS
FlavorJalapeno
Build TypeDebug
Commitba6a0d6
Direct Downloadwoocommerce-wear-prototype-build-pr13728-ba6a0d6.apk

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Mar 12, 2025

📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.

App Name WooCommerce Android
Platform📱 Mobile
FlavorJalapeno
Build TypeDebug
Commitba6a0d6
Direct Downloadwoocommerce-prototype-build-pr13728-ba6a0d6.apk

@malinajirka
Copy link
Contributor Author

malinajirka commented Mar 12, 2025

@coderabbitai review

1 similar comment
@malinajirka
Copy link
Contributor Author

@coderabbitai review

Copy link

coderabbitai bot commented Mar 12, 2025

📝 Walkthrough

Walkthrough

The pull request updates the WooPosItemsViewModel test file. Changes include adding a step in the setup to initialize a list of products using a product test utility, which is then used to mock the product data source behavior. Test comments and method names have been updated to clarify the different test phases (e.g., renaming “given loading products is failure” to “given loading products fails”) and redundant product generation code has been removed. These adjustments streamline the test structure and improve readability without affecting the underlying test logic.

Changes

File Path Change Summary
WooCommerce/.../ui/woopos/home/items/WooPosItemsViewModelTest.kt - Added product initialization using ProductTestUtils.generateProduct in the setup method.
- Updated test comments from "GIVEN" to "WHEN"/"THEN".
- Renamed test method from “given loading products is failure” to “given loading products fails”.
- Removed redundant product generation code.

Sequence Diagram(s)

sequenceDiagram
    participant TR as Test Runner
    participant VM as WooPosItemsViewModel
    participant DS as ProductsDataSource
    participant PT as ProductTestUtils

    TR->>VM: Initialize Test Environment
    VM->>PT: Generate product list
    PT-->>VM: Return product list
    VM->>DS: Call loadSimpleProducts()
    DS-->>VM: Return simulated error
    VM-->>TR: Emit error state
Loading

📜 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 b5ec003 and 3550026.

📒 Files selected for processing (1)
  • WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/items/WooPosItemsViewModelTest.kt (21 hunks)
🔇 Additional comments (6)
WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/items/WooPosItemsViewModelTest.kt (6)

53-70: Great addition of default "happy path" setup!

This change establishes a standard "happy path" configuration where products load successfully, reducing repetitive setup code across individual tests. This implementation aligns perfectly with the PR objective of improving test readability and maintainability.


75-76: Improved test structure with clear phase separation.

Restructuring the comments to clearly delineate between "WHEN" and "THEN" sections follows BDD (Behavior-Driven Development) best practices and makes the test flow easier to follow.

Also applies to: 101-102


141-141: Better grammatical structure in test name.

Renaming from "given loading products is failure" to "given loading products fails" improves readability while maintaining the same test intent.


167-168: Consistent test phase structure throughout the file.

The consistent application of clear "WHEN" and "THEN" phase comments throughout all tests creates a uniform reading experience and makes it easier to understand the purpose of each code section.

Also applies to: 183-184, 199-200, 219-222, 259-260, 303-304, 316-319, 399-400, 415-416, 433-434, 450-451, 509-510, 524-524


194-195: Simplified test by removing redundant setup.

Removing the complex product setup in favor of using a simple object definition makes the test more focused on what it's specifically testing (event sending) rather than product data structure.


270-271: Reduced test complexity by leveraging default setup.

These changes eliminate redundant setup code by relying on the default "happy path" configuration established in the setup method, making the tests more concise and focused on their specific scenarios.

Also applies to: 292-293, 315-317, 329-330, 361-362, 464-465, 480-481


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.

@AnirudhBhat AnirudhBhat self-assigned this Mar 14, 2025
Copy link
Contributor

@AnirudhBhat AnirudhBhat left a comment

Choose a reason for hiding this comment

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

Thanks for working on this. The code changes looks good to me :shipit:

Base automatically changed from issue/13722-add-dummy-entry-point-for-coupons to trunk March 17, 2025 12:42
@malinajirka malinajirka removed the status: do not merge Dependent on another PR, ready for review but not ready for merge. label Mar 17, 2025
@malinajirka malinajirka enabled auto-merge March 17, 2025 13:24
@malinajirka malinajirka merged commit ec6e8d5 into trunk Mar 17, 2025
19 checks passed
@malinajirka malinajirka deleted the update-pos-items-vm-tests branch March 17, 2025 13:25
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
category: unit tests Related to unit testing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants