test : added unit tests for date utility functions - #2079
Conversation
|
@tmdeveloper007 is attempting to deploy a commit to the ritesh Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughNew Vitest test suite for ChangesDateUtils Unit Tests
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@Frontend/src/utils/dateUtils.test.js`:
- Around line 73-78: The test for getTimeZoneAbbr doesn't exercise the catch
branch; modify the test to force Intl to throw and assert the function returns
the 'IST' fallback. Specifically, in the test for "returns IST as fallback on
Intl error" stub or mock the global Intl (or
Intl.DateTimeFormat.prototype.resolvedOptions) to throw an error when called,
call getTimeZoneAbbr(), expect exactly 'IST', and then restore the original Intl
implementation so other tests are unaffected; reference getTimeZoneAbbr and the
test block to locate where to apply the mock and restore.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 68b53806-12aa-483f-8d2c-5fd87984f7b5
📒 Files selected for processing (1)
Frontend/src/utils/dateUtils.test.js
| it('returns IST as fallback on Intl error', () => { | ||
| // The function has a try-catch that returns 'IST' on error | ||
| const result = getTimeZoneAbbr(); | ||
| // Either a real abbreviation or 'IST' fallback | ||
| expect(result).toBeTruthy(); | ||
| }); |
There was a problem hiding this comment.
Fallback-path test does not validate the actual 'IST' error branch
Line 73’s test never induces an Intl failure, so it only re-checks truthiness and can’t catch regressions in the catch/fallback path.
Suggested fix
it('returns IST as fallback on Intl error', () => {
- // The function has a try-catch that returns 'IST' on error
- const result = getTimeZoneAbbr();
- // Either a real abbreviation or 'IST' fallback
- expect(result).toBeTruthy();
+ const original = Intl.DateTimeFormat;
+ vi.spyOn(Intl, 'DateTimeFormat').mockImplementation(() => {
+ throw new Error('Intl failure');
+ });
+ expect(getTimeZoneAbbr()).toBe('IST');
+ Intl.DateTimeFormat = original;
});📝 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.
| it('returns IST as fallback on Intl error', () => { | |
| // The function has a try-catch that returns 'IST' on error | |
| const result = getTimeZoneAbbr(); | |
| // Either a real abbreviation or 'IST' fallback | |
| expect(result).toBeTruthy(); | |
| }); | |
| it('returns IST as fallback on Intl error', () => { | |
| const original = Intl.DateTimeFormat; | |
| vi.spyOn(Intl, 'DateTimeFormat').mockImplementation(() => { | |
| throw new Error('Intl failure'); | |
| }); | |
| expect(getTimeZoneAbbr()).toBe('IST'); | |
| Intl.DateTimeFormat = original; | |
| }); |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@Frontend/src/utils/dateUtils.test.js` around lines 73 - 78, The test for
getTimeZoneAbbr doesn't exercise the catch branch; modify the test to force Intl
to throw and assert the function returns the 'IST' fallback. Specifically, in
the test for "returns IST as fallback on Intl error" stub or mock the global
Intl (or Intl.DateTimeFormat.prototype.resolvedOptions) to throw an error when
called, call getTimeZoneAbbr(), expect exactly 'IST', and then restore the
original Intl implementation so other tests are unaffected; reference
getTimeZoneAbbr and the test block to locate where to apply the mock and
restore.
|
Superb implementation, @tmdeveloper007! I've successfully resolved all conflicts in your PR and queued it for merging into
Keep up the outstanding work! Let's build together! 🔥 |
c3cdf68
into
riteshbonthalakoti:gssoc
Closes #2057.
Summary of What Has Been Done:
Added comprehensive unit tests for all three date utility functions in Frontend/src/utils/dateUtils.js: formatTimelineDate, getTimeZoneAbbr, and formatFullTimestamp.
Changes Made:
Impact it Made:
Summary by CodeRabbit