Skip to content

Commit df7e0e8

Browse files
Copilothotlong
andcommitted
fix: replace hardcoded dates with dynamic dates in cell-renderers tests
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent de34ff0 commit df7e0e8

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

packages/fields/src/__tests__/cell-renderers.test.tsx

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,15 @@ describe('SelectCellRenderer', () => {
186186
// 3. DateCellRenderer
187187
// =========================================================================
188188
describe('DateCellRenderer', () => {
189-
let nowSpy: ReturnType<typeof vi.spyOn>;
190-
191189
beforeEach(() => {
192-
// Fix "now" to a known date: 2026-02-24T12:00:00Z
193-
nowSpy = vi.spyOn(Date, 'now').mockReturnValue(new Date('2026-02-24T12:00:00Z').getTime());
190+
// Fix "now" to a known date: 2026-02-24T12:00:00Z using fake timers
191+
// so that new Date() in formatRelativeDate returns a predictable value
192+
vi.useFakeTimers();
193+
vi.setSystemTime(new Date('2026-02-24T12:00:00Z'));
194194
});
195195

196196
afterEach(() => {
197-
nowSpy.mockRestore();
197+
vi.useRealTimers();
198198
});
199199

200200
it('should render today date as "Today"', () => {
@@ -402,36 +402,48 @@ describe('formatRelativeDate', () => {
402402
});
403403

404404
it('should return "Today" for today', () => {
405-
expect(formatRelativeDate('2026-02-24T08:00:00Z')).toBe('Today');
405+
const today = new Date();
406+
expect(formatRelativeDate(today)).toBe('Today');
406407
});
407408

408409
it('should return "Yesterday" for yesterday', () => {
409-
expect(formatRelativeDate('2026-02-23T08:00:00Z')).toBe('Yesterday');
410+
const yesterday = new Date();
411+
yesterday.setDate(yesterday.getDate() - 1);
412+
expect(formatRelativeDate(yesterday)).toBe('Yesterday');
410413
});
411414

412415
it('should return "Tomorrow" for tomorrow', () => {
413-
expect(formatRelativeDate('2026-02-25T08:00:00Z')).toBe('Tomorrow');
416+
const tomorrow = new Date();
417+
tomorrow.setDate(tomorrow.getDate() + 1);
418+
expect(formatRelativeDate(tomorrow)).toBe('Tomorrow');
414419
});
415420

416421
it('should return "Overdue Xd" for 2-7 days ago', () => {
417-
expect(formatRelativeDate('2026-02-21T08:00:00Z')).toBe('Overdue 3d');
422+
const threeDaysAgo = new Date();
423+
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);
424+
expect(formatRelativeDate(threeDaysAgo)).toBe('Overdue 3d');
418425
});
419426

420427
it('should return formatted date for >7 days ago', () => {
421-
const result = formatRelativeDate('2026-02-10T08:00:00Z');
422-
// Should be a formatted date like "Feb 10, 2026", not "14 days ago"
423-
expect(result).toContain('Feb');
424-
expect(result).toContain('2026');
428+
const tenDaysAgo = new Date();
429+
tenDaysAgo.setDate(tenDaysAgo.getDate() - 10);
430+
const result = formatRelativeDate(tenDaysAgo);
431+
const expected = tenDaysAgo.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
432+
expect(result).toBe(expected);
425433
});
426434

427435
it('should return "In X days" for 2-7 days in the future', () => {
428-
expect(formatRelativeDate('2026-02-28T08:00:00Z')).toBe('In 4 days');
436+
const fourDaysFromNow = new Date();
437+
fourDaysFromNow.setDate(fourDaysFromNow.getDate() + 4);
438+
expect(formatRelativeDate(fourDaysFromNow)).toBe('In 4 days');
429439
});
430440

431441
it('should return formatted date for >7 days in the future', () => {
432-
const result = formatRelativeDate('2026-03-15T08:00:00Z');
433-
expect(result).toContain('Mar');
434-
expect(result).toContain('2026');
442+
const fifteenDaysFromNow = new Date();
443+
fifteenDaysFromNow.setDate(fifteenDaysFromNow.getDate() + 15);
444+
const result = formatRelativeDate(fifteenDaysFromNow);
445+
const expected = fifteenDaysFromNow.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
446+
expect(result).toBe(expected);
435447
});
436448
});
437449

0 commit comments

Comments
 (0)