modifing yyy label, spy reset after each test gets finished

This commit is contained in:
Cibiyanna26 2025-03-25 14:01:03 +05:30
parent 85d89de072
commit 42739c660e
2 changed files with 11 additions and 7 deletions

View file

@ -41,19 +41,23 @@ describe('renderNumberLabels', () => {
});
});
// test for renderDateLabels
jest.spyOn(require('lib/date'), 'formatDate'); // Spy on formatDate but use real implementation
describe('renderDateLabels', () => {
const mockValues = [{ value: '2024-03-23T10:00:00Z' }, { value: '2024-03-24T15:30:00Z' }];
beforeEach(() => {
jest.spyOn(require('lib/date'), 'formatDate');
});
afterEach(() => {
jest.restoreAllMocks(); // Reset spy to prevent interference
});
test.each([
['minute', 'h:mm', 'en-US'],
['hour', 'p', 'en-US'],
['day', 'MMM d', 'en-US'],
['month', 'MMM', 'en-US'],
['year', 'YYY', 'en-US'],
['year', 'yyyy', 'en-US'],
])('formats date correctly for unit: %s', (unit, expectedFormat, locale) => {
const formatLabel = renderDateLabels(unit, locale);
const formatted = formatLabel('label', 0, mockValues);
@ -67,7 +71,7 @@ describe('renderDateLabels', () => {
expect(formatLabel('original-label', 0, mockValues)).toBe('original-label');
});
test('handles invalid date gracefully', () => {
test('throws error for invalid date input', () => {
const invalidValues = [{ value: 'invalid-date' }];
const formatLabel = renderDateLabels('day', 'en-US');

View file

@ -19,7 +19,7 @@ export function renderDateLabels(unit: string, locale: string) {
case 'month':
return formatDate(d, 'MMM', locale);
case 'year':
return formatDate(d, 'YYY', locale);
return formatDate(d, 'yyyy', locale);
default:
return label;
}