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

Fix/dirty dates #132

Merged
merged 2 commits into from
Nov 10, 2024
Merged

Fix/dirty dates #132

merged 2 commits into from
Nov 10, 2024

Conversation

gutentag2012
Copy link
Owner

@gutentag2012 gutentag2012 commented Nov 10, 2024

Pull Request Template

Key Value
Status Done
Related Issues
Description Changing dates do not correctly change the dirty state
Type of change Bug fix
Is a breaking change No

TODO

  • Own review of the code
  • All tests are passing
  • The code is well documented
  • The documentation website is up-to-date
  • Tests cover new code or fixes

Summary by CodeRabbit

  • New Features
    • Enhanced test coverage for form state management and utility functions.
  • Bug Fixes
    • Improved handling of Date objects in the deepCopy function to ensure accurate copying without reference issues.
  • Tests
    • Added new test cases for verifying dirty state in form fields and correct handling of date objects in utility functions.

Copy link

coderabbitai bot commented Nov 10, 2024

Walkthrough

The pull request introduces enhancements to the test coverage for the form management logic and utility functions in the form-core package. A new test case is added to the FieldGroupLogic test suite to verify the dirty state of a form group when field values differ from their defaults. Additionally, new test cases for the getValueAtPath and deepCopy functions are included to ensure correct handling of date objects. The deepCopy function is also modified to create a new Date instance when copying date objects.

Changes

File Path Change Summary
packages/form-core/src/FieldGroupLogic.spec.ts Added a test case to check if the form group is dirty when any field differs from its default value.
packages/form-core/src/utils/access.utils.spec.ts Added test cases for getValueAtPath and deepCopy to verify correct handling of date objects.
packages/form-core/src/utils/access.utils.ts Modified deepCopy to create a new Date instance when copying date objects to ensure they do not reference the original.

Poem

In the fields where logic plays,
A rabbit hops through testing ways.
With dates and forms, we check and see,
That every change is as it should be!
New tests are added, our code's a delight,
Hopping along, everything feels right! 🐇✨


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.

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

Coverage Report for Dev Tools React Coverage

Status Category Percentage Covered / Total
🔵 Lines 100% 88 / 88
🔵 Statements 100% 106 / 106
🔵 Functions 100% 35 / 35
🔵 Branches 100% 63 / 63
File CoverageNo changed files found.
Generated in workflow #352 for commit ca048fc by the Vitest Coverage Report Action

Copy link

Coverage Report for Form Core Coverage

Status Category Percentage Covered / Total
🔵 Lines 100% 1020 / 1020
🔵 Statements 100% 1096 / 1096
🔵 Functions 100% 309 / 309
🔵 Branches 100% 630 / 630
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/form-core/src/utils/access.utils.ts 100% 100% 100% 100%
Generated in workflow #352 for commit ca048fc by the Vitest Coverage Report Action

Copy link

Coverage Report for Form React Coverage

Status Category Percentage Covered / Total
🔵 Lines 100% 114 / 114
🔵 Statements 100% 123 / 123
🔵 Functions 100% 49 / 49
🔵 Branches 100% 33 / 33
File CoverageNo changed files found.
Generated in workflow #352 for commit ca048fc by the Vitest Coverage Report Action

Copy link

Coverage Report for Validation Adapter Zod Coverage

Status Category Percentage Covered / Total
🔵 Lines 100% 16 / 16
🔵 Statements 100% 21 / 21
🔵 Functions 100% 7 / 7
🔵 Branches 100% 10 / 10
File CoverageNo changed files found.
Generated in workflow #352 for commit ca048fc by the Vitest Coverage Report Action

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 (3)
packages/form-core/src/utils/access.utils.spec.ts (2)

50-53: Consider enhancing the date test coverage.

While the test correctly verifies object identity, consider adding assertions to validate the actual date value and timestamp to ensure complete correctness.

 it('should return dates', () => {
   const date = new Date()
   expect(getValueAtPath({ date }, 'date')).toBe(date)
+  expect(getValueAtPath({ date }, 'date').getTime()).toBe(date.getTime())
 })

153-160: Consider making date value preservation more explicit.

The test effectively verifies object copying and instance separation. However, it could be more explicit about preserving the actual date value.

 it('should copy dates', () => {
   const date = new Date()
   const obj = { date }
   const copy = deepCopy(obj)
   expect(copy).toStrictEqual(obj)
   expect(copy).not.toBe(obj)
   expect(copy.date).toStrictEqual(obj.date)
+  expect(copy.date.getTime()).toBe(obj.date.getTime())
+  expect(copy.date).not.toBe(obj.date)  // Explicitly verify date was copied
 })
packages/form-core/src/FieldGroupLogic.spec.ts (1)

208-232: Consider adding test cases for date edge cases.

The current test effectively covers the basic date modification scenario. Consider enhancing coverage with additional test cases for:

  • Setting date to null/undefined
  • Modifying individual date components
  • Multiple date field changes in sequence

Example test case structure:

it('should handle null/undefined date values correctly', () => {
  const form = new FormLogic({
    defaultValues: {
      date: new Date(2024, 1, 1)
    }
  })
  // Test setting to null
  // Test setting to undefined
  // Verify dirty states
})

it('should track dirty state when modifying date components', () => {
  const form = new FormLogic({
    defaultValues: {
      date: new Date(2024, 1, 1)
    }
  })
  // Test modifying year
  // Test modifying month
  // Test modifying day
  // Verify dirty states for each change
})
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 910fb6a and ca048fc.

📒 Files selected for processing (3)
  • packages/form-core/src/FieldGroupLogic.spec.ts (1 hunks)
  • packages/form-core/src/utils/access.utils.spec.ts (2 hunks)
  • packages/form-core/src/utils/access.utils.ts (1 hunks)
🔇 Additional comments (3)
packages/form-core/src/utils/access.utils.ts (1)

158-160: LGTM! Correct implementation of Date object deep copying.

The implementation properly creates a new Date instance using getTime(), which ensures value equality while breaking reference equality. This fixes the dirty state detection for dates.

Let's verify the usage of dates in forms across the codebase:

✅ Verification successful

Date object deep copying implementation is correctly used across the codebase

The verification confirms that the Date handling in deepCopy is appropriate for the codebase's usage patterns:

  • Test files properly verify Date object state management in forms
  • Forms handle Date fields with appropriate transformations (string-to-Date and vice-versa)
  • Date objects are used in form validation (max date constraints)
  • The implementation correctly supports both direct Date field usage and Date objects within nested form structures
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for Date object usage in form-related code
# Expected: Find form field definitions or state management code using Date objects

# Look for Date type definitions in form contexts
ast-grep --pattern 'type $_ = {
  $$$
  $_: Date
  $$$
}'

# Look for Date instantiation in form-related code
rg -l 'new Date' | xargs rg -l 'form|Form'

Length of output: 517


Script:

#!/bin/bash
# Let's examine the actual usage of dates in form contexts to verify the implementation

# Check test files for Date handling scenarios
rg -A 5 -B 5 "new Date" "packages/form-core/src/FormLogic.spec.ts" "packages/form-core/src/FieldGroupLogic.spec.ts"

# Check form components for Date field usage patterns
rg -A 5 -B 5 "new Date" "examples/react/async-form-signals/src/components/application/UserForm.tsx" "examples/react/simple-form-signals/src/App.tsx"

# Look for any date-related validation or transformation logic
ast-grep --pattern 'function $_($$_) {
  $$$
  new Date
  $$$
}'

Length of output: 5718

packages/form-core/src/utils/access.utils.spec.ts (1)

153-160: Verify the deepCopy implementation for dates.

The test changes look good and align with the PR objective. Let's verify the corresponding implementation changes.

✅ Verification successful

Implementation correctly handles date objects

The implementation properly creates a new Date instance using getTime(), which ensures deep copying of date objects as verified by the test cases.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check the implementation of date handling in deepCopy function

# Test: Search for date handling in the implementation
rg -A 5 "instanceof Date" packages/form-core/src/utils/access.utils.ts

# Test: Look for the deepCopy function implementation
ast-grep --pattern $'function deepCopy($_) {
  $$$
}'

Length of output: 363

packages/form-core/src/FieldGroupLogic.spec.ts (1)

208-232: LGTM! Test case effectively verifies date handling in dirty state.

The test case thoroughly validates the form's dirty state behavior when modifying date fields:

  1. Properly initializes with different dates for comparison
  2. Verifies initial clean state across form, group, and fields
  3. Confirms dirty state updates when date value changes

@gutentag2012 gutentag2012 merged commit 392a53a into main Nov 10, 2024
5 checks passed
@gutentag2012 gutentag2012 deleted the fix/dirty-dates branch November 10, 2024 22:02
@coderabbitai coderabbitai bot mentioned this pull request Nov 25, 2024
5 tasks
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