Skip to content

fix(utc): resolve UTC date handling bug and migrate tests to Vitest - #100

Open
HaidarZ wants to merge 1 commit into
shubhadip:masterfrom
mandeplus:fix/utc-handling
Open

fix(utc): resolve UTC date handling bug and migrate tests to Vitest#100
HaidarZ wants to merge 1 commit into
shubhadip:masterfrom
mandeplus:fix/utc-handling

Conversation

@HaidarZ

@HaidarZ HaidarZ commented Dec 31, 2025

Copy link
Copy Markdown

Summary

Fixes a critical bug where the use-utc prop was not consistently applied across date picker components, causing date/month selection to be off by one when UTC mode was enabled. Additionally migrates the test infrastructure from Jest to Vitest for better Vue 3 compatibility.

Problem

When using use-utc prop set to true:

  • Selecting a date would select the day before the intended date
  • Selecting a month would select the month before the intended month
  • This occurred because UTC handling was inconsistent - the useUtc parameter wasn't being properly passed through the component tree

Solution

UTC Fix:

  • Refactored DateUtils to use a factory pattern makeDateUtils(useUtc) that returns a utility object with UTC behavior baked in
  • This approach aligns with the original UTC handling pattern from the upstream vuejs-datepicker
  • Updated all picker components (PickerDay, PickerMonth, PickerYear, DateInput) to instantiate DateUtils with the useUtc prop value at setup time
  • Added missing use-utc prop binding to PickerDay component in DatePickerComponent
  • Re-enabled disabled date ranges functionality with corrected logic (using >= and <= for inclusive range checking)

Test Infrastructure:

  • Migrated from Jest to Vitest 4.0.16 (modern, Vite-native testing framework)
  • Replaced jsdom with happy-dom for faster, lighter DOM simulation
  • Updated @vue/test-utils to 2.4.6 (latest compatible version)
  • Fixed 3 failing tests:
    • Replaced jest.spyOn with vi.spyOn for Vitest compatibility
    • Removed deprecated done callback in async test
    • Updated DateUtils tests to use new factory pattern API
  • All 126 tests now passing ✅

Changes

Core Files

  • src/components/datepicker/utils/DateUtils.ts - Factory pattern implementation
  • src/components/datepicker/DatePickerComponent.vue - Added missing use-utc prop
  • src/components/datepicker/PickerDay.vue - Use makeDateUtils factory, enabled ranges
  • src/components/datepicker/PickerMonth.vue - Use makeDateUtils factory
  • src/components/datepicker/PickerYear.vue - Use makeDateUtils factory
  • src/components/datepicker/DateInput.vue - Use makeDateUtils factory

Test Infrastructure

  • vitest.config.ts - New Vitest configuration
  • tests/unit/specs/DateUtils.spec.js - Updated for new API
  • tests/unit/specs/DateInput/typedDates.spec.js - Fixed vi.spyOn
  • tests/unit/specs/Datepicker/Datepicker.spec.js - Fixed async/done
  • package.json - Added Vitest dependencies and scripts
  • .gitignore - Added coverage/ and .eslintcache
  • .eslintignore - Added vitest.config.ts
  • tsconfig.json - Added vitest.config.ts to include

Build Artifacts

  • build/DatePickerComponent.css
  • build/vuejs3-datepicker.js
  • build/vuejs3-datepicker.umd.cjs

Testing

  • ✅ All 126 tests passing
  • ✅ 9 new UTC-specific tests validating the fix
  • ✅ Library builds successfully
  • ✅ Linting passes

Breaking Changes

None. The changes are backward compatible - components using the default useUtc: false behavior remain unchanged.

Related Issues

Fixes the UTC date selection bug reported in the original issue.

When using the `use-utc` prop, dates and months were being selected as
the day/month before the intended selection. This occurred because UTC
handling was inconsistent across picker components - the `useUtc`
parameter was not being passed through the component tree properly.

Problem:
- DateUtils functions accepted `useUtc` as an optional parameter with
  default value `false`, but components weren't consistently passing
  the prop value, causing UTC dates to be interpreted in local time
- The `use-utc` prop was missing from PickerDay component declaration
- Disabled date ranges functionality was commented out and non-functional

Solution:
- Refactored DateUtils to use a factory pattern `makeDateUtils(useUtc)`
  that returns a utility object with UTC mode baked in, ensuring
  consistent behavior throughout the component lifecycle
- This approach aligns with the original UTC handling pattern from the
  upstream vuejs-datepicker (https://github.com/charliekassel/vuejs-datepicker)
- Updated all picker components (PickerDay, PickerMonth, PickerYear,
  DateInput, DatePickerComponent) to instantiate DateUtils with the
  `useUtc` prop value at setup time
- Added missing `use-utc` prop binding to PickerDay in parent template
- Re-enabled disabled date ranges functionality with corrected logic
  (using >= and <= for inclusive range checking)

Test Infrastructure Migration (Jest -> Vitest 4.0.16):
- Added vitest.config.ts with happy-dom environment and path aliases
- Updated package.json with Vitest scripts and dependencies
- Fixed 3 failing tests:
  - jest.spyOn -> vi.spyOn in typedDates.spec.js
  - Removed async/done callback anti-pattern in Datepicker.spec.js
  - Updated DateUtils.spec.js to use new factory pattern API
- All 126 tests now passing

Technical changes:
- DateUtils: Export `makeDateUtils(useUtc)` factory + default instance
- Components: Create dateUtils instance in setup() with props.useUtc
- PickerDay: Add `use-utc` prop to component props definition
- DatePickerComponent: Pass `:use-utc="useUtc"` to PickerDay
- Build outputs regenerated with fixes
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