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: mock property type support symbol and number #3

Merged
merged 1 commit into from
Dec 21, 2024

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Dec 21, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced mocking functionality to support a broader range of key types, including symbols and numbers.
    • Added new test cases for mocking Symbol properties and number properties.
  • Bug Fixes

    • Updated tests to ensure original properties are correctly restored after mocking.
  • Documentation

    • Updated test suite documentation to reflect changes in key handling and new test cases.

Copy link

coderabbitai bot commented Dec 21, 2024

Walkthrough

The changes introduce enhanced flexibility in key handling for a mocking utility by expanding the key type from string to PropertyKey. This modification allows mocking with various key types, including strings, numbers, and symbols. The updates span both the implementation in src/index.ts and the corresponding test suite in test/method.test.ts, with tests added to validate mocking capabilities for Symbol and numeric properties.

Changes

File Change Summary
src/index.ts - Updated MockItem interface to use PropertyKey for key
- Modified mock() function signature to accept PropertyKey
- Updated isMocked() function signature to accept PropertyKey
test/method.test.ts - Added fooSymbol constant
- Extended config object to include Symbol and numeric keys
- Added test cases for Symbol and numeric property mocking

Sequence Diagram

sequenceDiagram
    participant User
    participant MockUtility
    participant TargetObject

    User->>MockUtility: mock(object, key, value)
    MockUtility->>TargetObject: Set/Replace property
    MockUtility-->>User: Mocked property
    
    User->>MockUtility: isMocked(object, key)
    MockUtility-->>User: Boolean (mocked status)
Loading

Poem

🐰 A Rabbit's Ode to Flexible Keys 🔑

Strings, symbols, numbers galore,
Mocking magic opens every door!
PropertyKey, oh what a delight,
Flexibility shines so wonderfully bright!
Mock away, with keys of every kind! 🎉

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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.

CodeRabbit Configuration 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

pkg-pr-new bot commented Dec 21, 2024

Open in Stackblitz

npm i https://pkg.pr.new/node-modules/muk-prop.js/@cnpmjs/muk-prop@3

commit: 1e63442

Copy link

codecov bot commented Dec 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (49d3982) to head (1e63442).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master        #3   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           81        81           
  Branches        11        10    -1     
=========================================
  Hits            81        81           

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

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

🧹 Nitpick comments (1)
test/method.test.ts (1)

140-143: Consider adding Symbol enumeration test

While the test correctly verifies number property enumeration, consider adding assertions to verify that Symbol properties are not enumerated by Object.keys (which is the expected behavior).

Add this assertion after line 143:

 assert.deepEqual(Object.keys(config), [ '1', 'enableCache', 'delay' ]);
+// Verify Symbol properties are not enumerated
+assert.deepEqual(Object.getOwnPropertySymbols(config), [fooSymbol]);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 42046e3 and 1e63442.

📒 Files selected for processing (2)
  • src/index.ts (3 hunks)
  • test/method.test.ts (3 hunks)
🔇 Additional comments (6)
src/index.ts (3)

16-16: LGTM! Function signature update aligns with interface change

The mock function's key parameter type has been correctly updated to support all property key types.


78-78: LGTM! Function signature update is consistent

The isMocked function's key parameter type has been correctly updated to match the mock function signature.


4-4: LGTM! Note: Breaking change in MockItem interface

The change from string to PropertyKey correctly enables support for symbol and number property types. However, this is a breaking change that may affect consumers of this interface.

Let's check for any direct implementations of this interface:

✅ Verification successful

LGTM! Breaking change is properly contained within the module

The change from string to PropertyKey in the MockItem interface is safe to proceed with. After verification:

  • The interface is only used internally within the same file
  • The only usage is in the mocks array which is a private variable
  • All implementations (object literals) are created within the mock function itself
  • The change aligns with the function signature which already accepts PropertyKey
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any direct implementations of MockItem interface
rg "implements MockItem" 
rg "class \w+\s*:\s*MockItem"

Length of output: 58


Script:

#!/bin/bash
# Let's try a more comprehensive search for MockItem usage
# First, let's find the file containing MockItem definition
fd "MockItem" --type f

# Then search for any usage of MockItem in type declarations or class extensions
rg ":\s*MockItem" -A 2
rg "extends\s+MockItem" -A 2
rg "implements\s+MockItem" -A 2

# Also search for type assertions or casts to MockItem
rg "as\s+MockItem" -A 2

Length of output: 246


Script:

#!/bin/bash
# Let's examine how MockItem is used in the codebase
# First, let's see the content of the file where it's defined
cat src/index.ts

# Also search for any object literal that matches MockItem structure
rg "{\s*key:" -A 2

Length of output: 1900

test/method.test.ts (3)

75-80: LGTM! Good test setup for new property types

The test setup correctly defines both symbol and number properties for testing the new functionality.


95-102: LGTM! Comprehensive test coverage for Symbol properties

The test case thoroughly verifies mocking, checking mocked state, and restoration of Symbol properties.


104-111: LGTM! Well-structured test for number properties

The test case maintains consistency with the Symbol test pattern and provides good coverage for number properties.

@fengmk2 fengmk2 merged commit 79bf8af into master Dec 21, 2024
23 checks passed
@fengmk2 fengmk2 deleted the support-symbol branch December 21, 2024 15:28
fengmk2 pushed a commit that referenced this pull request Dec 21, 2024
[skip ci]

## [1.1.0](v1.0.0...v1.1.0) (2024-12-21)

### Features

* mock property type support symbol and number ([#3](#3)) ([79bf8af](79bf8af))
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.

1 participant